【问题标题】:WooCommerce reset quantity before adding to cart (to prevent addition)WooCommerce 在添加到购物车之前重置数量(以防止添加)
【发布时间】:2020-06-07 01:03:27
【问题描述】:

当我将产品数量3 添加到购物车时,一切正常。

但是在将5 更多数量添加到购物车后,我希望购物车中的数量为5 而不是8

我希望最后发送的数量覆盖之前的数量,而不是添加。

另外,我希望它只在特定产品 ID 上表现得像这样。

哪个钩子适合这个?

【问题讨论】:

  • 你好!您能否提供一个您遇到问题的代码示例?
  • 没有代码,该功能内置于 WooCommerce。只需尝试将库存产品添加到购物车,您就会看到它在数量之上。
  • 嘿@7uc1f3r,我已经阅读了这篇文章,并将确保在未来提出更好解释的问题,解释我尝试过的和没有奏效的。但是,我看到您正确地回答了我的问题,并且给出了完美的答案。所以谢谢你!

标签: php wordpress woocommerce product cart


【解决方案1】:

您可以使用woocommerce_add_to_cart 挂钩

在此示例中,使用产品 ID 30

function action_woocommerce_add_to_cart ( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
    // Get current product id
    $product_id = $variation_id > 0 ? $variation_id : $product_id;

    // Specific product ID, compare
    if ( $product_id == 30 ) {
        // Set quantity
        WC()->cart->set_quantity( $cart_item_key, $quantity );      
    }
}
add_action( 'woocommerce_add_to_cart', 'action_woocommerce_add_to_cart', 10, 6 );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 2020-11-19
    • 2018-03-31
    • 1970-01-01
    • 2015-10-10
    • 2015-08-18
    • 2014-02-25
    相关资源
    最近更新 更多