英文:
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('woocommerce_package_rates', 'increase_shipping_costs', 10, 2);
function increase_shipping_costs( $rates, $package ) {
$rate_multiplier = 1.65; // 65% increase cost
// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ) {
if ( 'free_shipping' !== $rate->method_id && $rate->cost > 0 ) {
$has_taxes = false; // Initializing
$taxes = array(); // Initializing
$rates[$rate_key]->cost = $rate->cost * $rate_multiplier; // Set new rate cost
// Loop through taxes array (change taxes rate cost if enabled)
foreach ($rate->taxes as $key => $tax){
if( $tax > 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]->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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论