Custom shipping ID from add_rate function WooCommerce not stored in DB

Question

I have written my own shipping method and hooked into WooCommerce. I’ve used the shipping class from the WooCommerce docs, works like a charm for most of it. This is my constructor:

/**
 * Constructor. The instance ID is passed to this.
 */
public function __construct( $instance_id = 0 ) 
{
    $this->id                 = 'my_shipping_id';
    $this->instance_id        = absint($instance_id);
    $this->method_title       = __('My shipping title');
    $this->method_description = __('shipping description');
    $this->supports = array(
        'shipping-zones',
        'instance-settings',
    );
    $this->instance_form_fields = array(
        'enabled' => array(
            'title'   => __('Enable'),
            'type'    => 'checkbox',
            'label'   => __('Enable this shipping method'),
            'default' => 'yes',
        ),
    );
    $this->enabled = $this->get_option('enabled');
    $this->title   = 'My shipping title';

    add_action('woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options'));
}

Then I have the calculate_shipping method. This method calls our API for possible shipping options and rates and these are added to Woo through $this->add_rate()

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

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

So far this works like a charm. The checkout shows all options and I can place orders and the backend shows the right shipping title. This is where it goes wrong. The order is stored with the shipping ID from the constructor class, but with the title from the $this->add_rate() function. Therefore all shipping methods coming from the API look the same in the DB.

I would like to process this later automatically by selecting the shipment ID so we know which method to use.

Did I take the wrong turn somewhere and is this not possible? Or am I missing something?

0
, Mike 7 months 0 Answers 95 views 0

Leave an answer