简体   繁体   English

在 Woocommerce 购物车页面中自动更新购物车数量变化

[英]Auto update cart on quantity change in Woocommerce cart page

I have a $5 fee that gets added to the order total when using lets say "Cash on Delivery".使用“货到付款”时,我有 5 美元的费用会被添加到订单总额中。 This fee should be removed when switching to "bank transfer".切换到“银行转帐”时应取消此费用。 This fee gets removed just fine when I trigger the order review table to update by lets say changing the billing zip code.当我通过更改帐单邮政编码触发订单审核表进行更新时,这笔费用就会被删除。 But I need it to also trigger when selecting a different payment gateway.但我还需要在选择不同的支付网关时触发它。

Here is my current code:这是我当前的代码:

<script>
    $('#payment_method_bacs').on('click', function() {
    $( 'body' ).trigger( 'update_checkout' );})
</script>

Any ideas?有任何想法吗?

You can follow this to update cart via AJAX.您可以按照此通过 AJAX 更新购物车。 Here is the link这是链接

jQuery(function( $ ) {
    $( "form.checkout" ).on( "click", "input.qty", function( e ) {//modify this to payment gateway radio button selection
      var data = {
      action: 'update_order_review',
      security: wc_checkout_params.update_order_review_nonce,
      post_data: $( 'form.checkout' ).serialize()
    };

    jQuery.post( add_quantity.ajax_url, data, function( response )
    {
      $( 'body' ).trigger( 'update_checkout' );
    });
  });
});

Here in above code instead of quantity update, Just add your change event of payement Gateway radio button selection在上面的代码中,而不是数量更新,只需添加您的付款网关单选按钮选择的更改事件

Since this part (payment methods) of the checkout being updated in the background, you should bind the click to "body" such as:由于结账的这部分(支付方式)是在后台更新的,所以你应该将点击绑定到“body”,例如:

jQuery(document).ready(function(){
    jQuery('body').on('click', 'ul.payment_methods li', function(){
            console.log('payment method clicked');
    });
});

(Untested!) (未经测试!)

Just to make this question somewhat useful for anyone who reads here in the future.. here is the script that ended up working for me.只是为了让这个问题对将来在这里阅读的任何人都有用..这是最终为我工作的脚本。 I found the answer here from this helpful guide .我从这个有用的指南中找到了答案。

    jQuery('div.woocommerce').on('click', 'input.qty', function(){ 

        jQuery("[name='update_cart']").trigger("click");

    }); 

To avoid customer click so many time make ajax call so many time:为了避免客户点击这么多次,让 ajax 调用这么多次:

 jQuery( function( $ ) {
    let timeout;
    $('body').on('change', 'input.qty', function(){
        if ( timeout !== undefined ) {
            clearTimeout( timeout );
        }
        timeout = setTimeout(function() {
            $("[name='update_cart']").trigger("click");
        }, 800 );
    });
} );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM