有条件地为特定产品 ID 和数量自动应用优惠券
Conditionally apply coupons automatically for specific Product IDs and quantities
我正在尝试根据产品 ID 和数量条件在我的 WooCommerce 商店中自动应用优惠券。我的最终目标是在将两 (2) 种所需产品添加到购物车时自动应用特定优惠券,并在将三 (3) 种所需产品添加到购物车时自动应用另一张优惠券。单个数量的产品不应有折扣。 以下是代码的更正版本,现在有效:
add_action( 'woocommerce_before_cart', 'conditional_auto_add_coupons' );
function conditional_auto_add_coupons() {
if ( !WC()->cart->is_empty() ){
// Define HERE your Targeted Product ID and coupons codes
$target_pid = 103;
$coupon1 = 'soccer-sibling-2';
$coupon2 = 'soccer-sibling-3';
// First cart loop: Counting number of subactegory items in cart
foreach ( WC()->cart->get_cart() as $cart_item ){
if( $target_pid == $cart_item['data']->id ){
// Removes any coupons in the cart already
WC()->cart->remove_coupons();
if( 2 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon1 ) ){
WC()->cart->remove_coupons();
WC()->cart->add_discount( $coupon1 );
wc_add_notice( __( 'The multiple sibling discount has been applied.', 'theme_domain' ), 'success' );
} elseif( 3 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon2 ) ){
WC()->cart->remove_coupons();
WC()->cart->add_discount( $coupon2 );
wc_add_notice( __( 'The multiple sibling discount has been applied.', 'theme_domain' ), 'success' );
}
// Recalculates Cart Totals to show correct price
WC()->cart->calculate_totals();
}
}
}
}
您的代码中有很多错误,而且有点过时了...
我已经重写了你函数中的所有内容并将其挂接到另一个钩子中。
这是您重新访问的代码:
add_action( 'woocommerce_before_cart', 'conditional_auto_add_coupons' );
function conditional_auto_add_coupons() {
if ( !WC()->cart->is_empty() ){
// Define HERE your Targeted Product ID and coupons codes
$target_pid = 103;
$coupon1 = 'soccer-sibling-2';
$coupon2 = 'soccer-sibling-3';
// First cart loop: Counting number of subactegory items in cart
foreach ( WC()->cart->get_cart() as $cart_item ){
if( $target_pid == $cart_item['data']->id ){
if( 2 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon1 ) ){
WC()->cart->add_discount( $coupon1 );
wc_add_notice( __( 'A quantity discount of <strong>5%</strong> has been added.', 'theme_domain' ), 'success' );
} elseif( 3 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon2 ) ){
WC()->cart->add_discount( $coupon2 );
wc_add_notice( __( 'A quantity discount of <strong>10%</strong> has been added.', 'theme_domain' ), 'success' );
}
}
}
}
}
此代码位于您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。
这应该有效,不会让您的网站崩溃,但我还没有真正测试过,因为这是非常特殊的。
几年后回到这里:我发现了一个导致问题的漏洞。因为该功能计算的是购物车的总内容,而不仅仅是优惠券适用的产品数量,客户可以利用它添加 non-coupon 相关产品以获得打折产品的额外优惠券。以下代码通过仅检查相关产品的数量来弥补漏洞:
add_action( 'woocommerce_before_cart', 'conditional_auto_add_coupons_tourney' );
function conditional_auto_add_coupons_tourney() {
if ( !WC()->cart->is_empty() ){
// Define HERE your Targeted Product ID and coupons codes
$target_pid = 96;
$coupon1 = 'multiple-25';
$coupon2 = 'multiple-50';
// UPDATE: switched WC()->cart->get_cart_contents_count() for $quantity on 236 and 240
// First cart loop: Counting number of subcategory items in cart
foreach ( WC()->cart->get_cart() as $cart_item ){
if( $target_pid == $cart_item['product_id'] ){
WC()->cart->remove_coupons();
$quantity = $cart_item['quantity']; // Added to find quantity of specific product
if( 5 <= $quantity && !WC()->cart->has_discount( $coupon2 ) ){
WC()->cart->remove_coupons();
WC()->cart->add_discount( $coupon2 );
wc_add_notice( __( 'The multiple team discount has been applied.', 'theme_domain' ), 'success' );
} elseif( 2 <= $quantity && !WC()->cart->has_discount( $coupon1 ) ){
WC()->cart->remove_coupons();
WC()->cart->add_discount( $coupon1 );
wc_add_notice( __( 'The multiple team discount has been applied.', 'theme_domain' ), 'success' );
}
WC()->cart->calculate_totals();
}
}
}
}
我正在尝试根据产品 ID 和数量条件在我的 WooCommerce 商店中自动应用优惠券。我的最终目标是在将两 (2) 种所需产品添加到购物车时自动应用特定优惠券,并在将三 (3) 种所需产品添加到购物车时自动应用另一张优惠券。单个数量的产品不应有折扣。 以下是代码的更正版本,现在有效:
add_action( 'woocommerce_before_cart', 'conditional_auto_add_coupons' );
function conditional_auto_add_coupons() {
if ( !WC()->cart->is_empty() ){
// Define HERE your Targeted Product ID and coupons codes
$target_pid = 103;
$coupon1 = 'soccer-sibling-2';
$coupon2 = 'soccer-sibling-3';
// First cart loop: Counting number of subactegory items in cart
foreach ( WC()->cart->get_cart() as $cart_item ){
if( $target_pid == $cart_item['data']->id ){
// Removes any coupons in the cart already
WC()->cart->remove_coupons();
if( 2 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon1 ) ){
WC()->cart->remove_coupons();
WC()->cart->add_discount( $coupon1 );
wc_add_notice( __( 'The multiple sibling discount has been applied.', 'theme_domain' ), 'success' );
} elseif( 3 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon2 ) ){
WC()->cart->remove_coupons();
WC()->cart->add_discount( $coupon2 );
wc_add_notice( __( 'The multiple sibling discount has been applied.', 'theme_domain' ), 'success' );
}
// Recalculates Cart Totals to show correct price
WC()->cart->calculate_totals();
}
}
}
}
您的代码中有很多错误,而且有点过时了... 我已经重写了你函数中的所有内容并将其挂接到另一个钩子中。
这是您重新访问的代码:
add_action( 'woocommerce_before_cart', 'conditional_auto_add_coupons' );
function conditional_auto_add_coupons() {
if ( !WC()->cart->is_empty() ){
// Define HERE your Targeted Product ID and coupons codes
$target_pid = 103;
$coupon1 = 'soccer-sibling-2';
$coupon2 = 'soccer-sibling-3';
// First cart loop: Counting number of subactegory items in cart
foreach ( WC()->cart->get_cart() as $cart_item ){
if( $target_pid == $cart_item['data']->id ){
if( 2 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon1 ) ){
WC()->cart->add_discount( $coupon1 );
wc_add_notice( __( 'A quantity discount of <strong>5%</strong> has been added.', 'theme_domain' ), 'success' );
} elseif( 3 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon2 ) ){
WC()->cart->add_discount( $coupon2 );
wc_add_notice( __( 'A quantity discount of <strong>10%</strong> has been added.', 'theme_domain' ), 'success' );
}
}
}
}
}
此代码位于您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。
这应该有效,不会让您的网站崩溃,但我还没有真正测试过,因为这是非常特殊的。
几年后回到这里:我发现了一个导致问题的漏洞。因为该功能计算的是购物车的总内容,而不仅仅是优惠券适用的产品数量,客户可以利用它添加 non-coupon 相关产品以获得打折产品的额外优惠券。以下代码通过仅检查相关产品的数量来弥补漏洞:
add_action( 'woocommerce_before_cart', 'conditional_auto_add_coupons_tourney' );
function conditional_auto_add_coupons_tourney() {
if ( !WC()->cart->is_empty() ){
// Define HERE your Targeted Product ID and coupons codes
$target_pid = 96;
$coupon1 = 'multiple-25';
$coupon2 = 'multiple-50';
// UPDATE: switched WC()->cart->get_cart_contents_count() for $quantity on 236 and 240
// First cart loop: Counting number of subcategory items in cart
foreach ( WC()->cart->get_cart() as $cart_item ){
if( $target_pid == $cart_item['product_id'] ){
WC()->cart->remove_coupons();
$quantity = $cart_item['quantity']; // Added to find quantity of specific product
if( 5 <= $quantity && !WC()->cart->has_discount( $coupon2 ) ){
WC()->cart->remove_coupons();
WC()->cart->add_discount( $coupon2 );
wc_add_notice( __( 'The multiple team discount has been applied.', 'theme_domain' ), 'success' );
} elseif( 2 <= $quantity && !WC()->cart->has_discount( $coupon1 ) ){
WC()->cart->remove_coupons();
WC()->cart->add_discount( $coupon1 );
wc_add_notice( __( 'The multiple team discount has been applied.', 'theme_domain' ), 'success' );
}
WC()->cart->calculate_totals();
}
}
}
}