I have written my ownI've made a custom shipping method and hooked intoclass for WooCommerce. I've usedIn the shipping class fromcalculate_shipping function an API request is done to our server requesting the WooCommerce docs, works like a charm for most of itpossible shipping methods. This is my constructor:can result in one or multiple rates.
I have integrated this as follows in the calculate_shipping() function
/**
* 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 / =Make __('Mya shipping title');
$this->method_description = __('shipping description');
$this->supports = array(
'shipping-zones',
'instance-settings',
);
$this->instance_form_fields = array(
'enabled' => array(
'title' => __('Enable'),
'type' rate for =>each 'checkbox',result
'label' =>foreach __('Enable this shipping method'),
'default' => 'yes',
),
);
$this$quote->enabled>shipping_options =as $this->get_option('enabled'$option);
$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_' 'my_method_'. $option->method>id,
'label' => $methodNames[$option->method]>name],
'cost' => $option->price,
);
// Register the rate
$this->add_rate($rate);
}
So far this works like a charm. The checkout shows allThis displays the right options, uses the right rate and in the WooCommerce admin I can place orders and the backend shows the right shipping titlesee which method was used. This is where it goes wrongSo far, so good.
The issue is:
The order is stored with the shipping ID from the constructor class, but with the title from the $this->add_rate() function. Therefore allcustom shipping methods coming from the API lookclass and the same inlabel of the DBchosen rate.
How do I would like to process this later automatically by selecting the shipment ID so we knowrecognize programmatically which shipping method to use.
Didwas used? I take the wrong turn somewheredon't want to match on label for obvious reasons, and the ID is this not possible? Or amwrong. Can I missing somethingstore an identifier or change this behaviour?