基于产品总数的某些类别的折扣 [英] Discount for Certain Category Based on Total Number of Products

查看:91
本文介绍了基于产品总数的某些类别的折扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce中,我有一类产品称为样本",每个样本的价格为2.99美元. 但是,我希望有一种方法可以在将5个样品添加到购物车后自动将样品的价格从2.99美元更改为1美元.

In WooCommerce, I have a category of products called Samples, each sample costs $2.99. But I'd like a way to automatically change the cost of the Samples from $2.99 to $1 when 5 Samples are added to cart.

因此,如果将4个样本添加到购物车,则总计为$ 11.96…但是,如果添加5个样本,则总计为$ 5.

So if 4 samples are added to cart, the total would be $11.96… but if 5 were added the total would be $5.

因此,每5个产品,产品价格将从2.99美元更改为1美元,但如果将6个样品添加到购物车中,则总计为7.99美元;如果添加10个样品,则总计为10美元,等等.

So for every 5 products, the product price would change from $2.99 to $1 but if 6 Samples were added to cart the total would be $7.99 and if 10 were added the total would be $10 etc...

我怎么能做到这一点?

谢谢.

推荐答案

更新-增加了Woocommerce 3兼容性.

这是应该满足您需求的东西.
此功能将为购物车增加折扣:

Here is something that should be convenient to your requirements.
This function will add discount to cart:

add_action( 'woocommerce_cart_calculate_fees','custom_cart_discount', 20, 1 );
function custom_cart_discount( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Define HERE your targeted product category (id, slug or name are accepted)
    $category = 'posters';
    // Set the price for Five HERE
    $price_x5 = 5;

    // initializing variables
    $calculated_qty = 0;
    $calculated_total = 0;
    $discount = 0;

    // Iterating through each cart item
    foreach($cart->get_cart() as $cart_item):

        // Make this discount calculations only for products of your targeted category
        if(has_term($category, 'product_cat', $cart_item['product_id'])):
            $item_price = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->price : $cart_item['data']->get_price(); // The price for one (assuming that there is always 2.99)
            $item_qty = $cart_item["quantity"];// Quantity
            $item_line_total = $cart_item["line_total"]; // Item total price (price x quantity)
            $calculated_qty += $item_qty; // ctotal number of items in cart
            $calculated_total += $item_line_total; // calculated total items amount
        endif;
    endforeach;

    // ## CALCULATIONS (updated) ##
    if($calculated_qty >= 5):      
        for($j = 5, $k=0; $j <= $calculated_qty; $j+=5,$k++); // Update $k=0 (instead of $k=1)
        $qty_modulo = $calculated_qty % 5;
        $calculation = ( $k * $price_x5 ) + ($qty_modulo * $item_price);
        $discount -= $calculated_total - $calculation;
    endif;

    // Adding the discount 
    if ($discount != 0)
        $cart->add_fee( __( 'Quantity discount', 'woocommerce' ), $discount, false );
        // Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于基于产品总数的某些类别的折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆