ajax在woocommerce中增加或减少数量?

时间:2019-11-05 02:24:16

标签: php wordpress woocommerce hook-woocommerce

当我进入CART页面时,我正在使用此代码更新购物车-

jQuery('div.woocommerce').on('change keyup mouseup', 'input.qty', function(){ // keyup and mouseup for Firefox support
    if (timeout != undefined) clearTimeout(timeout); //cancel previously scheduled event
    if (jQuery(this).val() == '') return; //qty empty, instead of removing item from cart, do nothing
    timeout = setTimeout(function() {
        jQuery('[name="update_cart"]').trigger('click');
    }, 1000 );
});

我也有与商店页面中相同的增加减少按钮,并尝试使用此代码执行相同的操作

jQuery('li.product').on('change keyup mouseup', 'input.qty', function(){ // keyup and mouseup for Firefox support
    if (timeout != undefined) clearTimeout(timeout); //cancel previously scheduled event
    if (jQuery(this).val() == '') return; //qty empty, instead of removing item from cart, do nothing
    timeout = setTimeout(function() {
        jQuery('[name="update_cart"]').trigger('click');
    }, 1000 );
});

但是它不起作用,如何解决呢?

2 个答案:

答案 0 :(得分:0)

Remove disabled property before update cart

jQuery("[name='update_cart']").prop("disabled", false);

So code will be:

jQuery('li.product').on('change keyup mouseup', 'input.qty', function(){ // keyup and mouseup for Firefox support
    if (timeout != undefined) clearTimeout(timeout); //cancel previously scheduled event
    if (jQuery(this).val() == '') return; //qty empty, instead of removing item from cart, do nothing
    timeout = setTimeout(function() {
       jQuery("[name='update_cart']").prop("disabled", false);
       jQuery('[name="update_cart"]').trigger('click');
    }, 1000 );
});

答案 1 :(得分:0)

var timeout;

jQuery( function( $ ) {
$('.woocommerce').on('change', 'input.qty', function(){

    if ( timeout !== undefined ) {
        clearTimeout( timeout );
    }

    timeout = setTimeout(function() {
        $("[name='update_cart']").trigger("click");
    }, 1000 ); // 1 second delay, half a second (500) seems comfortable too

   });
} );