首页 > 解决方案 > 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;
}

标签: woocommercehook-woocommercewoothemes

解决方案


推荐阅读