WooCommerce 中赠品的强制兑换券
Mandatory redemption coupon for gifted items in WooCommerce
我正在使用目前接受单个(或一组)静态优惠券代码的以下功能。我如何更改它以使用网站中存在的任何有效(即尚未使用最长时间等)优惠券代码?
add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );
function mandatory_coupon_code() {
$product_categories = array( 'gifts' ); // Category ID or slug of targeted category
// NEED TO EDIT THIS PART TO GET THE LIST OF VALID CODES
$coupon_code = 'testing1'; // The required coupon code
$coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ){
if( has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) && ! $coupon_applied ) {
wc_clear_notices(); // Clear all other notices
// Avoid checkout displaying an error notice
wc_add_notice( sprintf( 'The product "%s" requires a valid redemption code for checkout.', $cart_item['data']->get_name() ), 'error' );
break; // stop the loop
}
}
}
编辑:
根据 this link 的评论和信息,我尝试了以下代码,但它不起作用:
add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );
function mandatory_coupon_code() {
$product_categories = array( 'gifts' ); // Category ID or slug of targeted category
$coupon_code = array();
foreach ( $coupons as $coupon ) {
$coupon_name = $coupon->post_title;
array_push( $coupon_code, $coupon_name );
}
$coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ){
if( has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) && ! $coupon_applied ) {
wc_clear_notices(); // Clear all other notices
// Avoid checkout displaying an error notice
wc_add_notice( sprintf( 'The product "%s" requires a valid redemption code for checkout.', $cart_item['data']->get_name() ), 'error' );
break; // stop the loop
}
}
}
由于 wooCommerce 在应用优惠券时已经检查了优惠券的有效性,如果它们无效,它会根据无效原因显示不同的警告消息,删除无效应用的优惠券...
所以你不需要在你的代码中获取所有有效的优惠券......你只需要以下内容:
add_action( 'woocommerce_check_cart_items', 'mandatory_redemption_coupon_code_for_gifted_items' );
function mandatory_redemption_coupon_code_for_gifted_items() {
$product_categories = array( 'gifts' ); // Term ID, slug or name of targeted category
$applied_coupons = WC()->cart->get_applied_coupons();
// When no coupon has been applied
if( empty($applied_coupons) ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $item ){
// Check for gifted items
if( has_term( $product_categories, 'product_cat', $item['product_id'] ) ) {
wc_clear_notices(); // Clear all other notices
// Avoid checkout displaying an error notice
wc_add_notice( sprintf( 'The product "%s" requires a valid redemption code for checkout.', $item['data']->get_name() ), 'error' );
break; // stop the loop
}
}
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
Now if the required coupons needs to be specific, you will have make a custom SQL query to check if any applied coupon is valid for redemption.
注意:在您的编辑中,缺少优惠券查询,因此它不起作用。
我正在使用目前接受单个(或一组)静态优惠券代码的以下功能。我如何更改它以使用网站中存在的任何有效(即尚未使用最长时间等)优惠券代码?
add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );
function mandatory_coupon_code() {
$product_categories = array( 'gifts' ); // Category ID or slug of targeted category
// NEED TO EDIT THIS PART TO GET THE LIST OF VALID CODES
$coupon_code = 'testing1'; // The required coupon code
$coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ){
if( has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) && ! $coupon_applied ) {
wc_clear_notices(); // Clear all other notices
// Avoid checkout displaying an error notice
wc_add_notice( sprintf( 'The product "%s" requires a valid redemption code for checkout.', $cart_item['data']->get_name() ), 'error' );
break; // stop the loop
}
}
}
编辑:
根据 this link 的评论和信息,我尝试了以下代码,但它不起作用:
add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );
function mandatory_coupon_code() {
$product_categories = array( 'gifts' ); // Category ID or slug of targeted category
$coupon_code = array();
foreach ( $coupons as $coupon ) {
$coupon_name = $coupon->post_title;
array_push( $coupon_code, $coupon_name );
}
$coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ){
if( has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) && ! $coupon_applied ) {
wc_clear_notices(); // Clear all other notices
// Avoid checkout displaying an error notice
wc_add_notice( sprintf( 'The product "%s" requires a valid redemption code for checkout.', $cart_item['data']->get_name() ), 'error' );
break; // stop the loop
}
}
}
由于 wooCommerce 在应用优惠券时已经检查了优惠券的有效性,如果它们无效,它会根据无效原因显示不同的警告消息,删除无效应用的优惠券...
所以你不需要在你的代码中获取所有有效的优惠券......你只需要以下内容:
add_action( 'woocommerce_check_cart_items', 'mandatory_redemption_coupon_code_for_gifted_items' );
function mandatory_redemption_coupon_code_for_gifted_items() {
$product_categories = array( 'gifts' ); // Term ID, slug or name of targeted category
$applied_coupons = WC()->cart->get_applied_coupons();
// When no coupon has been applied
if( empty($applied_coupons) ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $item ){
// Check for gifted items
if( has_term( $product_categories, 'product_cat', $item['product_id'] ) ) {
wc_clear_notices(); // Clear all other notices
// Avoid checkout displaying an error notice
wc_add_notice( sprintf( 'The product "%s" requires a valid redemption code for checkout.', $item['data']->get_name() ), 'error' );
break; // stop the loop
}
}
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
Now if the required coupons needs to be specific, you will have make a custom SQL query to check if any applied coupon is valid for redemption.
注意:在您的编辑中,缺少优惠券查询,因此它不起作用。