wordpress 在 WooCommerce AJAX 购物车更新后运行 jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39312360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:14:19  来源:igfitidea点击:

Run jQuery after WooCommerce AJAX cart update

wordpresswoocommerce

提问by Mando

WooCommerce:

WooCommerce:

I have some jQuery that loads on document.ready that effects the quantity input box. Works great.

我有一些 jQuery 加载在 document.ready 上,影响数量输入框。效果很好。

My issue is that when I update the cart/delete item, the jQuery needs to run again because the AJAX call kills it (refreshes it). Is there some hook/filter I can add that will re-run my script again after a cart update? Ive searched and can't find anything (or maybe I'm not aware of what I am looking for).

我的问题是,当我更新购物车/删除项目时,jQuery 需要再次运行,因为 AJAX 调用会杀死它(刷新它)。我可以添加一些钩子/过滤器,以便在购物车更新后再次运行我的脚本吗?我搜索过但找不到任何东西(或者我可能不知道我在找什么)。

Thank you!!!!

谢谢!!!!

回答by helgatheviking

The WooCommerce scripts have several custom eventsbuilt in. Your own script can listen to these events and run your own code when they are triggered. The most obvious one for your case might be updated_cart_totalsbut updated_wc_divmight also be helpful, I'm not sure without testing. Tested and works.

WooCommerce 脚本内置了多个自定义事件。您自己的脚本可以侦听这些事件并在它们被触发时运行您自己的代码。对于您的情况,最明显的可能是updated_cart_totalsupdated_wc_div也可能会有所帮助,我不确定没有测试。测试和工作。

$( document.body ).on( 'updated_cart_totals', function(){
    //re-do your jquery
});

回答by Yehuda

I also needed to run a function after user removes item from cart, and the above events updated_cart_totalsor updated_wc_divdidn't work for me.

我还需要在用户从购物车中删除项目后运行一个函数,并且上述事件updated_cart_totalsupdated_wc_div对我不起作用。

After digging into the Woocommercefrontend code

在深入研究Woocommerce前端代码之后

[/wp-content/plugins/woocommerce/assets/js/frontend/add-to-cart.min.js]

[/wp-content/plugins/woocommerce/assets/js/frontend/add-to-cart.min.js]

I found these events:

我发现了这些事件:

$(document.body).on('added_to_cart removed_from_cart', do_magic);

These worked like a charm!

这些工作就像一个魅力!

回答by Rus Skazkin

In my case I have to add the following:

就我而言,我必须添加以下内容:

jQuery(document.body).on('removed_from_cart updated_cart_totals', function () {
    location.reload();
});

Because I have a huge customization with js cart submit

因为我对 js 购物车提交进行了大量自定义