简体   繁体   English

WooCommerce购物车页面需要实时更新,而无需“更新购物车”按钮

[英]WooCommerce Cart page needs to update live without the Update Cart button

The WooCommerce shop that I am working on needs to have its Cart page update after the quantity is changed. 数量更改后,我正在处理的WooCommerce商店需要更新其“购物车”页面。 Also, it can't have an Update Cart button as the one present by default. 另外,默认情况下,它不能具有“更新购物车”按钮。 Any ideas on how do it, possibly with AJAX or any other clues? 关于如何执行此操作的任何想法,可能与AJAX或其他任何线索有关?

Any help would be fantastic! 任何帮助都太棒了! Currently our page's cart is similar to http://www.shoopclothing.com and it should be like http://www.shoescribe.com/ for example. 目前,我们的页面的车是类似于http://www.shoopclothing.com ,它应该像http://www.shoescribe.com/例如。

What I've tried so far: 到目前为止,我已经尝试过:

  • tried searching for the Update cart method itself but unable to find what exactly was changing the Cart subtotals. 尝试搜索“更新购物车”方法本身,但找不到确切更改“购物车”小计的内容。

  • also tried using AJAX to print out the page again without reloading it. 还尝试使用AJAX重新打印页面而不重新加载页面。 That just reloaded the full page but the quantity was still the same 刚刚重新加载了整个页面,但数量仍然相同

This is the script we've tried: 这是我们尝试过的脚本:

 $(".product-order").on("click", "span.add", function () {
        var form = $(this).closest('form').serialize();
        $.ajax({
          type: "POST",
          url: "update_cart url",
          data: form
        }).done(function( msg ) {
            console.log(msg);
            alert("Data Saved: " + msg);
        });
  });

Hey if it's not too late, here is a quick solution that will update your cart without the user having to click the "Update Cart" button. 嘿,如果还不算太晚,这是一个快速的解决方案,它将更新您的购物车,而无需用户单击“更新购物车”按钮。 It's not AJAX but it's a simple way to get it done. 它不是AJAX,而是完成它的一种简单方法。

Basically, if the user changes the quantity of any item, the cart will update once the user has exited the product-quantity td. 基本上,如果用户更改了任何物品的数量,则一旦用户退出产品数量td,购物车就会更新。 It uses the .trigger() jQuery event handler to submit the form automatically. 它使用.trigger()jQuery事件处理程序自动提交表单。

Just use this jQuery code: 只需使用以下jQuery代码:

jQuery(document).ready(function() {
    var itemQtyInitial;

    jQuery('.woocommerce table.cart tr.cart_item .product-quantity').hover(function() {
        itemQtyInitial = jQuery('.qty', this).val();
    }, function() {
        var itemQtyExit = jQuery('.qty', this).val();

        if(itemQtyInitial != itemQtyExit) {
            jQuery(".button[name='update_cart']").trigger("click");
        }
    });

});

Then you can hide the "Update Cart" button with this CSS: 然后,您可以使用以下CSS隐藏“更新购物车”按钮:

.woocommerce table.cart td.actions input[name="update_cart"] {
    display: none;
}

That should work! 那应该工作!

Thanks to Niko. 感谢Niko。 I made little change to his code snippet. 我对他的代码段几乎没有改变。 It now should works well even with ajax of woocommerce. 即使使用woocommerce的ajax,它现在也应该可以很好地工作。 And the cart will be updated automatically after user click "+" or "-". 用户单击“ +”或“-”后,购物车将自动更新。

$('body').on('click','table.cart .quantity',function(){
    var itemQtyInitial;

    jQuery('.woocommerce table.cart tr.cart_item .product-quantity').hover(function() {
        itemQtyInitial = jQuery('.qty', this).val();
        console.log("inital",itemQtyInitial);
    }, function() {
        var itemQtyExit = jQuery('.qty', this).val();
        console.log("change",itemQtyExit);
        if(itemQtyInitial != itemQtyExit) {
            jQuery(".button[name='update_cart']").trigger("click");
        }
    });
});

您的问题是您发送SERIALIZED数据以更新购物车功能,因此它无法识别发布数据,因此您必须将所有输入字段都放入jquery发布中

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

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