根据 Woocommerce 购物车重量应用累进折扣 [英] Apply a progressive discount based on Woocommerce cart weight

查看:22
本文介绍了根据 Woocommerce 购物车重量应用累进折扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种根据购物车总重量应用折扣(百分比)的方法.示例:

I'm looking for a way to apply a discount (percentage) based on cart total weight. Example:

  • 10 到 25 公斤,设置 5% 的折扣
  • 26 到 50 公斤,设置 7.5% 的折扣
  • 51 到 100 公斤,设置 10% 的折扣
  • 101 到 150 公斤,设置 12.5% 的折扣
  • 超过 150 公斤,设置 15% 的折扣

当应用其中一项规则时,应该会出现一条消息,就像图中所示的付款折扣.支付方式折扣

When one of the rules will be applied there should be a message also like this payment discount shown in the picture. Payment method discount

如果可能,还可以显示一个消息框,例如添加到购物车消息框",显示客户必须订购多少才能获得第二个折扣规则中的第一个,例如:订购 **kg 以上并获得 5% 的折扣.

And if also possible showing a message box like the 'added to cart message box' which shows how much customers have to order to get the first of a second discounted rule like: Order **kg more and get 5% discount.

我不知道如何实现这一点,所以很遗憾我没有尝试任何东西.提前致谢.

I have no idea how to achieve this, so I unfortunately did not tried anything. Thanks in advance.

问候,瓦斯科

推荐答案

您可以使用负费用根据购物车重量进行累进折扣,如下所示:

You can use a negative fee to make a progressive discount based on cart weight as follow:

add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_discount', 30, 1 );
function shipping_weight_discount( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $cart_weight   = $cart->get_cart_contents_weight();
    $cart_subtotal = $cart->get_subtotal(); // Or $cart->subtotal;
    $percentage    = 0;

    if ( $cart_weight >= 10 && $cart_weight <= 25 ) {
        $percentage = 5;
    } elseif ( $cart_weight > 25 && $cart_weight <= 50 ) {
        $percentage = 7.5;
    } elseif ( $cart_weight > 50 && $cart_weight <= 100 ) {
        $percentage = 10;
    } elseif ( $cart_weight > 100 && $cart_weight <= 150 ) {
        $percentage = 12.5;
    } elseif ( $cart_weight > 150 ) {
        $percentage = 15;
    }

    // Apply a calculated discount based on weight
    if( $percentage > 0 ) {
        $discount = $cart_subtotal * $percentage / 100;
        $cart->add_fee( sprintf( __( 'Weight %s discount', 'woocommerce' ), $percentage.'%'), -$discount );
    }
}

代码位于活动子主题(或活动主题)的 function.php 文件中.

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

基于:添加自定义费用Woocommerce 的总重量

这篇关于根据 Woocommerce 购物车重量应用累进折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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