1

Using the filter, woocommerce_package_rates, it works at first, but upon loading the checkout page, it seems to load this filter multiple times.

Example. If the cost of shipping is, $20. I need to multiply this by 2, which should be $40. But it will loop again, and it will become $80. When I first load the checkout, it shows the right price, but after 2 seconds, it adjusts it again to $80.

Not sure what to do here, or how to handle this.

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;
}
Justin
  • 2,502
  • 7
  • 42
  • 77

0 Answers0