How to get Shipping Method instance field value from Instance ID?
-
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 ofWC_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?
- The topic ‘How to get Shipping Method instance field value from Instance ID?’ is closed to new replies.