最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Add multiple shipping rates from add_rate function with custom ID

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Jun 15, 2020 at 9:09 Mike asked Jun 10, 2020 at 13:53 MikeMike 1611 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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...

发布评论

评论列表(0)

  1. 暂无评论