简体   繁体   English

根据商品数量有条件地按购物车商品添加折扣

[英]Adding a discount by cart items conditionally based on the item quantity

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). 我已经建立了一个Woocommerce商店,并希望根据12(一个盒子)的倍数设置所有产品的特定折扣。 I've tried many discount plugins but haven't found what I am looking for. 我尝试了许多折扣插件,但没有找到我要找的东西。

For example, if I order 12 of product X, I get a 10% discount. 例如,如果我订购产品X的12个,我可以获得10%的折扣。 If I order 15 of product X, I get a 10% discount on the first 12, and the last three are full price. 如果我订购15件产品X,我会在前12件中获得10%的折扣,最后三件是全价。 If I order 24, then that 10% discount applies to all 24 of product X. 如果我订购24,那么10%的折扣适用于所有24个产品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. 但这最终会作为折扣(实际上是负面费用)应用,我想在产品旁边的购物车中显示折扣,就像普通折扣一样。

I also need this discount to be disabled if a product is already on sale. 如果产品已经开始销售,我也需要禁用此折扣。

Thanks. 谢谢。

This code will not work in Woocommerce 3+ 此代码在Woocommerce 3+中不起作用 ...

See: Cart item discount based on quantity in Woocommerce 3 : 请参阅: 基于Woocommerce 3中数量的购物车商品折扣

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. 是的,这也是可能的,为每个购物车项目进行自定义计算, 使用挂在woocommerce_before_calculate_totals动作挂钩中的自定义函数单独替换它们的价格(匹配您的条件和计算)

This is the code: 这是代码:

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' );
}

This make exactly the discount that you are expecting separately for each item in cart (based on it's quantity) and not for items that are in sale . 这样就可以为购物车中的每件商品(根据商品的数量)分别提供折扣, 而不是针对销售商品 But you will not get any label (text) indicating a discount in the line items of cart. 但是你不会得到任何标签(文字),表明购物车的行项目有折扣。

Optionally I display a notice when a discount is applied to some cart items… 可选地,当对某些购物车项目应用折扣时,我会显示通知...

Code goes in function.php file of your active child theme (or theme) or also in any plugin file. 代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

This code is tested and works. 此代码经过测试和运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM