• Resolved Luke Breen

    (@origami-penguin)


    Since WooCommerce 3.4 the “shipping_method” and “instance_id” have been split, whereas before they were one colon separated value (ie table_rate:9). This is now causing some problems when I try to generate some custom meta data for the order based on the selected shipping rule.

    Just to note, I’m using the WooCommerce Table Rate Plugin which is why my shipping_methods all display as “table_rate”.

    This is the code that was working prior to WooCommerce 3.4

    
    add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2);
    function before_checkout_create_order( $order, $data ) {
        if( $order->has_shipping_method('table_rate:9' ) { 
        $order->update_meta_data( 'royal_mail_shipping_code', 'SD1' );
        }
    	if( $order->has_shipping_method('table_rate:8') ) { 
        $order->update_meta_data( 'royal_mail_shipping_code', 'TPS48' );
        }
        else $order->update_meta_data( 'royal_mail_shipping_code', 'No Value' );
    }
    

    After WooCommerce 3.4 the “shipping_method” returns “table_rate” rather than “table_rate:9” so I now need a way to check the order shipping “instance_id” instead, but I can’t find any documentation on how to do this.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support John Coy a11n

    (@johndcoy)

    Automattic Happiness Engineer

    Hi @origami-penguin

    You’ll likely want to adjust the code to get the selected method_id & instance_id using get_instance_id() and get_method_id(). We can’t with how to do that here but this API doc may help:

    https://docs.woocommerce.com/wc-apidocs/class-WC_Shipping_Method.html

    Thanks,

    Thread Starter Luke Breen

    (@origami-penguin)

    Hi John,

    Thanks for getting back to me.

    As both of my shipping rules use the Method “table_rate”, I’m guessing all I need to do is check the “instance_id” in my ‘if’ statement. I’ve tried the following, but it doesn’t seem to work…

    add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2);
    function before_checkout_create_order( $order, $data ) {
        if( $order->get_instance_id('9' ) { 
        $order->update_meta_data( 'royal_mail_shipping_code', 'SD1' );
        }
    	if( $order->get_instance_id('8') ) { 
        $order->update_meta_data( 'royal_mail_shipping_code', 'TPS48' );
        }
        else $order->update_meta_data( 'royal_mail_shipping_code', 'No Value' );
    }

    Any further help with this would be hugely appreciated.

    Thread Starter Luke Breen

    (@origami-penguin)

    Further to this I have also tried the following…

    add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2);
    function before_checkout_create_order( $order, $data ) {
    $method_id = $order->get_method_id();
    $instance_id = $order->get_instance_id();
    if( $instance_id = '9' ) {
    $order->update_meta_data( 'royal_mail_shipping_code', 'SD1' );
    }
    if( $instance_id = '8' ) {
    $order->update_meta_data( 'royal_mail_shipping_code', 'TPS48' );
    }
    else $order->update_meta_data( 'royal_mail_shipping_code', 'No Value' );
    } 

    This resulted in the error “SyntaxError: Unexpected token F in JSON at position 1”
    appearing when I try and place a test order.

    I’m obviously no PHP developer, so any further help with this would be hugely appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Using instance_id in WooCommerce 3.4+ to define the chosen shipping method’ is closed to new replies.