【发布时间】:2017-06-06 14:03:40
【问题描述】:
我希望使用 woocommerce 智能优惠券创建“邀请码”。为此,我希望特定产品在购物车/结帐时需要优惠券代码。我已经看到了这样做的方法,但是它们会影响所有产品,而不仅仅是我想要的产品。
我正在尝试将返回的变量传递给另一个函数,但是我不知道该怎么做。
这是最新版本:
//Check to see if user has product with a specific product category in cart
function check_product_in_cart() {
global $woocommerce;
//assigns a default negative value
// categories targeted 87, 18, 19
$product_in_cart = false;
// start of the loop that fetches the cart items
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
// second level loop search, in case some items have several categories
foreach ($terms as $term) {
$_categoryid = $term->term_id;
if (( $_categoryid === 87 ) || ( $_categoryid === 18 ) || ( $_categoryid === 19 )) {
//category is in cart!
$product_in_cart = true;
}
}
}
return $product_in_cart;
}
// Force Coupon codes for Woocommerce
add_action('woocommerce_check_cart_items', 'force_coupon_code');
function force_coupon_code( $product_in_cart )
{
global $woocommerce;
if(is_cart() || is_checkout()){
$my_coupon = $woocommerce->cart->applied_coupons;
echo $woocommerce->cart->get_applied_coupons;
if(empty($my_coupon))
{
wc_add_notice( '<strong>' . $btn['label'] . '</strong> ' . __( 'A coupon code is mandatory for this product.', 'woocommerce' ), 'error' );
}
}
}
第一部分应该退回产品 $product_in_cart,第二部分强制使用优惠券。这是我正在使用的代码的来源。 Mandatory Coupon code 和 Checking products in cart based on category。我读到将变量放在新函数 args 中可能会实现这个like so,但它绝不工作。
【问题讨论】:
标签: php wordpress woocommerce categories coupon