【问题标题】:Problem with product quantity when add to cart添加到购物车时产品数量问题
【发布时间】:2022-10-01 20:09:54
【问题描述】:

我有一个问题,是否有人可以帮助我了解添加到购物车中的产品数量 - 例如,当我在数量 2 中添加产品 x 时,当我想添加任意数量的其他产品时,例如y,然后它在购物车中显示购物车中的 2 件商品。产品 x 的数量不会重置,好像只有当它“重置”自己时,我才能添加不同数量的不同产品。这就像我是一个客户,他标记了 2 件产品 x,但没有添加到购物篮就退出了,并选择了 1 件产品 y,然后我阅读了 2 件产品 x。有谁知道如何解决它?请帮忙。网站 - https://www.fabrykaprzypraw.com.pl/blog22/

    /**
 * Add quantity field on the archive page.
 */
function custom_quantity_field_archive() {

    $product = wc_get_product( get_the_ID() );

    //if ( ! $product->is_sold_individually() && \'variable\' != $product->product_type && $product->is_purchasable() ) { //niki. ako ne go iskash za variable products. az go iskam
    if ( ! $product->is_sold_individually() && $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\', \'custom_quantity_field_archive\', 15, 9 );

function custom_add_to_cart_quantity_handler() {
wc_enqueue_js( \'
jQuery( \"body\" ).on( \"click\", \".quantity input\", function() {
return false;
});
jQuery( \"body\" ).on( \"change input\", \".quantity .qty\", function() {
var add_to_cart_button = jQuery( this ).parents( \".product\" ).find( \".add_to_cart_button\" );
// For AJAX add-to-cart actions
add_to_cart_button.attr( \"data-quantity\", jQuery( 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=\" + jQuery( this ).val() );
});
\' );
}
add_action( \'init\', \'custom_add_to_cart_quantity_handler\' );

    标签: php wordpress woocommerce


    【解决方案1】:

    我遇到了同样的问题,我通过稍微更改 JavaScript 解决了这个问题。此 JavaScript 必须在方法 custom_add_to_cart_quantity_handler 中的 functions.php

     wc_enqueue_js( '
            $(".woocommerce .products").on("click", ".quantity input", function() {
                return false;
            });
            $(".woocommerce li.product").on("change input", ".quantity .qty", function() {
                var add_to_cart_button = $(this).parent( ".quantity" ).parent(".product").find(".add_to_cart_button");
                console.log(add_to_cart_button);
                // For AJAX add-to-cart actions
                add_to_cart_button.attr("data-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");
                }
            });
        ' );

    【讨论】:

      猜你喜欢
      • 2016-02-28
      • 1970-01-01
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多