Uncorrectly handle the quantity button for variable product
-
I added quantity plus, minus button for both simple product and variable product. There correctly handled and correctly work for simple product, but did not correctly get the quantity for variable product for example: I set the quantity to ‘3’, the quantity did not get ‘3’, there only one get the variable product in the cart. Here is the code below
/** * Display QTY Input before add to cart link. */ function custom_wc_template_loop_quantity_input() { // Global Product. global $product; // Check if the product is not null, is purchasable, is in stock, and not sold individually. if ( $product && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) { if ( $product->is_type( 'simple' ) ) { // For simple products. woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity(), ) ); } elseif ( $product->is_type( 'variable' ) ) { // For variable products. woocommerce_variable_add_to_cart(); } } } add_action( 'woocommerce_after_shop_loop_item', 'custom_wc_template_loop_quantity_input', 9 ); /** * Add JS script in <head/> tag. */ function custom_wc_add_qty_change_script() { ?> <script> (function ($) { $(document).on("change", "li.product .quantity input.qty", function (e) { e.preventDefault(); var add_to_cart_button = $(this).closest("li.product").find("a.add_to_cart_button"); // For AJAX add-to-cart actions. add_to_cart_button.attr("data-quantity", $(this).val()); // For non-AJAX add-to-cart actions. add_to_cart_button.attr("href", "?add-to-cart=" + add_to_cart_button.attr("data-product_id") + "&quantity=" + $(this).val()); }); })(jQuery); </script> <?php } add_action( 'wp_head', 'custom_wc_add_qty_change_script', 20 );
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Uncorrectly handle the quantity button for variable product’ is closed to new replies.