• Resolved baudouin88

    (@baudouin88)


    Great plugin but the hook woocommerce_package_rate isn’t firing.

    Currently trying to increase the outputted rate by x%, here is the following code:

    Since the Echo on the console.log isn’t showing up, I’m assuming the woocommerce_package_rates isn’t firing. In that case, what would be the hook to use with flexible shipping?

    add_filter('woocommerce_package_rates', 'add_percentage_to_shipping_cost', 10, 2);
    function add_percentage_to_shipping_cost($rates, $package) {
        // I get the selected shipping method
        $chosen_shipping_method = WC()->session->get('chosen_shipping_methods')[0];
      echo '<script>console.log("Chosen Shipping Methods: ");</script>';
    
        // I've Defined the shipping method IDs for which I want to add 10%
        $target_shipping_methods = array(
            'flexible_shipping_ups:2:02', 
            'flexible_shipping_ups:2:03', 
            'flexible_shipping_ups:2:01', 
        );
    
      
        if (in_array($chosen_shipping_method, $target_shipping_methods)) {
            
            foreach ($rates as $rate_id => $rate) {
                
                if (in_array($rate_id, $target_shipping_methods)) {
                   
                    $new_cost = $rate->cost + ($rate->cost * 0.1);
                    $rates[$rate_id]->cost = $new_cost;
                }
            }
        }
    
        return $rates;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author grola

    (@grola)

    Hi @baudouin88,

    the woocommerce_package_rates filter is triggered by woocommerce, not by our plugin.

    If woocommerce_package_rates does not start, just go to the checkout page and change some address fields, simply refreshing the page may not be enough.

    Best reghards
    Grzegorz

    Thread Starter baudouin88

    (@baudouin88)

    Thanks I understand. I was under the impression the hook was being bypassed somehow by the plugin. Please feel free to delete this thread if you can.

    Best regards

    • This reply was modified 1 year, 4 months ago by baudouin88.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘woocommerce_package_rates not firing’ is closed to new replies.