【发布时间】:2019-02-24 17:23:41
【问题描述】:
这是与 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 购买 product5 和 product6,价值 4000 美元。
所以总结购物车应该如下所示:
Cart subtotal: $5000
Category 1 Discount (15%) of $1000: -$150
Category 2 Discount (60%) of $4000 -$2400
Amount to Pay: $2450
应根据每个类别的小计应用折扣。我尝试了很多插件并自定义了代码,但我无法在 woocoomerce 中实现功能。
您能否建议任何插件或自定义代码来实现此功能?
更新:
我已经根据线程Auto apply a percentage or fixed cart discount based on the total in WooCommerce 尝试了以下代码。
尝试根据 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)
}
【问题讨论】:
标签: wordpress woocommerce discount