【问题标题】:Shipping cost discount based on a shipping classes in Woocommerce基于 Woocommerce 中的运输类别的运输成本折扣
【发布时间】:2023-03-23 14:41:01
【问题描述】:

我正在尝试为当前在购物车中的产品的一个配送类别应用折扣。这适用于结帐视图。

在 Woocommerce 后端中,该选项设置为对每个运输类别单独收费。另外,我只使用一种名为“统一费率”的运输方式。

基于Override all shipping costs for a specific shipping class in Woocommerce,应应用折扣的以下代码:

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 美元。

我在这里做错了什么,对运输类应用折扣的正确方法是什么?

谢谢。

【问题讨论】:

  • @LoicTheAztec,感谢您的意见!以后我会多加注意的。

标签: php wordpress woocommerce discount shipping-method


【解决方案1】:

更新(只是关于设置)

要在“统一费率”运输方式中仅为“大型”运输类别添加折扣,您必须:

  • 直接根据您的运输方式成本设置折扣价。
  • 启用选项“按类别:单独为每个运输类别收取运费”

喜欢:


原答案:

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

测试:运输选项标签下运输设置中的临时“启用调试模式”...

运费“统一费率”设置:应定义您的运费等级。

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

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 below
    $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 文件中。经过测试并且可以工作。

一旦测试过一次,别忘了在发货设置中禁用“调试模式”

【讨论】:

  • 感谢您的帮助。这确实增加了折扣,但是,它将折扣应用于购物车中所有运输类别的总和。似乎在代码$shipping_class = 'large'; 中定义运输类别对总数没有任何影响。这怎么可能?
  • 这很奇怪,因为我已经按照您在屏幕截图中的建议进行了运输。 “大” = 200 美元; “小” = 150 美元;每类运费。但是在结帐时,运费是 175 美元,这是两个类别加起来的 50% 折扣; 200+150 / 2 = 175。查看您提供的代码,我也看不出问题出在哪里。还有其他想法吗?非常感谢!
  • @Mario83 对我来说,它与所提供的设置完美配合:每件物品的运输等级成本都会被添加并产生正确的总成本......所以还有其他一些问题在您的安装和设置中造成麻烦。
  • 我希望我也能这么说。我在 2 个本地环境和一个安装了基本 WP 的实时环境中对此进行了测试 - 不走运。它不断打折总和而不是单一的运输类别。明天我会更深入地研究这个问题,我会分享我的进展和可能的解决方案。
  • @Mario 您不需要任何代码,只需要设置,直接在您的“大型”运输类别中输入折扣成本。因此,如果“小” = 150 美元……“大”(折扣)= 100 美元(200 美元的 50%)……如果两者都在购物车中,您将有 150 + 100 = 250 美元。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-28
  • 1970-01-01
  • 2014-02-12
  • 1970-01-01
  • 2021-04-12
相关资源
最近更新 更多