问题描述
我已经开设了一家Woocommerce商店,希望对所有产品设置特定的折扣,折扣为12(盒)的倍数.我尝试了许多折扣插件,但没有找到我想要的东西.
I have set up a Woocommerce shop and wish to set up a specific discount on all products based on multiples of 12 (a box). I've tried many discount plugins but haven't found what I am looking for.
例如,如果我订购12件产品X,我将获得10%的折扣.如果我订购15个产品X,则前12个产品可享受10%的折扣,而后三个价格为全价.如果我订购24件,则该10%的折扣适用于产品X的全部24件.
For example, if I order 12 of product X, I get a 10% discount. If I order 15 of product X, I get a 10% discount on the first 12, and the last three are full price. If I order 24, then that 10% discount applies to all 24 of product X.
我找到的最接近的是:
The closest I have found is this: Discount for Certain Category Based on Total Number of Products
但这是最后的折扣(实际上是负费用),我想像正常折扣一样在产品旁边的购物车中显示折扣.
But this is applied as a discount (actually a negative fee) at the end, and I'd like to display the discount in the cart next to the product, like normal discounts.
如果产品已经开始销售,我还需要禁用此折扣.
谢谢.
推荐答案
请参阅:基于Woocommerce 3中的数量:
是的,通过使用 woocommerce_before_calculate_totals
动作挂钩.
Yes this is also possible, making a custom calculation for each cart item and replacing individually their price (matching your conditions and calculations), using a custom function hooked in woocommerce_before_calculate_totals
action hook.
这是代码:
add_action( 'woocommerce_before_calculate_totals', 'custom_discounted_cart_item_price', 10, 1 );
function custom_discounted_cart_item_price( $cart_object ) {
$discount_applied = false;
// Set Here your targeted quantity discount
$t_qty = 12;
// Iterating through each item in cart
foreach ( $cart_object->get_cart() as $item_values ) {
## Get cart item data
$item_id = $item_values['data']->id; // Product ID
$item_qty = $item_values['quantity']; // Item quantity
$original_price = $item_values['data']->price; // Product original price
// Getting the object
$product = new WC_Product( $item_id );
// CALCULATION FOR EACH ITEM
// when quantity is up to the targetted quantity and product is not on sale
if( $item_qty >= $t_qty && !$product->is_on_sale() ){
for($j = $t_qty, $loops = 0; $j <= $item_qty; $j += $t_qty, $loops++);
$modulo_qty = $item_qty % $t_qty; // The remaining non discounted items
$item_discounted_price = $original_price * 0.9; // Discount of 10 percent
$total_discounted_items_price = $loops * $t_qty * $item_discounted_price;
$total_normal_items_price = $modulo_qty * $original_price;
// Calculating the new item price
$new_item_price = ($total_discounted_items_price + $total_normal_items_price) / $item_qty;
// Setting the new price item
$item_values['data']->price = $new_item_price;
$discount_applied = true;
}
}
// Optionally display a message for that discount
if ( $discount_applied )
wc_add_notice( __( 'A quantity discount has been applied on some cart items.', 'my_theme_slug' ), 'success' );
}
(可选)当某些购物车项目享受折扣时,我会显示一条通知...
Optionally I display a notice when a discount is applied to some cart items…
代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.
此代码已经过测试并且可以正常工作.
This code is tested and works.
这篇关于根据商品数量有条件地按购物车商品添加折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!