I’ve made a custom shipping class for WooCommerce. In the calculate_shipping function an API request is done to our server requesting the possible shipping methods. This can result in one or multiple rates.

I have integrated this as follows in the calculate_shipping() function

    // Make a shipping rate for each result
    foreach ($quote->shipping_options as $option) {

        $rate = array(
            'id' => 'my_method_'.$option->id,
            'label' => $methodNames[$option->name],
            'cost' => $option->price,
        );

        // Register the rate
        $this->add_rate($rate);
    }

This displays the right options, uses the right rate and in the WooCommerce admin I can see which method was used. So far, so good.

The issue is:

The order is stored with the shipping ID from the custom shipping class and the label of the chosen rate.

How do I recognize programmatically which shipping method was used? I don’t want to match on label for obvious reasons, and the ID is wrong. Can I store an identifier or change this behaviour?

1 Answer
1

to understand how to store choosen method id or else try using:

// Make a shipping rate for each result
 foreach ($quote->shipping_options as $option) {

    $rate = array(
        'id' => 'my_method_'.$option->id,
        'label' => $methodNames[$option->name],
        'cost' => $option->price,
        'meta_data' => array(),
    );

    // Register the rate
    $this->add_rate($rate);
}

in ‘meta_data’ you can store whatever you wantin database…

Leave a Reply

Your email address will not be published. Required fields are marked *