【问题标题】:Woocommerce trigger added_to_cart and get product IDWoocommerce 触发器 added_to_cart 并获取产品 ID
【发布时间】:2020-04-17 03:56:14
【问题描述】:

在 Woocommerce 上,使用 jQuery,有一种方法可以在触发“add_to_cart”时使用 ajax 检索添加到购物车的产品 ID?

例如

$( document.body ).on( 'added_to_cart', function() { 
    // print the product id added on console
});

谢谢

【问题讨论】:

标签: jquery wordpress woocommerce


【解决方案1】:

这只是一个 hack

我也无法获取它,但我认为您可以使用一些参数 (event, fragments, cart_hash, $button) 来捕捉该事件。第四个非常有趣,因为它返回您刚刚按下的按钮的 DOMElement。

在您的主题中,我建议您为 ADD_TO_CART 按钮添加一个属性 例如:<button data-product_id="1234"> ... </button>

那么你的 .js 文件最终会是这样的:

$(document.body).on("added_to_cart", function (event, fragments, cart_hash, $button) {
    console.log($($button[0]).data('product_id'));
});

【讨论】:

    猜你喜欢
    • 2016-03-23
    • 2021-01-15
    • 1970-01-01
    • 2014-07-04
    • 2016-01-09
    • 1970-01-01
    • 2015-05-27
    相关资源
    最近更新 更多