【发布时间】:2018-01-01 15:27:23
【问题描述】:
希望在 woocommerce 中从购物车中删除产品时运行一些 jQuery。我认为这会做到这一点,但它没有发生。
有什么想法吗?
jQuery( document.body ).on( 'updated_wc_div', function(){
//magic
});
【问题讨论】:
标签: jquery wordpress woocommerce
希望在 woocommerce 中从购物车中删除产品时运行一些 jQuery。我认为这会做到这一点,但它没有发生。
有什么想法吗?
jQuery( document.body ).on( 'updated_wc_div', function(){
//magic
});
【问题讨论】:
标签: jquery wordpress woocommerce
购物车更新时也可以触发updated_cart_totals 事件。因此,您可以使用以下解决方法,因为在更新购物车时不会触发一个全局事件:
jQuery( document.body ).on( 'updated_wc_div', do_magic );
jQuery( document.body ).on( 'updated_cart_totals', do_magic );
function do_magic() {
// do magic
}
【讨论】:
您可以使用此代码从购物车中删除产品:
main.js
$.ajax({
type: "POST",
url: 'http://localhost/your_site/wp-admin/admin-ajax.php',
data: {action : 'remove_item_from_cart','product_id' : '4'},
success: function (res) {
if (res) {
alert('Removed Successfully');
}
}
});
functions.php
function remove_item_from_cart() {
$cart = WC()->instance()->cart;
$id = $_POST['product_id'];
$cart_id = $cart->generate_cart_id($id);
$cart_item_id = $cart->find_product_in_cart($cart_id);
if($cart_item_id){
$cart->set_quantity($cart_item_id, 0);
return true;
}
return false;
}
add_action('wp_ajax_remove_item_from_cart', 'remove_item_from_cart');
add_action('wp_ajax_nopriv_remove_item_from_cart', 'remove_item_from_cart');
【讨论】:
在你的js中给出这组代码 文件
jQuery(document.body)
.on(
'removed_from_cart updated_cart_totals',
function() {
// your code...
}
);
【讨论】: