Skip to main content
deleted 1089 characters in body; edited title
Source Link
Mike
  • 161
  • 1
  • 9

Custom Add multiple shipping IDrates from add_rate function WooCommerce not stored in DBwith custom ID

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?

Custom shipping ID from add_rate function WooCommerce not stored in DB

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?

Add multiple shipping rates from add_rate function with custom ID

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?

Source Link
Mike
  • 161
  • 1
  • 9

Custom shipping ID from add_rate function WooCommerce not stored in DB

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?