woocommerce - WooCommerce 包裹费率(循环 2 次)会产生成倍的费率成本
问题描述
使用过滤器 woocommerce_package_rates,它起初可以工作,但在加载结帐页面时,它似乎多次加载此过滤器。
例子。如果运费是 20 美元。我需要将其乘以 2,即 40 美元。但它会再次循环,它将变成 80 美元。当我第一次加载结帐时,它显示正确的价格,但 2 秒后,它再次将其调整为 80 美元。
不知道在这里做什么,或者如何处理。
add_filter( 'woocommerce_package_rates', 'woocommerce_shipping_rates', 10, 2);
function woocommerce_shipping_rates($rates, $package) {
$multiplier = 2;
foreach ( $rates as $rate_key => $rate ){
if( 'canada_post' === $rate->method_id ) {
$has_taxes = false;
// Set the new cost
//echo "CALC:". $rate->cost ." * ". $multiplier ;
$rates[$rate_key]->cost = $rate->cost * $multiplier ;
// Taxes rate cost (if enabled)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $tax > 0 ){
// New tax calculated cost
$taxes[$key] = $tax * $multiplier ;
$has_taxes = true;
}
}
// Set new taxes cost
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}
解决方案
推荐阅读
- django - 基于 CharField 用 ForeignKey 替换 CharField
- java - 如何更改 barChart ApachePoi 中文本的颜色
- mysql - 确定是否在一个线程 MySQL 中读取所有消息
- qt - 如何替换或重新定义父类的内联组件
- python - 在 JSON Python 字典中引用没有标签的日期字段
- iframe - iframe 在一个雨果帖子中的 HTML 文件
- node.js - 即使在删除 node_modules 和 npm install 后安装新包时也无法修复编译错误
- android - Android 如何接管相机使用
- flutter - 在 Column 小部件中移动图像
- spring-boot - spring boot tomcat访问日志log4j2弹性通用模式