• Resolved Ben Huson

    (@husobj)


    I have a custom shipping method (extends WC_Shipping_Method) in which I add an instance form field called “distributor” by setting $this->instance_form_fields.

    This works fine and I can setup the shipping method in my shipping zones, and set a different distributor for each.

    Later, when an order is processed I want to be able to retrieve the “distributor” for the order. I can hook into woocommerce_order_status_changed and retrieve the shipping method for the order using $order->get_shipping_methods() which return an instance of WC_Order_Item_Shipping.

    From this I can get the shipping method ID $method->get_method_id() and shipping method instance ID $method->get_instance_id()

    However, I’m now stuck how to get the correct instance of the custom shipping method class so I can retrieve the ‘distributor’ value for that shipping method?

    Example code so far:

    function my_woocommerce_order_status_changed( $order_id, $from_status, $to_status, $order ) {
    	$methods = is_callable( array( $order, 'get_shipping_methods' ) ) ? $order->get_shipping_methods() : array();
    	foreach ( $methods as $method ) {
    		if ( 'my_custom_shipping_method' == $method->get_method_id() ) {
    			$instance_id = $method->get_instance_id();
    			// How to get the shipping method instance to retrieve field value
    		}
    	}
    }
    add_action( 'woocommerce_order_status_changed', 'my_woocommerce_order_status_changed', 10, 4 );

    Can anyone point me in the right direction?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @husobj,

    Thanks for reaching out on the forums! This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    Im the meantime, I recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well 🙂

    Thread Starter Ben Huson

    (@husobj)

    Thanks – yes, more of a complex development question 😉
    I think I may have figure this out…

    
    // Get names of all the shipping classes
    $shipping_class_names = WC()->shipping->get_shipping_method_class_names();
    
    // Create an instance of the shipping method passing the instance ID
    $method_instance = new $shipping_class_names['my_custom_shipping_method']( $instance_id );
    
    // Get the field value from my shipping instance
    $field_value = $method_instance->get_option( 'my_custom_shipping_field_key' );
    
    Plugin Support laceyrod a11n

    (@laceyrod)

    Automattic Happiness Engineer

    Hi @husobj

    I’m glad to hear you’ve gotten this figured out, and thanks for sharing what you’ve found in case it helps anyone in the future.

    I’m going to mark this thread as Resolved now, but please feel free to open a new one if you have any further questions.

    Cheers!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to get Shipping Method instance field value from Instance ID?’ is closed to new replies.