WooCommerce 结帐前的强制性优惠券(可选:针对特定产品)
Mandatory coupon before checkout in WooCommerce (optional: for specific products)
我想强制客户在结账前添加优惠券代码。我希望它适用于我的 WooCommerce 商店中的每个优惠券代码和每个产品。
我正在使用这个代码,它几乎可以解决问题,但它只适用于一个优惠券代码 (freev1
)
如何让它适用于生成的每个优惠券代码?
add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );
function mandatory_coupon_code() {
// HERE set your coupon code
$mandatory_coupon = 'freev1';
$applied_coupons = WC()->cart->get_applied_coupons();
// If coupon is found we exit
if( in_array( $mandatory_coupon, $applied_coupons ) ) return;
// Not found: display an error notice
wc_add_notice( __( 'Add coupon before checkout.', 'woocommerce' ), 'error' );
}
只检查$applied_coupons
是否为空,为空时添加提示。删除 $mandatory_coupon
& if ( in_array...
所以你得到:
function action_woocommerce_check_cart_items() {
// Isset
if ( WC()->cart ) {
// Get applied coupons
$applied_coupons = WC()->cart->get_applied_coupons();
// When empty
if ( empty ( $applied_coupons ) ) {
// Not found: display an error notice
wc_add_notice( __( 'Add coupon before checkout.', 'woocommerce' ), 'error' );
}
}
}
add_action( 'woocommerce_check_cart_items', 'action_woocommerce_check_cart_items', 10 );
更新:
要将此应用于购物车中的特定产品,请使用:
function action_woocommerce_check_cart_items() {
// The targeted product ids
$targeted_ids = array( 30, 815 );
// Flag
$found = false;
// Isset
if ( WC()->cart ) {
// Get applied coupons
$applied_coupons = WC()->cart->get_applied_coupons();
// When empty
if ( empty ( $applied_coupons ) ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$found = true;
break;
}
}
}
}
// True
if ( $found ) {
// Not found: display an error notice
wc_add_notice( __( 'Add coupon before checkout.', 'woocommerce' ), 'error' );
}
}
add_action( 'woocommerce_check_cart_items', 'action_woocommerce_check_cart_items', 10 );
补充问题:
"Will it be possible to use almost same code, but instead make it work from the checkout page and force the use of a coupon before
placing a order?"
You could replace woocommerce_check_cart_items
with the
woocommerce_checkout_process
hook for the checkout page
我想强制客户在结账前添加优惠券代码。我希望它适用于我的 WooCommerce 商店中的每个优惠券代码和每个产品。
我正在使用这个代码,它几乎可以解决问题,但它只适用于一个优惠券代码 (freev1
)
如何让它适用于生成的每个优惠券代码?
add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );
function mandatory_coupon_code() {
// HERE set your coupon code
$mandatory_coupon = 'freev1';
$applied_coupons = WC()->cart->get_applied_coupons();
// If coupon is found we exit
if( in_array( $mandatory_coupon, $applied_coupons ) ) return;
// Not found: display an error notice
wc_add_notice( __( 'Add coupon before checkout.', 'woocommerce' ), 'error' );
}
只检查$applied_coupons
是否为空,为空时添加提示。删除 $mandatory_coupon
& if ( in_array...
所以你得到:
function action_woocommerce_check_cart_items() {
// Isset
if ( WC()->cart ) {
// Get applied coupons
$applied_coupons = WC()->cart->get_applied_coupons();
// When empty
if ( empty ( $applied_coupons ) ) {
// Not found: display an error notice
wc_add_notice( __( 'Add coupon before checkout.', 'woocommerce' ), 'error' );
}
}
}
add_action( 'woocommerce_check_cart_items', 'action_woocommerce_check_cart_items', 10 );
更新:
要将此应用于购物车中的特定产品,请使用:
function action_woocommerce_check_cart_items() {
// The targeted product ids
$targeted_ids = array( 30, 815 );
// Flag
$found = false;
// Isset
if ( WC()->cart ) {
// Get applied coupons
$applied_coupons = WC()->cart->get_applied_coupons();
// When empty
if ( empty ( $applied_coupons ) ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$found = true;
break;
}
}
}
}
// True
if ( $found ) {
// Not found: display an error notice
wc_add_notice( __( 'Add coupon before checkout.', 'woocommerce' ), 'error' );
}
}
add_action( 'woocommerce_check_cart_items', 'action_woocommerce_check_cart_items', 10 );
补充问题:
"Will it be possible to use almost same code, but instead make it work from the checkout page and force the use of a coupon before placing a order?"
You could replace
woocommerce_check_cart_items
with thewoocommerce_checkout_process
hook for the checkout page