John Davidson

php - Use minimum purchase quantity in quantities form WooCommerce Shop page

0 comments
Message:


I'm using this plugin for minimum order quantities and quantity packaging for products on my website.
I added this code to functions.php to show quantities next to the add to cart buttons with - and + buttons:


add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input( array(), $product, false );
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
$html .= '</form>';
}
return $html;
}

This code works, it properly adds the package quantity every time when I click the + button. E.g. if the package quantity is '4', it now goes from 1 to 4, to 8, to 12, etc. and also back in steps of 4 when clicking the - button.


The only problem I have is that the quantity form always starts with '1' and I would like it to start with the minimum order quantity, e.g. 4. But this is of course different per product and not always required.


Any help would be much appreciated.



My Answer:
->is_sold_individually() ) {
return $html;
}

ob_start();

woocommerce_quantity_input( array(
'input_value' => 1,
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
), $product );

return ob_get_clean() . $html;
}

This code will display quantity inputs next to the add to cart buttons on the WooCommerce Shop page, with the minimum purchase quantity set for each product. You can adjust the min_value and max_value parameters to fit your specific requirements.

Rate this post

3 of 5 based on 9536 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us