• Resolved minsuwai

    (@minsuwai)


    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 );
    Here is the variable product
Viewing 1 replies (of 1 total)
  • Plugin Support mouli a11n

    (@mouli)

    @minsuwai
    Interesting question however I am not a developer so I’m not the best person to help you. Hopefully someone with more coding experience can offer some help.

    From what I see it looks like if the product is a variable product it simply runs that woocommerce_variable_add_to_cart(); function which would simply add it to the cart. This is where I suggest you start looking for the reason it isn’t allowing multiples to be added.

    I hope that helps you to figure it out.
    Feel free to get back to us if you have further questions.

Viewing 1 replies (of 1 total)
  • The topic ‘Uncorrectly handle the quantity button for variable product’ is closed to new replies.