将百分比增加添加到所有WooCommerce运费率的程序化方法

huangapple go评论60阅读模式
英文:

Add percentage increase to all WooCommerce shipping rates programmatically

问题

我尝试在所有运输方式上增加65%的费用。

我使用了这个函数的不同变体,但这不会影响返回的费率。

function increase_shipping_costs() {
  // 获取当前的运费费率
  $shipping_zones = WC_Shipping_Zones::get_zones();

  foreach ( $shipping_zones as $zone ) {
    foreach ( $zone['shipping_methods'] as $shipping_method ) {
      // 检查运输方式是否是固定费率
      if ( !$shipping_method->id === 'free_shipping' ) {
        // 获取当前费用
        $current_cost = $shipping_method->cost;

        // 计算增加后的费用
        $increased_cost = $current_cost + ( $current_cost * 0.65 );

        // 更新运输费用
        $shipping_method->cost = $increased_cost;
        $shipping_method->save();
      }
    }
  }
}

add_filter('woocommerce_package_rates', 'increase_shipping_costs', 10, 2);
英文:

I'm trying to add a 65% increase across all shipping methods.

I've used variations of this function but this doesn't affect the rates that are returned.

function increase_shipping_costs() {
  // Get the current shipping rates
  $shipping_zones = WC_Shipping_Zones::get_zones();

  foreach ( $shipping_zones as $zone ) {
    foreach ( $zone['shipping_methods'] as $shipping_method ) {
      // Check if the shipping method is a flat rate
      if ( !$shipping_method->id === 'free_shipping' ) {
        // Get the current cost
        $current_cost = $shipping_method->cost;

        // Calculate the increased cost
        $increased_cost = $current_cost + ( $current_cost * 0.65 );

        // Update the shipping cost
        $shipping_method->cost = $increased_cost;
        $shipping_method->save();
      }
    }
  }
}

add_filter('woocommerce_package_rates', 'increase_shipping_costs', 10, 2);

答案1

得分: 1

过滤器的回调函数设置不正确:

  • 缺少函数参数
  • 没有返回值(过滤器需要返回值)

您将想要使用通过过滤器提供的数据(例如:$package_rates),而不是WC_Shipping_Zones::get_zones()

function increase_shipping_costs( $package_rates, $package ) {
    ...

    return $package_rates;
}
add_filter( 'woocommerce_package_rates', 'increase_shipping_costs', 10, 2 );
英文:

The filter's callback is not setup properly:

  • function parameters are missing
  • no value being returned (filters require a return value)

You'll want to use the data provided through the filter (ex: $package_rates) rather than WC_Shipping_Zones::get_zones().

function increase_shipping_costs( $package_rates, $package ) {
    ...

    return $package_rates;
}
add_filter( 'woocommerce_package_rates', 'increase_shipping_costs', 10, 2 );

</details>



# 答案2
**得分**: 1

要在所有运输方式费用上增加65%的费用,`woocommerce_package_rates`是正确的过滤器挂钩,但您需要修改函数中的大部分内容以使其正常工作(因为代码中有一些缺少的部分和错误):

```php
add_filter('woocommerce_package_rates', 'increase_shipping_costs', 10, 2);
function increase_shipping_costs($rates, $package) {
    $rate_multiplier = 1.65; // 增加65%的费用
    
    // 循环遍历运输费率
    foreach ($rates as $rate_key => $rate) {
        
        if ('free_shipping' !== $rate->method_id && $rate->cost > 0) {
            $has_taxes = false; // 初始化
            $taxes     = array(); // 初始化

            $rates[$rate_key]->cost = $rate->cost * $rate_multiplier; // 设置新的费率费用
            
            // 循环遍历税收数组(如果启用,更改税率费用)
            foreach ($rate->taxes as $key => $tax) {
                if ($tax > 0) {
                    $taxes[$key] = $tax * $rate_multiplier; // 在数组中设置新的税费用
                    $has_taxes   = true; // 启用税收更改
                }
            }
            // 设置运输税费用数组
            if ($has_taxes) {
                $rates[$rate_key]->taxes = $taxes;
            }
        }
    }
    // 对于过滤器挂钩,始终返回主要参数
    return $rates; 
}

代码放在活动子主题(或活动主题)的functions.php文件中。它应该能够正常工作。

英文:

To add a 65% increase cost across all shipping methods rates, woocommerce_package_rates is the correct filter hook to be used, but you will need to modify mostly everything in your function to get it working properly (as there are some missing things and some mistakes in your code):

add_filter(&#39;woocommerce_package_rates&#39;, &#39;increase_shipping_costs&#39;, 10, 2);
function increase_shipping_costs( $rates, $package ) {
    $rate_multiplier = 1.65; //  65% increase cost
    
    // Loop through shipping rates
    foreach ( $rates as $rate_key =&gt; $rate ) {
        
        if ( &#39;free_shipping&#39; !== $rate-&gt;method_id &amp;&amp; $rate-&gt;cost &gt; 0 ) {
            $has_taxes = false; // Initializing
            $taxes     = array(); // Initializing

            $rates[$rate_key]-&gt;cost = $rate-&gt;cost * $rate_multiplier; // Set new rate cost
            
            // Loop through taxes array (change taxes rate cost if enabled)
            foreach ($rate-&gt;taxes as $key =&gt; $tax){
                if( $tax &gt; 0 ){
                    $taxes[$key] = $tax * $rate_multiplier; // Set the new tax cost in the array
                    $has_taxes   = true; // Enabling tax changes
                }
            }
            // set array of shipping tax cost
            if( $has_taxes ) {
                $rates[$rate_key]-&gt;taxes = $taxes;
            }
        }
    }
    // For a filter hook, always return the main argument
    return $rates; 
}

Code goes in functions.php file of the active child theme (or active theme). It should work.

huangapple
  • 本文由 发表于 2023年6月9日 00:29:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定