基于Woocommerce中运输类别的运输成本折扣 [英] Shipping cost discount based on a shipping classes in Woocommerce

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

问题描述

我正在尝试对当前购物车中的产品的一种运输类别实行折扣.这将应用于结帐视图.

I'm trying to apply a discount to one shipping class for products currently in a cart. This is applied on the checkout view.

在Woocommerce后端中,该选项设置为单独收取每个运输类别的费用.另外,我仅使用一种称为统一费率"的送货方式.

In Woocommerce backend, the option is set to charge each shipping class individually. Also, I use only one shipping method named "flat rate".

基于覆盖Woocommerce中特定运输类别的所有运输费用,应应用以下折扣的代码:

Based on Override all shipping costs for a specific shipping class in Woocommerce, the following code that should apply the discount:

add_filter('woocommerce_package_rates', 'shipping_class_null_shipping_costs', 10, 2);
function shipping_class_null_shipping_costs( $rates, $package ){

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

    $shipping_class_slug = 'large'; // Your shipping class slug
    $found = false;

    // Loop through cart items and checking for the specific defined shipping class
    foreach( $package['contents'] as $cart_item ) {
        if( $cart_item['data']->get_shipping_class() == $shipping_class_slug )
            $found = true;
    }
    $percentage = 50; // 50%
    $subtotal = WC()->cart->get_cart_shipping_total();

    // Set shipping costs to 50% discount if shipping class is found
    if( $found ){
        foreach ( $rates as $rate_key => $rate ){
            $has_taxes = false;

            // Targetting "flat rate"
            if( 'flat_rate' === $rate->method_id ){
                $rates[$rate_key]->cost = $subtotal;
            }
        }       
    }
    return $rates;
}

但是,无论我如何尝试,计算出的运输结果都是$ 0.

But whatever I try, the calculated shipping result is $0.

我在这里做错了什么?对运输班级实行折扣的正确方法是什么?

What am I doing wrong here and what would be the correct way to apply a discount to shipping class?

谢谢.

推荐答案

更新(仅关于设置)

要仅通过固定费率"运输方式为大型"运输类别添加折扣,您将必须:

Update (Just about settings)

To add a discount only for "large" shipping class on "Flat rate" shipping method, You will have to:

  • 直接在运输方式成本上设置折扣价.
  • 计算选项 按类别:为每个运输类别分别收取运输费用"

赞:

原始答案:

当在购物车项目中找到特定的定义运输方式时,以下代码将为统一费率"运输方式设置50%的运输成本.

The following code will set the shipping cost of 50% for "Flat rate" shipping method, when a specific defined shipping method is found in cart items.

测试:运送"选项 tab ...

Testing: Temporary "Enable debug mode" in Shipping settings under Shipping options tab...

运输统一费率"设置:应定义您的运输费用类别.

Shipping "Flat rate" settings: Your shipping classes costs should be defined.

在下面的代码中,在每个函数中定义运输类别标签和自定义通知:

In the code bellow define in each function your shipping class slug and your custom notice:

add_filter('woocommerce_package_rates', 'shipping_costs_discounted_based_on_shipping_class', 10, 2);
function shipping_costs_discounted_based_on_shipping_class( $rates, $package ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return $rates;

    // Your settings bellow
    $shipping_class = 'large'; // <=== Shipping class slug
    $percentage     = 50; //      <=== Discount percentage

    $discount_rate  = $percentage / 100;
    $is_found       = false;

    // Loop through cart items and checking for the specific defined shipping class
    foreach( $package['contents'] as $cart_item ) {
        if( $cart_item['data']->get_shipping_class() == $shipping_class )
            $is_found = true;
    }

    // Set shipping costs to 50% if shipping class is found
    if( $is_found ){
        foreach ( $rates as $rate_key => $rate ){
            $has_taxes = false;
            // Targeting "flat rate"
            if( 'flat_rate' === $rate->method_id  ){
                $rates[$rate_key]->cost = $rate->cost * $discount_rate;

                // Taxes rate cost (if enabled)
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $tax > 0 ){
                        $has_taxes = true;
                        $taxes[$key] = $tax * $discount_rate;
                    }
                }
                if( $has_taxes )
                    $rates[$rate_key]->taxes = $taxes;
            }
        }
    }
    return $rates;
}

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

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

一旦经过一次测试,别忘了在运输设置中禁用调试模式" .

这篇关于基于Woocommerce中运输类别的运输成本折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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