WooCommerce 检查购物车是否有优惠券
WooCommerce Check cart for coupons
我正在尝试检查购物车,看看它是否包含任何特定的优惠券等,但它总是 returns 错误。
add_action( 'woocommerce_after_checkout_billing_form', 'display_extra_fields_after_billing_address' , 10, 1 );
function display_extra_fields_after_billing_address () {
// Check if a pre-order product is in the cart
$preorder_in_cart = false;
$coupon_codes = array('vip15', 'vip20');
// Loop through all products in the Cart
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_id = $cart_item['product_id'];
$product = wc_get_product( $product_id );
if( 'yes' === $product->get_meta( '_ywpo_preorder' ) ) {
$preorder_in_cart = true;
// break;
}
// Checks if the coupons are in the cart
if(in_array($coupon_codes, WC()->cart->get_applied_coupons() )){
echo 'found';
} else {
echo 'not found';
}
}
}
不过,如果我检查一张优惠券就可以了。
$coupon_codes = 'vip15';
WC_Cart::get_applied_coupons()
returns 一组应用于您的购物车的优惠券。
为了检查多张优惠券,变量$coupon_codes
应该是一个数组。然后您可以遍历 $coupon_codes
并检查每个优惠券代码。
in_array()
的第一个参数必须是字符串。
add_action( 'woocommerce_after_checkout_billing_form', 'display_extra_fields_after_billing_address' , 10, 1 );
function display_extra_fields_after_billing_address () {
// Check if a pre-order product is in the cart
$preorder_in_cart = false;
$coupon_codes = array('vip15', 'vip20'); // or $coupon_codes = array('vip15');
foreach($coupon_codes as $coupon_code) {
// Checks if the coupons are in the cart
if(in_array($coupon_code, WC()->cart->get_applied_coupons() )){
echo 'found';
} else {
echo 'not found';
}
}
}
我正在尝试检查购物车,看看它是否包含任何特定的优惠券等,但它总是 returns 错误。
add_action( 'woocommerce_after_checkout_billing_form', 'display_extra_fields_after_billing_address' , 10, 1 );
function display_extra_fields_after_billing_address () {
// Check if a pre-order product is in the cart
$preorder_in_cart = false;
$coupon_codes = array('vip15', 'vip20');
// Loop through all products in the Cart
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_id = $cart_item['product_id'];
$product = wc_get_product( $product_id );
if( 'yes' === $product->get_meta( '_ywpo_preorder' ) ) {
$preorder_in_cart = true;
// break;
}
// Checks if the coupons are in the cart
if(in_array($coupon_codes, WC()->cart->get_applied_coupons() )){
echo 'found';
} else {
echo 'not found';
}
}
}
不过,如果我检查一张优惠券就可以了。
$coupon_codes = 'vip15';
WC_Cart::get_applied_coupons()
returns 一组应用于您的购物车的优惠券。
为了检查多张优惠券,变量$coupon_codes
应该是一个数组。然后您可以遍历 $coupon_codes
并检查每个优惠券代码。
in_array()
的第一个参数必须是字符串。
add_action( 'woocommerce_after_checkout_billing_form', 'display_extra_fields_after_billing_address' , 10, 1 );
function display_extra_fields_after_billing_address () {
// Check if a pre-order product is in the cart
$preorder_in_cart = false;
$coupon_codes = array('vip15', 'vip20'); // or $coupon_codes = array('vip15');
foreach($coupon_codes as $coupon_code) {
// Checks if the coupons are in the cart
if(in_array($coupon_code, WC()->cart->get_applied_coupons() )){
echo 'found';
} else {
echo 'not found';
}
}
}