【问题标题】:Remove add to cart button if the product is in cart on Woocommerce [duplicate]如果产品在 Woocommerce 的购物车中,请删除添加到购物车按钮 [重复]
【发布时间】:2019-08-04 10:05:13
【问题描述】:

如何允许将特定产品添加到购物车一次?

或者如果产品已经在购物车中,有没有办法重定向到购物车页面

我在这里找到了解决方案,因为它不起作用

Disable Woocommerce add to cart button if the product is already in cart

如何为所有产品而不是单个产品做到这一点?

【问题讨论】:

  • 我在现有线程中添加了一种新的改进方式,当它已经在购物车中时禁用添加到购物车按钮
  • @LoicTheAztec 非常感谢

标签: php wordpress woocommerce product cart


【解决方案1】:

此功能将检查您的产品是否已在购物车中。如果是这样,它将重定向到购物车页面。如果您想直接重定向到结帐,请参阅代码中的注释。

只需将以下内容放在您的functions.php中。

经过测试并且有效。

function check_if_product_in_cart($valid, $product_id, $quantity) {
    global $woocommerce;
    if($woocommerce->cart->cart_contents_count > 0){
        foreach($woocommerce->cart->get_cart() as $key => $val ) {
            $_product = $val['data'];
            if($product_id == $_product->id ) {
                // $url = WC()->cart->get_checkout_url(); // Checkout
                $url = wc_get_cart_url();  // Cart
                wp_redirect($url);
                exit;
            }
        }
    }
    return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'check_if_product_in_cart',11,3);

【讨论】:

  • 这对我没有帮助。我想更改购物车中的商品数量不限
  • 我更新了答案
  • 非常感谢
  • 当产品已经在购物车中时,有什么方法可以更改文本?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-30
  • 2021-01-31
  • 2021-06-28
  • 2014-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多