Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1259446

    Hello Sirs,
    How are you?
    In Product Loop View (as Front Page Carousel, Related Products, Shop Page e.t.c.) it does not update the quantities as expected. For example, I select 3 items thru the quantity selector and it drops one item in the cart.
    Please see the following screenshot:

    View post on imgur.com

    Could you help me with it?
    I use the following Function:

    function shop_page_add_quantity_field() {

    /** @var WC_Product $product */
    $product = wc_get_product( get_the_ID() );

    if ( ! $product->is_sold_individually() && ‘variable’ != $product->get_type() && $product->is_purchasable() ) {
    woocommerce_quantity_input( array( ‘min_value’ => 1, ‘max_value’ => $product->backorders_allowed() ? ” : $product->get_stock_quantity() ) );
    }

    }
    add_action( ‘woocommerce_after_shop_loop_item’, ‘shop_page_add_quantity_field’, 20, 999 );

    /**
    * Add required JavaScript.
    */
    function shop_page_quantity_add_to_cart_handler() {

    wc_enqueue_js( ‘
    $(“.woocommerce .products”).on(“click”, “.quantity input”, function() {
    return false;
    });
    $(“.woocommerce .products”).on(“change input”, “.quantity .qty”, function() {
    var add_to_cart_button = $(this).parents( “.product” ).find(“.add_to_cart_button”);

    // For AJAX add-to-cart actions
    add_to_cart_button.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());
    });

    // Trigger on Enter press
    $(“.woocommerce .products”).on(“keypress”, “.quantity .qty”, function(e) {
    if ((e.which||e.keyCode) === 13) {
    $( this ).parents(“.product”).find(“.add_to_cart_button”).trigger(“click”);
    }
    });
    ‘ );

    }
    add_action( ‘init’, ‘shop_page_quantity_add_to_cart_handler’ );

    I am looking forward to your precious support.
    Thank You in advance,

    Regards,
    Nick

    #1260034

    Hey nickgin,

    Thank you for the inquiry.

    Where can we see this issue? Did you also modify the add to cart button? It is possible that the default add to cart button from the theme does not recognize the changes in the quantity input, or that the selectors used in the script do not match the actual elements in the product page.

    Best regards,
    Ismael

    #1260424

    Hi Ismael,
    Thank You for your Great Support!
    I don’t think, I have modified the add to cart button.
    Could you look at the following link sent in private section?

    Thank You in advance,

    Best regards,
    Nick

    #1261229

    Hi,

    Thank you for the update.

    It might not be working because in the woocommerce_quantity_input function, the $product is null or it is using the global product variable. Try to return the current $product as the second parameter in the function, and set the max_value to -1 when back orders are allowed.

    // http://hookr.io/functions/woocommerce_quantity_input/

    Please look for this code..

    woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ) );
    

    .., then replace it with:

    woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? -1 : $product->get_stock_quantity() ), $product );
    

    Where did you get the snippet above?

    Best regards,
    Ismael

    #1261448

    Hi Ismael,
    Thank You for your quick support!
    Unfortunately, it didn’t work…
    I really don’t remeber where I got it…

    I am looking forward to your news.

    Best regards,
    Nick

    #1261790

    Hi,

    The add to cart button also relies on the data-quantity attribute, but the value of this attribute does not change and does not respond to the quantity adjustment, so it is always set to 1. Please ask the script authors if they could adjust the script so that the value of the data-quantity attribute changes in response to the adjustment in the quantity input. The quantity field or input and the data-quantity attribute should have the same value.

    // For AJAX add-to-cart actions
    add_to_cart_button.data(“quantity”, $(this).val());
    

    Thank you for your patience.

    Best regards,
    Ismael

    #1262538

    Hi Ismael,
    Thank you for your quidelines and your support.
    Please, I have posted a second request but I haven’t received any answer for 15 days..
    Could you help me on that please?

    I am looking forward to your reply.
    Thank You in advance.

    Best Regards,
    Nick

    #1263435

    Hi,

    One of the moderators have already replied to the previous thread. Did you manage to adjust the script so that the value of the data-quantity responds or adjusts based on the actual registered quantity for the product? Please contact the script author for more info about the script.

    Best regards,
    Ismael

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.