小编Bre*_*oks的帖子

自动添加或删除 Woocommerce 购物车中的免费产品

我正在尝试创建代码,一旦客户达到购物车中的特定价格点,该代码就会自动将商品添加到客户的购物车中。如果他们只订购虚拟产品,我试图排除这种情况的发生,因为“免费礼物”仅适用于正在发货的产品。我使用的代码是以正确的金额添加免费礼物,但并不排除任何虚拟产品。谁能找出我做错了什么吗?

\n

这是代码:

\n
/**\n * Add another product depending on the cart total\n */\n\nadd_action( 'template_redirect', 'add_product_to_cart' );\nfunction add_product_to_cart() {\n  if ( ! is_admin() ) {\n        global $woocommerce;\n        $product_id = 85942; //replace with your product id\n        $found = false;\n        $cart_total = 15; //replace with your cart total needed to add above item\n\n        if( $woocommerce->cart->total >= $cart_total ) {\n            //check if product already in cart\n            if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {\n\n                $isVirtualOnly = false;\n                foreach ( $woocommerce->cart->get_cart() as …
Run Code Online (Sandbox Code Playgroud)

php wordpress product cart woocommerce

4
推荐指数
1
解决办法
4467
查看次数

标签 统计

cart ×1

php ×1

product ×1

woocommerce ×1

wordpress ×1