diff --git a/includes/class-wc-order-item-shipping.php b/includes/class-wc-order-item-shipping.php index fbd3040aa73..00720ca77c3 100644 --- a/includes/class-wc-order-item-shipping.php +++ b/includes/class-wc-order-item-shipping.php @@ -25,6 +25,7 @@ class WC_Order_Item_Shipping extends WC_Order_Item { protected $extra_data = array( 'method_title' => '', 'method_id' => '', + 'instance_id' => '', 'total' => 0, 'total_tax' => 0, 'taxes' => array( @@ -93,6 +94,16 @@ class WC_Order_Item_Shipping extends WC_Order_Item { $this->set_prop( 'method_id', wc_clean( $value ) ); } + /** + * Set shipping instance id. + * + * @param string $value Value to set. + * @throws WC_Data_Exception May throw exception if data is invalid. + */ + public function set_instance_id( $value ) { + $this->set_prop( 'instance_id', wc_clean( $value ) ); + } + /** * Set total. * @@ -142,10 +153,11 @@ class WC_Order_Item_Shipping extends WC_Order_Item { * @param WC_Shipping_Rate $shipping_rate Shipping rate to set. */ public function set_shipping_rate( $shipping_rate ) { - $this->set_method_title( $shipping_rate->label ); - $this->set_method_id( $shipping_rate->id ); - $this->set_total( $shipping_rate->cost ); - $this->set_taxes( $shipping_rate->taxes ); + $this->set_method_title( $shipping_rate->get_label() ); + $this->set_method_id( $shipping_rate->get_method_id() ); + $this->set_instance_id( $shipping_rate->get_instance_id() ); + $this->set_total( $shipping_rate->get_cost() ); + $this->set_taxes( $shipping_rate->get_taxes() ); $this->set_meta_data( $shipping_rate->get_meta_data() ); } @@ -199,6 +211,16 @@ class WC_Order_Item_Shipping extends WC_Order_Item { return $this->get_prop( 'method_id', $context ); } + /** + * Get instance ID. + * + * @param string $context View or edit context. + * @return string + */ + public function get_instance_id( $context = 'view' ) { + return $this->get_prop( 'instance_id', $context ); + } + /** * Get total cost. * diff --git a/includes/data-stores/class-wc-order-item-shipping-data-store.php b/includes/data-stores/class-wc-order-item-shipping-data-store.php index ed98e1191ef..76708a1adfe 100644 --- a/includes/data-stores/class-wc-order-item-shipping-data-store.php +++ b/includes/data-stores/class-wc-order-item-shipping-data-store.php @@ -21,7 +21,7 @@ class WC_Order_Item_Shipping_Data_Store extends Abstract_WC_Order_Item_Type_Data * @since 3.0.0 * @var array */ - protected $internal_meta_keys = array( 'method_id', 'cost', 'total_tax', 'taxes' ); + protected $internal_meta_keys = array( 'method_id', 'instance_id', 'cost', 'total_tax', 'taxes' ); /** * Read/populate data properties specific to this order item. @@ -33,9 +33,10 @@ class WC_Order_Item_Shipping_Data_Store extends Abstract_WC_Order_Item_Type_Data parent::read( $item ); $id = $item->get_id(); $item->set_props( array( - 'method_id' => get_metadata( 'order_item', $id, 'method_id', true ), - 'total' => get_metadata( 'order_item', $id, 'cost', true ), - 'taxes' => get_metadata( 'order_item', $id, 'taxes', true ), + 'method_id' => get_metadata( 'order_item', $id, 'method_id', true ), + 'instance_id' => get_metadata( 'order_item', $id, 'instance_id', true ), + 'total' => get_metadata( 'order_item', $id, 'cost', true ), + 'taxes' => get_metadata( 'order_item', $id, 'taxes', true ), ) ); $item->set_object_read( true ); } @@ -50,10 +51,11 @@ class WC_Order_Item_Shipping_Data_Store extends Abstract_WC_Order_Item_Type_Data public function save_item_data( &$item ) { $id = $item->get_id(); $save_values = array( - 'method_id' => $item->get_method_id( 'edit' ), - 'cost' => $item->get_total( 'edit' ), - 'total_tax' => $item->get_total_tax( 'edit' ), - 'taxes' => $item->get_taxes( 'edit' ), + 'method_id' => $item->get_method_id( 'edit' ), + 'instance_id' => $item->get_instance_id( 'edit' ), + 'cost' => $item->get_total( 'edit' ), + 'total_tax' => $item->get_total_tax( 'edit' ), + 'taxes' => $item->get_taxes( 'edit' ), ); foreach ( $save_values as $key => $value ) { update_metadata( 'order_item', $id, $key, $value );