code error
-
this is a code to make 4 minimum,quantity increases by 4,order only accept multiples of 4 and when i use it first time increases the quantity from 4 to 5 after that it start increases 4 so it goes like 4,5,9,13,17,21..... i want to know the error because i want it to go like 4,8,12,16,20 : function wc_minimum_order_quantity( $passed, $product_id, $quantity ) {
// Define the categories that should require a minimum quantity
$required_categories = array( 'cocuk-takim' );
// Define the quantity step for these categories
$quantity_step = 4;
// Get the product object
$product = wc_get_product( $product_id );
// Check if the product belongs to one of the required categories
if ( has_term( $required_categories, 'product_cat', $product_id ) ) {
// Check if the quantity is less than the quantity step
if ( $quantity < $quantity_step ) {
// Set the quantity to the quantity step
$quantity = $quantity_step;
// Set a notice message
wc_add_notice( sprintf( ( 'Please purchase in multiples of %d', 'woocommerce' ), $quantity_step ), 'notice' ); } else { // Calculate the remainder of the quantity divided by the quantity step $remainder = $quantity % $quantity_step; // If the remainder is not zero, adjust the quantity if ( $remainder != 0 ) { // Calculate the new quantity $new_quantity = $quantity - $remainder + $quantity_step; // Set the new quantity $quantity = $new_quantity; // Set a notice message wc_add_notice( sprintf( ( 'Please purchase in multiples of %d', 'woocommerce' ), $quantity_step ), 'notice' );
}
}
}
return $quantity;
} function wc_quantity_input_args( $args, $product ) {
// Get the product ID
$product_id = $product->get_id();
// Get the product object
$product = wc_get_product( $product_id );
// Check if the product belongs to one of the required categories
if ( has_term( array( 'cocuk-takim' ), 'product_cat', $product_id ) ) {
// Set the step value to 4
$args['step'] = 4;
// Set the default value to 4
$args['input_value'] = 4;
// Set the minimum value to 4
$args['min_value'] = 4;
}
return $args;
} add_filter( 'woocommerce_quantity_input_args', 'wc_quantity_input_args', 10, 2 );
add_filter( 'woocommerce_add_to_cart_validation', 'wc_minimum_order_quantity', 10, 3 );The page I need help with: [log in to see the link]
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
The topic ‘code error’ is closed to new replies.