From 58efd51a9ccf10abb2fe97e13033c3e6f7569783 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 15 Jan 2018 12:24:05 +0000 Subject: [PATCH 1/4] phpcs --- ...class-wc-order-item-shipping-data-store.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) 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 48cd5b8c53d..ed98e1191ef 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 @@ -1,19 +1,23 @@ get_id(); From ad18445eeb396ea97a5185906dc7235a4beee2de Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 15 Jan 2018 14:28:08 +0000 Subject: [PATCH 2/4] phpcs --- includes/class-wc-order-item-shipping.php | 80 +++++++++++++---------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/includes/class-wc-order-item-shipping.php b/includes/class-wc-order-item-shipping.php index 3ae4f957975..fbd3040aa73 100644 --- a/includes/class-wc-order-item-shipping.php +++ b/includes/class-wc-order-item-shipping.php @@ -1,20 +1,24 @@ set_method_title( $value ); @@ -71,8 +75,8 @@ class WC_Order_Item_Shipping extends WC_Order_Item { /** * Set method title. * - * @param string $value - * @throws WC_Data_Exception + * @param string $value Value to set. + * @throws WC_Data_Exception May throw exception if data is invalid. */ public function set_method_title( $value ) { $this->set_prop( 'name', wc_clean( $value ) ); @@ -82,8 +86,8 @@ class WC_Order_Item_Shipping extends WC_Order_Item { /** * Set shipping method id. * - * @param string $value - * @throws WC_Data_Exception + * @param string $value Value to set. + * @throws WC_Data_Exception May throw exception if data is invalid. */ public function set_method_id( $value ) { $this->set_prop( 'method_id', wc_clean( $value ) ); @@ -92,8 +96,8 @@ class WC_Order_Item_Shipping extends WC_Order_Item { /** * Set total. * - * @param string $value - * @throws WC_Data_Exception + * @param string $value Value to set. + * @throws WC_Data_Exception May throw exception if data is invalid. */ public function set_total( $value ) { $this->set_prop( 'total', wc_format_decimal( $value ) ); @@ -102,8 +106,8 @@ class WC_Order_Item_Shipping extends WC_Order_Item { /** * Set total tax. * - * @param string $value - * @throws WC_Data_Exception + * @param string $value Value to set. + * @throws WC_Data_Exception May throw exception if data is invalid. */ protected function set_total_tax( $value ) { $this->set_prop( 'total_tax', wc_format_decimal( $value ) ); @@ -113,19 +117,20 @@ class WC_Order_Item_Shipping extends WC_Order_Item { * Set taxes. * * This is an array of tax ID keys with total amount values. - * @param array $raw_tax_data - * @throws WC_Data_Exception + * + * @param array $raw_tax_data Value to set. + * @throws WC_Data_Exception May throw exception if data is invalid. */ public function set_taxes( $raw_tax_data ) { $raw_tax_data = maybe_unserialize( $raw_tax_data ); $tax_data = array( - 'total' => array(), + 'total' => array(), ); if ( isset( $raw_tax_data['total'] ) ) { - $tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data['total'] ); + $tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data['total'] ); } elseif ( ! empty( $raw_tax_data ) && is_array( $raw_tax_data ) ) { // Older versions just used an array. - $tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data ); + $tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data ); } $this->set_prop( 'taxes', $tax_data ); $this->set_total_tax( array_sum( $tax_data['total'] ) ); @@ -134,7 +139,7 @@ class WC_Order_Item_Shipping extends WC_Order_Item { /** * Set properties based on passed in shipping rate object. * - * @param WC_Shipping_Rate $shipping_rate + * @param WC_Shipping_Rate $shipping_rate Shipping rate to set. */ public function set_shipping_rate( $shipping_rate ) { $this->set_method_title( $shipping_rate->label ); @@ -162,7 +167,7 @@ class WC_Order_Item_Shipping extends WC_Order_Item { /** * Get order item name. * - * @param string $context + * @param string $context View or edit context. * @return string */ public function get_name( $context = 'view' ) { @@ -172,7 +177,7 @@ class WC_Order_Item_Shipping extends WC_Order_Item { /** * Get title. * - * @param string $context + * @param string $context View or edit context. * @return string */ public function get_method_title( $context = 'view' ) { @@ -187,7 +192,7 @@ class WC_Order_Item_Shipping extends WC_Order_Item { /** * Get method ID. * - * @param string $context + * @param string $context View or edit context. * @return string */ public function get_method_id( $context = 'view' ) { @@ -197,7 +202,7 @@ class WC_Order_Item_Shipping extends WC_Order_Item { /** * Get total cost. * - * @param string $context + * @param string $context View or edit context. * @return string */ public function get_total( $context = 'view' ) { @@ -207,7 +212,7 @@ class WC_Order_Item_Shipping extends WC_Order_Item { /** * Get total tax. * - * @param string $context + * @param string $context View or edit context. * @return string */ public function get_total_tax( $context = 'view' ) { @@ -217,7 +222,7 @@ class WC_Order_Item_Shipping extends WC_Order_Item { /** * Get taxes. * - * @param string $context + * @param string $context View or edit context. * @return array */ public function get_taxes( $context = 'view' ) { @@ -227,7 +232,7 @@ class WC_Order_Item_Shipping extends WC_Order_Item { /** * Get tax class. * - * @param string $context + * @param string $context View or edit context. * @return string */ public function get_tax_class( $context = 'view' ) { @@ -244,9 +249,10 @@ class WC_Order_Item_Shipping extends WC_Order_Item { */ /** - * offsetGet for ArrayAccess/Backwards compatibility. + * Offset get: for ArrayAccess/Backwards compatibility. + * * @deprecated Add deprecation notices in future release. - * @param string $offset + * @param string $offset Key. * @return mixed */ public function offsetGet( $offset ) { @@ -257,10 +263,11 @@ class WC_Order_Item_Shipping extends WC_Order_Item { } /** - * offsetSet for ArrayAccess/Backwards compatibility. + * Offset set: for ArrayAccess/Backwards compatibility. + * * @deprecated Add deprecation notices in future release. - * @param string $offset - * @param mixed $value + * @param string $offset Key. + * @param mixed $value Value to set. */ public function offsetSet( $offset, $value ) { if ( 'cost' === $offset ) { @@ -270,12 +277,13 @@ class WC_Order_Item_Shipping extends WC_Order_Item { } /** - * offsetExists for ArrayAccess - * @param string $offset + * Offset exists: for ArrayAccess. + * + * @param string $offset Key. * @return bool */ public function offsetExists( $offset ) { - if ( in_array( $offset, array( 'cost' ) ) ) { + if ( in_array( $offset, array( 'cost' ), true ) ) { return true; } return parent::offsetExists( $offset ); From 054e68a0477940db6e5b8aaa926e4989a623d596 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 15 Jan 2018 14:31:05 +0000 Subject: [PATCH 3/4] Instance ID methods --- includes/class-wc-order-item-shipping.php | 30 ++++++++++++++++--- ...lass-wc-order-item-shipping-data-store.php | 18 ++++++----- 2 files changed, 36 insertions(+), 12 deletions(-) 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 ); From c64c726da9d0ce3761b7457d5e389c72836015c3 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 15 Jan 2018 14:49:55 +0000 Subject: [PATCH 4/4] Set instance id --- includes/class-wc-checkout.php | 3 ++- .../class-wc-order-item-shipping-data-store.php | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/includes/class-wc-checkout.php b/includes/class-wc-checkout.php index a39595b95c0..4b12acbf0c3 100644 --- a/includes/class-wc-checkout.php +++ b/includes/class-wc-checkout.php @@ -439,7 +439,8 @@ class WC_Checkout { $item->legacy_package_key = $package_key; // @deprecated For legacy actions. $item->set_props( array( 'method_title' => $shipping_rate->label, - 'method_id' => $shipping_rate->id, + 'method_id' => $shipping_rate->method_id, + 'instance_id' => $shipping_rate->instance_id, 'total' => wc_format_decimal( $shipping_rate->cost ), 'taxes' => array( 'total' => $shipping_rate->taxes, 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 76708a1adfe..a0cf9d1f6b3 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 @@ -38,6 +38,14 @@ class WC_Order_Item_Shipping_Data_Store extends Abstract_WC_Order_Item_Type_Data 'total' => get_metadata( 'order_item', $id, 'cost', true ), 'taxes' => get_metadata( 'order_item', $id, 'taxes', true ), ) ); + + // BW compat. + if ( '' === $item->get_instance_id() && strstr( $item->get_method_id(), ':' ) ) { + $legacy_method_id = explode( ':', $item->get_method_id() ); + $item->set_method_id( $legacy_method_id[0] ); + $item->set_instance_id( $legacy_method_id[1] ); + } + $item->set_object_read( true ); }