我已经建立了一个 Woocommerce 商店,并希望为所有产品设置基于 12 倍数(一盒)的特定折扣。我尝试了很多折扣插件,但没有找到我想要的。
例如,如果我订购 12 件产品 X,我将获得 10% 的折扣。如果我订购 15 件产品 X,前 12 件我可以享受 10% 的折扣,后 3 件是全价。如果我订购 24 件,则 10% 的折扣适用于所有 24 件产品 X。
我发现的最接近的是:根据产品总数对某些类别进行折扣 https://stackoverflow.com/questions/40871142/discount-for-certain-category-based-on-total-number-of-products/40899247#40899247
但这最后会作为折扣(实际上是负费用)应用,我想在产品旁边的购物车中显示折扣,就像正常折扣一样。
如果产品已经在打折,我还需要禁用此折扣。
Thanks.
此代码在 Woocommerce 3+ 中不起作用…
See: Woocommerce 3 中基于数量的购物车商品折扣 https://stackoverflow.com/questions/52010505/cart-item-discount-based-on-quantity-in-woocommerce-3/52012795#52012795:
是的,这也是可能的,为每个购物车项目进行自定义计算并单独替换其价格(符合您的条件和计算),使用挂钩的自定义函数woocommerce_before_calculate_totals
动作挂钩。
这是代码:
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' );
}
这正是您对购物车中每件商品分别期望的折扣(基于其数量)不适用于正在销售的商品。但您不会在购物车的订单项中获得任何指示折扣的标签(文本)。
(可选)当折扣应用于某些购物车商品时,我会显示一条通知...
代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。
这段代码已经过测试并且可以工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)