应针对 woocommerce 中的 2 个不同类别应用两组不同的折扣
Two different set of discounts that should be applied with respect to 2 different categories in woocommerce
这是与 woocommerce 集成的 WordPress 网站。在本店中,主要有两大类Category1和Category2。
我想针对 2 个不同的类别设置两组不同的折扣。
例如:
类别 1 的场景:
- 购买 $500-$999 可享受 5% 的折扣
- 购买 $1000-$2000 即可享受 15% 的折扣
- 购买 $2001-$5000 可享受 25% 的折扣
类别 2 的场景:
- 购买 $500-$999 可享受 20% 的折扣
- 购买 $1000-$2000 即可享受 40% 的折扣
- 购买 $2001-$5000 可享受 60% 的折扣
考虑一下我是否从 Category1 购买价值 1000 美元的 product1 和 product2,以及从 Category2 购买价值 4000 美元的 product5 和 product6。
所以摘要购物车应该如下所示:
Cart subtotal: 00
Category 1 Discount (15%) of 00: -0
Category 2 Discount (60%) of 00 -00
Amount to Pay: 50
应根据每个类别的小计应用折扣。尝试了很多插件和自定义代码,都无法实现woocoomerce中的功能。
您能否建议任何插件或自定义代码来实现此功能?
更新:
我尝试了以下基于线程 的代码。
尝试根据 2 个条件应用累进折扣。
- 基于购物车价值
- 关于特定类别。
但尝试的代码无效。我需要在此代码中更改什么?我需要为 2 个不同的类别设置累进折扣。
add_action( 'woocommerce_cart_calculate_fees', 'cart_items_quantity_wise_discount', 10, 1 );
function cart_items_quantity_wise_discount($cart_object) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Set HERE your category (can be an ID, a slug or the name)
$category = '224'; // our category id
$category_count = 0;
$category_total = 0;
$discount = 0;
// Iterating through each cart item
foreach($cart_object->get_cart() as $cart_item):
//print_r($cart_item);exit;
if( has_term( $category, 'product_cat', $cart_item['product_id']) ):
$category_count += $cart_item['quantity'];
$category_total += $cart_item["line_total"]; // calculated total items amount (quantity x price)
endif;
endforeach;
$discount_text = __( 'Quantity discount of ', 'woocommerce' );
if ( $category_total >=2000 && $category_total <=2999 ) {
$discount -= $category_total * 0.3; // Discount of 10%
$discount_text_output = $discount_text . '10%';
}
elseif ( $category_total >=3000 && $category_total <=4999 ) {
$discount -= $category_total * 0.15; // Discount of 15%
$discount_text_output = $discount_text . '15%';
}
// Adding the discount
if ( $discount != 0 && $category_count >= 12 )
$cart_object->add_fee( $discount_text_output, $discount, false );
// Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
}
第二次更新(第 2 版)- 我尝试了以下代码。但类别明智的折扣不会影响我的结帐页面。你能看看这个以及我在这段代码中做错了什么吗?
add_action( 'woocommerce_cart_calculate_fees', 'cart_items_quantity_multiple_discounts', 10, 1 );
function cart_items_quantity_multiple_discounts( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE set your 2 product category term names
$category1 = 'Appliances';
$category2 = 'Electronics';
$total1 = $total2 = $percentage1 = $percentage2 = 0;
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ) {
if( has_term( $category1, 'product_cat', $cart_item['product_id']) ) {
$total1 += $cart_item["line_total"]; // Excluding taxes
} elseif( has_term( $category2, 'product_cat', $cart_item['product_id']) ) {
$total2 += $cart_item["line_total"]; // Excluding taxes
}
}
// All Amounts need to be set without taxes
// First category "Appliances" progressive percentage discount
if ( $total1 >= 1000 && $total1 < 1500 ) { // <== set excluding taxes
$percentage1 = 5;
}elseif ( $total1 >= 1500 && $total1 < 2500 ) { // <== set excluding taxes
$percentage1 = 10;
}elseif ( $total1 >= 2500 ) { // <== set excluding taxes amounts
$percentage1 = 15;
}
// Second category "Electronics" progressive percentage discount
if ( $total2 >= 1000 && $total2 < 1500 ) { // <== set excluding taxes amounts
$percentage2 = 10;
}elseif ( $total2 >= 1500 && $total2 < 2500 ) { // <== set excluding taxes amounts
$percentage2 = 15;
}elseif ( $total2 >= 2500 ) { // <== set excluding taxes amounts
$percentage2 = 20;
}
// Set the first discount for "Appliances"
if( $percentage1 > 0 ){
$discount1 = $total1 * $percentage1 / 100;
$label_text1 = sprintf( __( '%s Discount (%s) of %s', 'woocommerce' ),
$category1, $percentage1 . '%', strip_tags(wc_price($total1)));
$cart->add_fee( $label_text1, -$discount1 );
}
// Set the Second discount for "Electronics"
if( $percentage2 > 0 ){
$discount2 = $total2 * $percentage2 / 100;
$label_text2 = sprintf( __( '%s Discount (%s) of %s', 'woocommerce' ),
$category2, $percentage2 . '%', strip_tags(wc_price($total2)));
$cart->add_fee( $label_text2, -$discount2 );
}
// Note: Last argument "taxable" in add_fee() method is always true for negative fees (discounts)
}
@Vinayagam - 请在您的子主题中使用以下代码 functions.php。我已经测试过并且工作正常。
add_action( 'woocommerce_cart_calculate_fees', 'cart_items_quantity_multiple_discounts', 10, 1 );
function cart_items_quantity_multiple_discounts( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE set your 2 product category term names
$category1 = 250;//Appliances
$category2 = 224;//Electronics
$pdt_id_1 = $pdt_id_2 = $total1 = $total2 = $percentage1 = $percentage2 = 0;
//$pdt_id_1 - Category One;
//$pdt_id_2 - Category Two;
enter code here
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ) {
$_product = $cart_item['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
foreach ($terms as $term) {
$_categoryid = $term->parent;
if ( $_categoryid === $category1 and $pdt_id_1!=$_product->id) {
$total1 += $cart_item["line_total"];
$pdt_id _1= $_product->id;
}
else if ( $_categoryid === $category2 and $pdt_id_2!=$_product->id) {
$total2 += $cart_item["line_total"];
$pdt_id_2 = $_product->id;
}
}
}
// All Amounts need to be set without taxes
// First category "Appliances" progressive percentage discount
if ( $total1 >= 2000 && $total1 <= 2999 ) {
$percentage1 = 3;
}elseif ( $total1 >= 3000 && $total1 <= 4999 ) {
$percentage1 = 5;
}elseif ( $total1 >= 5000 && $total1 <= 9999) {
$percentage1 = 8;
}elseif ( $total1 >= 10000 && $total1 <= 14999) {
$percentage1 = 10;
}elseif ( $total1 >= 15000 && $total1 <= 24999) {
$percentage1 = 13;
}elseif ( $total1 >= 25000 && $total1 <= 29999) {
$percentage1 = 15;
}elseif ( $total1 >= 30000 && $total1 <= 49999) {
$percentage1 = 18;
}elseif ( $total1 >= 50000 && $total1 <= 59999) {
$percentage1 = 19;
}elseif ( $total1 >= 60000 && $total1 <= 499999) {
$percentage1 = 20;
}
// Second category "Electronics" progressive percentage discount
if ( $total2 >= 2000 && $total2 <= 2999 ) {
$percentage2 = 5;
}elseif ( $total2 >= 3000 && $total2 <= 4999 ) {
$percentage2 = 10;
}elseif ( $total2 >= 5000 && $total2 <= 9999 ) {
$percentage2 = 15;
}elseif ( $total2 >= 10000 && $total2 <= 14999 ) {
$percentage2 = 20;
}elseif ( $total2 >= 15000 && $total2 <= 24999 ) {
$percentage2 = 25;
}elseif ( $total2 >= 25000 && $total2 <= 29999 ) {
$percentage2 = 30;
}elseif ( $total2 >= 30000 && $total2 <= 49999 ) {
$percentage2 = 40;
}elseif ( $total2 >= 50000 && $total2 <= 59999 ) {
$percentage2 = 45;
}elseif ( $total2 >= 60000 && $total2 <= 499999 ) {
$percentage2 = 50;
}
// Set the first discount for "Appliances"
if( $percentage1 > 0 ){
$discount1 = $total1 * $percentage1 / 100;
$label_text1 = sprintf( __( '%s Discount (%s) of %s', 'woocommerce' ),
'Appliances', $percentage1 . '%', strip_tags(wc_price($total1)));
$cart->add_fee( $label_text1, -$discount1, true, '');
}
// Set the Second discount for "Electronics"
if( $percentage2 > 0 ){
$discount2 = $total2 * $percentage2 / 100;
$label_text2 = sprintf( __( '%s Discount (%s) of %s', 'woocommerce' ),
'Electronics', $percentage2 . '%', strip_tags(wc_price($total2)));
$cart->add_fee( $label_text2, -$discount2 );
}
// Note: Last argument "taxable" in add_fee() method is always true for negative fees (discounts)
}
这是与 woocommerce 集成的 WordPress 网站。在本店中,主要有两大类Category1和Category2。
我想针对 2 个不同的类别设置两组不同的折扣。
例如:
类别 1 的场景:
- 购买 $500-$999 可享受 5% 的折扣
- 购买 $1000-$2000 即可享受 15% 的折扣
- 购买 $2001-$5000 可享受 25% 的折扣
类别 2 的场景:
- 购买 $500-$999 可享受 20% 的折扣
- 购买 $1000-$2000 即可享受 40% 的折扣
- 购买 $2001-$5000 可享受 60% 的折扣
考虑一下我是否从 Category1 购买价值 1000 美元的 product1 和 product2,以及从 Category2 购买价值 4000 美元的 product5 和 product6。
所以摘要购物车应该如下所示:
Cart subtotal: 00
Category 1 Discount (15%) of 00: -0
Category 2 Discount (60%) of 00 -00
Amount to Pay: 50
应根据每个类别的小计应用折扣。尝试了很多插件和自定义代码,都无法实现woocoomerce中的功能。
您能否建议任何插件或自定义代码来实现此功能?
更新:
我尝试了以下基于线程
尝试根据 2 个条件应用累进折扣。
- 基于购物车价值
- 关于特定类别。
但尝试的代码无效。我需要在此代码中更改什么?我需要为 2 个不同的类别设置累进折扣。
add_action( 'woocommerce_cart_calculate_fees', 'cart_items_quantity_wise_discount', 10, 1 );
function cart_items_quantity_wise_discount($cart_object) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Set HERE your category (can be an ID, a slug or the name)
$category = '224'; // our category id
$category_count = 0;
$category_total = 0;
$discount = 0;
// Iterating through each cart item
foreach($cart_object->get_cart() as $cart_item):
//print_r($cart_item);exit;
if( has_term( $category, 'product_cat', $cart_item['product_id']) ):
$category_count += $cart_item['quantity'];
$category_total += $cart_item["line_total"]; // calculated total items amount (quantity x price)
endif;
endforeach;
$discount_text = __( 'Quantity discount of ', 'woocommerce' );
if ( $category_total >=2000 && $category_total <=2999 ) {
$discount -= $category_total * 0.3; // Discount of 10%
$discount_text_output = $discount_text . '10%';
}
elseif ( $category_total >=3000 && $category_total <=4999 ) {
$discount -= $category_total * 0.15; // Discount of 15%
$discount_text_output = $discount_text . '15%';
}
// Adding the discount
if ( $discount != 0 && $category_count >= 12 )
$cart_object->add_fee( $discount_text_output, $discount, false );
// Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
}
第二次更新(第 2 版)- 我尝试了以下代码。但类别明智的折扣不会影响我的结帐页面。你能看看这个以及我在这段代码中做错了什么吗?
add_action( 'woocommerce_cart_calculate_fees', 'cart_items_quantity_multiple_discounts', 10, 1 );
function cart_items_quantity_multiple_discounts( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE set your 2 product category term names
$category1 = 'Appliances';
$category2 = 'Electronics';
$total1 = $total2 = $percentage1 = $percentage2 = 0;
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ) {
if( has_term( $category1, 'product_cat', $cart_item['product_id']) ) {
$total1 += $cart_item["line_total"]; // Excluding taxes
} elseif( has_term( $category2, 'product_cat', $cart_item['product_id']) ) {
$total2 += $cart_item["line_total"]; // Excluding taxes
}
}
// All Amounts need to be set without taxes
// First category "Appliances" progressive percentage discount
if ( $total1 >= 1000 && $total1 < 1500 ) { // <== set excluding taxes
$percentage1 = 5;
}elseif ( $total1 >= 1500 && $total1 < 2500 ) { // <== set excluding taxes
$percentage1 = 10;
}elseif ( $total1 >= 2500 ) { // <== set excluding taxes amounts
$percentage1 = 15;
}
// Second category "Electronics" progressive percentage discount
if ( $total2 >= 1000 && $total2 < 1500 ) { // <== set excluding taxes amounts
$percentage2 = 10;
}elseif ( $total2 >= 1500 && $total2 < 2500 ) { // <== set excluding taxes amounts
$percentage2 = 15;
}elseif ( $total2 >= 2500 ) { // <== set excluding taxes amounts
$percentage2 = 20;
}
// Set the first discount for "Appliances"
if( $percentage1 > 0 ){
$discount1 = $total1 * $percentage1 / 100;
$label_text1 = sprintf( __( '%s Discount (%s) of %s', 'woocommerce' ),
$category1, $percentage1 . '%', strip_tags(wc_price($total1)));
$cart->add_fee( $label_text1, -$discount1 );
}
// Set the Second discount for "Electronics"
if( $percentage2 > 0 ){
$discount2 = $total2 * $percentage2 / 100;
$label_text2 = sprintf( __( '%s Discount (%s) of %s', 'woocommerce' ),
$category2, $percentage2 . '%', strip_tags(wc_price($total2)));
$cart->add_fee( $label_text2, -$discount2 );
}
// Note: Last argument "taxable" in add_fee() method is always true for negative fees (discounts)
}
@Vinayagam - 请在您的子主题中使用以下代码 functions.php。我已经测试过并且工作正常。
add_action( 'woocommerce_cart_calculate_fees', 'cart_items_quantity_multiple_discounts', 10, 1 );
function cart_items_quantity_multiple_discounts( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE set your 2 product category term names
$category1 = 250;//Appliances
$category2 = 224;//Electronics
$pdt_id_1 = $pdt_id_2 = $total1 = $total2 = $percentage1 = $percentage2 = 0;
//$pdt_id_1 - Category One;
//$pdt_id_2 - Category Two;
enter code here
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ) {
$_product = $cart_item['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
foreach ($terms as $term) {
$_categoryid = $term->parent;
if ( $_categoryid === $category1 and $pdt_id_1!=$_product->id) {
$total1 += $cart_item["line_total"];
$pdt_id _1= $_product->id;
}
else if ( $_categoryid === $category2 and $pdt_id_2!=$_product->id) {
$total2 += $cart_item["line_total"];
$pdt_id_2 = $_product->id;
}
}
}
// All Amounts need to be set without taxes
// First category "Appliances" progressive percentage discount
if ( $total1 >= 2000 && $total1 <= 2999 ) {
$percentage1 = 3;
}elseif ( $total1 >= 3000 && $total1 <= 4999 ) {
$percentage1 = 5;
}elseif ( $total1 >= 5000 && $total1 <= 9999) {
$percentage1 = 8;
}elseif ( $total1 >= 10000 && $total1 <= 14999) {
$percentage1 = 10;
}elseif ( $total1 >= 15000 && $total1 <= 24999) {
$percentage1 = 13;
}elseif ( $total1 >= 25000 && $total1 <= 29999) {
$percentage1 = 15;
}elseif ( $total1 >= 30000 && $total1 <= 49999) {
$percentage1 = 18;
}elseif ( $total1 >= 50000 && $total1 <= 59999) {
$percentage1 = 19;
}elseif ( $total1 >= 60000 && $total1 <= 499999) {
$percentage1 = 20;
}
// Second category "Electronics" progressive percentage discount
if ( $total2 >= 2000 && $total2 <= 2999 ) {
$percentage2 = 5;
}elseif ( $total2 >= 3000 && $total2 <= 4999 ) {
$percentage2 = 10;
}elseif ( $total2 >= 5000 && $total2 <= 9999 ) {
$percentage2 = 15;
}elseif ( $total2 >= 10000 && $total2 <= 14999 ) {
$percentage2 = 20;
}elseif ( $total2 >= 15000 && $total2 <= 24999 ) {
$percentage2 = 25;
}elseif ( $total2 >= 25000 && $total2 <= 29999 ) {
$percentage2 = 30;
}elseif ( $total2 >= 30000 && $total2 <= 49999 ) {
$percentage2 = 40;
}elseif ( $total2 >= 50000 && $total2 <= 59999 ) {
$percentage2 = 45;
}elseif ( $total2 >= 60000 && $total2 <= 499999 ) {
$percentage2 = 50;
}
// Set the first discount for "Appliances"
if( $percentage1 > 0 ){
$discount1 = $total1 * $percentage1 / 100;
$label_text1 = sprintf( __( '%s Discount (%s) of %s', 'woocommerce' ),
'Appliances', $percentage1 . '%', strip_tags(wc_price($total1)));
$cart->add_fee( $label_text1, -$discount1, true, '');
}
// Set the Second discount for "Electronics"
if( $percentage2 > 0 ){
$discount2 = $total2 * $percentage2 / 100;
$label_text2 = sprintf( __( '%s Discount (%s) of %s', 'woocommerce' ),
'Electronics', $percentage2 . '%', strip_tags(wc_price($total2)));
$cart->add_fee( $label_text2, -$discount2 );
}
// Note: Last argument "taxable" in add_fee() method is always true for negative fees (discounts)
}