简体   繁体   English

在 WooCommerce 中单击时自动更新购物车

[英]Auto update cart on click in WooCommerce

I want to auto update the cart when quantity is changed.我想在数量改变时自动更新购物车。 I got this working code in functions.php , but it's only working for the first click.我在functions.php得到了这个工作代码,但它只适用于第一次点击。 How to adjust it so it's working for every click?如何调整它以使其适用于每次点击?

add_action( 'wp_footer', 'cart_update_qty_script' ); 
function cart_update_qty_script() { 
    if (is_cart()) : 
    ?> 
    <script> 
        jQuery('div.woocommerce').on('click', '.quantity .button', function(){ 
            jQuery("[name='update_cart']").trigger("click"); }); 
    </script> 
    <?php 
    endif; 
}

try it like this..像这样试试..

add_action( 'wp_footer', 'cart_update_qty_script' ); 
function cart_update_qty_script() { 
    if (is_cart()) : 
    ?> 
    <script>
    jQuery( 'div.woocommerce' ).on( 'change', '.qty', function () {
    jQuery( "[name='update_cart']" ).trigger( "click" );
    } );
    </script>
    <?php 
    endif; 
}

I it's because the html is being replaced, div.woocommerce click event is no longer there... if you attached it to body, it might work...我是因为 html 被替换了,div.woocommerce click 事件不再存在......如果你将它附加到 body,它可能会工作......

The reason that is happening is because your dom is refreshed with the Ajax and events are flushed.发生这种情况的原因是您的 dom 已使用 Ajax 刷新并且事件已刷新。

What you need to do is listen to the event 'updated_cart_totals' which will tell you that dom is updated and after that reactivate your listeners.您需要做的是侦听事件“updated_cart_totals”,它会告诉您 dom 已更新,然后重新激活您的侦听器。

function quantity_upd() {
  $(".woocommerce-cart-form").on("change", ".qty", function() {
    $("[name='update_cart']").removeAttr('disabled');
    $("[name='update_cart']").trigger("click");
  });
}

$( document ).on( 'updated_cart_totals', function() {
  quantity_upd();
}

Please adjust it for your theme and HTML, it can vary请根据您的主题和 HTML 进行调整,它可能会有所不同

You can use plugin like below.您可以使用如下插件。

https://wordpress.org/plugins/woo-update-cart-on-quantity-change/ https://wordpress.org/plugins/woo-update-cart-on-quantity-change/

Also this plugin is working for you.这个插件也适合你。

https://wordpress.org/plugins/woocommerce-ajax-cart/ https://wordpress.org/plugins/woocommerce-ajax-cart/

Please use plugin for your safe side because plugin works in every wordpress version and woocommerce version.请使用插件以确保安全,因为插件适用于每个 wordpress 版本和 woocommerce 版本。

OR

You can try custom code with minor modifications like below.您可以尝试像下面这样稍作修改的自定义代码。

add_action( 'wp_footer', 'cart_update_qty_script' );
function cart_update_qty_script() {
    if (is_cart()) :
        ?>
        <script type="text/javascript">
            jQuery(document).on("click", "div.woocommerce .quantity .button", function(e) {
               jQuery("[name='update_cart']").trigger("click"); 
            });

OR  
            jQuery("body").on("click", "div.woocommerce .quantity .button", function(e) {
                jQuery("[name='update_cart']").trigger("click"); 
            });
        </script>
        <?php
    endif;
}

As I don't know your HTML structure so I built a sample jQuery which is working on storefront theme.因为我不知道你的 HTML 结构,所以我构建了一个示例 jQuery,它正在处理店面主题。

Here is a sample code.这是一个示例代码。

add_action( 'wp_footer', 'cart_update_qty_script' );
function cart_update_qty_script() {
    if (is_cart()) :
    ?>
    <script>
        jQuery('div.woocommerce').on('keyup', '.quantity .qty', function(){
            //console.log('clicked');
            jQuery("[name='update_cart']").trigger("click"); });
    </script>
    <?php
    endif;
}

Rather than try to trigger the click, how about removing the "disabled" attribute from the Update Cart button so the user can click it.与其尝试触发点击,不如从“更新购物车”按钮中删除“禁用”属性,以便用户可以点击它。 The cart page already works this way where when the quantity is changed on an item, the Update Cart button becomes clickable.购物车页面已经以这种方式工作,当商品的数量发生变化时,“更新购物车”按钮变得可点击。

add_action( 'wp_footer', 'cart_update_qty_script' ); 
function cart_update_qty_script() { 
    if (is_cart()) : 
    ?> 
    <script> 
        jQuery('body').on('click', 'div.woocommerce .quantity .button', function(){ 
            jQuery("[name='update_cart']").prop("disabled", false);
        }); 
    </script> 
    <?php 
    endif; 
}

The update button is actually disabled when the page loads, so you need basically to enable it, right before triggering the click event.页面加载时更新按钮实际上是禁用的,因此您基本上需要在触发点击事件之前启用它。

Use this code:使用此代码:

add_action( 'wp_footer', 'cart_update_qty_script' );
function cart_update_qty_script() {
  if (is_cart()) :
   ?>
    <script>
        jQuery('div.woocommerce').on('change', '.qty', function(){
           jQuery("[name='update_cart']").removeAttr('disabled');
           jQuery("[name='update_cart']").trigger("click"); 
        });
   </script>
<?php
endif;
}

Copy this code to functions:将此代码复制到函数:

function bbloomer_cart_refresh_update_qty() { 
   if (is_cart()) { 
      ?>      
      <script>
        jQuery('div.woocommerce').on('change', '.qty', function(){
            jQuery("[name='update_cart']").prop("disabled", false);
            jQuery("[name='update_cart']").trigger("click");
        });
        </script>
      <?php 
   } 
}

End this to style:结束这个风格:

button[name='update_cart'] {
display: none !important;
}

The reason it only works on the first click is because everytime you update the form, it refreshes itself.它只适用于第一次点击的原因是因为每次更新表单时,它都会自行刷新。 So instead of doing:所以,而不是做:

jQuery('div.woocommerce').on('click', '.quantity .button', function() {

You need to switch div.woocommerce to document :您需要将div.woocommerce切换到document

jQuery('document').on('click', '.quantity .button', function() {

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

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