IN WooCommerce I would like to add 10% of discount with WooCommerce Coupon feature, only when customer purchases products from 3 different product categories (like Category1, Category2, Category3).
在WooCommerce我想用WooCommerce优惠券功能增加10%的折扣,只有当客户从3个不同的产品类别(如Category1,Category2,Category3)购买产品时。
How this can be done with WooCommerce Coupon feature?
如何使用WooCommerce优惠券功能完成此操作?
Any help on this will be appreciated.
任何有关这方面的帮助将不胜感激。
Update note: I only 3 parent product categories with no sub categories. Each product is assigned to one category. Some products are variable and some others are simple.
更新说明:我只有3个没有子类别的父产品类别。每个产品都分配到一个类别。有些产品是可变的,有些则很简单。
2 个解决方案
#1
1
To handle this functionality with a coupon you will need to have one product category by product or one parent category by product, because a product can have many categories and subcategories set for it.
要使用优惠券处理此功能,您需要按产品分类一个产品类别或按产品分类一个父类别,因为产品可以为其设置许多类别和子类别。
This custom function will add a coupon discount when cart items are from 3 different product categories. If cart items are removed from cart and there is not anymore 3 different product categories, the coupon code will be auto removed.
当购物车商品来自3个不同的产品类别时,此自定义功能将添加优惠券折扣。如果购物车商品已从购物车中移除,并且不再有3种不同的商品类别,优惠券代码将自动删除。
Also you will need to set in the function the coupon code name and an array of all your matching product categories IDs.
此外,您还需要在功能中设置优惠券代码名称和所有匹配产品类别ID的数组。
Here is the code:
这是代码:
add_action( 'woocommerce_before_calculate_totals', 'add_discount_for_3_diff_cats', 10, 1 );
function add_discount_for_3_diff_cats( $wc_cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE set your coupon code and your parent product categories in the array
$coupon_code_to_apply = 'summer';
// HERE define your product categories IDs in the array
$your_categories = array( 11, 13, 14 ); // IDs
// If coupon is already set
if( $wc_cart->has_discount( $coupon_code_to_apply ) )
$has_coupon = true;
foreach( $wc_cart->get_cart() as $cart_item ) {
$product_id = $cart_item['product_id'];
$product = wc_get_product($product_id);
foreach( $product->get_category_ids() as $category_id ) {
if( has_term( $your_categories, 'product_cat', $product_id ) && in_array( $category_id, $your_categories ) ){
// Set the categories in an array (avoiding duplicates)
$categories[$category_id] = $category_id;
}
}
}
$count_cats = count($categories);
$has_discount = $wc_cart->has_discount( $coupon_code_to_apply );
if ( 3 <= $count_cats && ! $has_discount ) {
$wc_cart->add_discount($coupon_code_to_apply);
} elseif ( 3 > $count_cats && $has_discount ) {
$wc_cart->remove_coupon($coupon_code_to_apply);
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
Tested and works with simple and variable products…
测试并使用简单和可变的产品......
#2
1
Here's a solution that is not using coupon codes that I recycled from this previous question.
这是一个解决方案,不使用我从上一个问题回收的优惠券代码。
add_action( 'woocommerce_cart_calculate_fees' , 'add_multiple_category_discount' );
function add_multiple_category_discount( $cart ){
if( $cart->cart_contents_count < 3 ){
return;
}
$product_cats = array();
foreach( $cart->get_cart() as $item ) {
$product = wc_get_product( $item['product_id'] );
foreach( $product->get_category_ids() as $key => $cat_id ) {
if( ! in_array( $cat_id, $product_cats ) )
$product_cats[] = $cat_id;
}
}
// If we have 3 distinct categories then apply a discount
if( count( $product_cats ) >= 3 ) {
// Add a 10% discount
$discount = $cart->subtotal * 0.1;
$cart->add_fee( 'You have 3 different product categories in your cart, a 10% discount has been added.', -$discount );
}
}
#1
1
To handle this functionality with a coupon you will need to have one product category by product or one parent category by product, because a product can have many categories and subcategories set for it.
要使用优惠券处理此功能,您需要按产品分类一个产品类别或按产品分类一个父类别,因为产品可以为其设置许多类别和子类别。
This custom function will add a coupon discount when cart items are from 3 different product categories. If cart items are removed from cart and there is not anymore 3 different product categories, the coupon code will be auto removed.
当购物车商品来自3个不同的产品类别时,此自定义功能将添加优惠券折扣。如果购物车商品已从购物车中移除,并且不再有3种不同的商品类别,优惠券代码将自动删除。
Also you will need to set in the function the coupon code name and an array of all your matching product categories IDs.
此外,您还需要在功能中设置优惠券代码名称和所有匹配产品类别ID的数组。
Here is the code:
这是代码:
add_action( 'woocommerce_before_calculate_totals', 'add_discount_for_3_diff_cats', 10, 1 );
function add_discount_for_3_diff_cats( $wc_cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE set your coupon code and your parent product categories in the array
$coupon_code_to_apply = 'summer';
// HERE define your product categories IDs in the array
$your_categories = array( 11, 13, 14 ); // IDs
// If coupon is already set
if( $wc_cart->has_discount( $coupon_code_to_apply ) )
$has_coupon = true;
foreach( $wc_cart->get_cart() as $cart_item ) {
$product_id = $cart_item['product_id'];
$product = wc_get_product($product_id);
foreach( $product->get_category_ids() as $category_id ) {
if( has_term( $your_categories, 'product_cat', $product_id ) && in_array( $category_id, $your_categories ) ){
// Set the categories in an array (avoiding duplicates)
$categories[$category_id] = $category_id;
}
}
}
$count_cats = count($categories);
$has_discount = $wc_cart->has_discount( $coupon_code_to_apply );
if ( 3 <= $count_cats && ! $has_discount ) {
$wc_cart->add_discount($coupon_code_to_apply);
} elseif ( 3 > $count_cats && $has_discount ) {
$wc_cart->remove_coupon($coupon_code_to_apply);
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
Tested and works with simple and variable products…
测试并使用简单和可变的产品......
#2
1
Here's a solution that is not using coupon codes that I recycled from this previous question.
这是一个解决方案,不使用我从上一个问题回收的优惠券代码。
add_action( 'woocommerce_cart_calculate_fees' , 'add_multiple_category_discount' );
function add_multiple_category_discount( $cart ){
if( $cart->cart_contents_count < 3 ){
return;
}
$product_cats = array();
foreach( $cart->get_cart() as $item ) {
$product = wc_get_product( $item['product_id'] );
foreach( $product->get_category_ids() as $key => $cat_id ) {
if( ! in_array( $cat_id, $product_cats ) )
$product_cats[] = $cat_id;
}
}
// If we have 3 distinct categories then apply a discount
if( count( $product_cats ) >= 3 ) {
// Add a 10% discount
$discount = $cart->subtotal * 0.1;
$cart->add_fee( 'You have 3 different product categories in your cart, a 10% discount has been added.', -$discount );
}
}