Merge pull request #18483 from woocommerce/update/16912

Set shipping method ID and instance ID
This commit is contained in:
Mike Jolley 2018-03-01 16:46:38 +00:00 committed by GitHub
commit 1f16ad0a03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 102 additions and 57 deletions

View File

@ -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,

View File

@ -1,26 +1,31 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Order Line Item (shipping).
*
* @version 3.0.0
* @since 3.0.0
* @package WooCommerce/Classes
* @author WooThemes
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WC_Order_Item_Shipping class.
*/
class WC_Order_Item_Shipping extends WC_Order_Item {
/**
* Order Data array. This is the core order data exposed in APIs since 3.0.0.
*
* @since 3.0.0
* @var array
*/
protected $extra_data = array(
'method_title' => '',
'method_id' => '',
'instance_id' => '',
'total' => 0,
'total_tax' => 0,
'taxes' => array(
@ -61,8 +66,8 @@ class WC_Order_Item_Shipping extends WC_Order_Item {
/**
* Set order item name.
*
* @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_name( $value ) {
$this->set_method_title( $value );
@ -71,8 +76,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,18 +87,28 @@ 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 ) );
}
/**
* 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.
*
* @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 +117,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 +128,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,13 +150,14 @@ 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 );
$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() );
}
@ -162,7 +179,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 +189,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,17 +204,27 @@ 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' ) {
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.
*
* @param string $context
* @param string $context View or edit context.
* @return string
*/
public function get_total( $context = 'view' ) {
@ -207,7 +234,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 +244,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 +254,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 +271,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 +285,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 +299,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 );

View File

@ -1,38 +1,51 @@
<?php
/**
* WC Order Item Shipping Data Store
*
* @version 3.0.0
* @package data-stores
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WC Order Item Shipping Data Store
*
* @version 3.0.0
* @category Class
* @author WooCommerce
* WC_Order_Item_Shipping_Data_Store class.
*/
class WC_Order_Item_Shipping_Data_Store extends Abstract_WC_Order_Item_Type_Data_Store implements WC_Object_Data_Store_Interface, WC_Order_Item_Type_Data_Store_Interface {
/**
* Data stored in meta keys.
*
* @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.
*
* @since 3.0.0
* @param WC_Order_Item_Shipping $item
* @param WC_Order_Item_Shipping $item Item to read to.
*/
public function read( &$item ) {
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 ),
) );
// 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 );
}
@ -41,18 +54,19 @@ class WC_Order_Item_Shipping_Data_Store extends Abstract_WC_Order_Item_Type_Data
* Ran after both create and update, so $id will be set.
*
* @since 3.0.0
* @param WC_Order_Item_Shipping $item
* @param WC_Order_Item_Shipping $item Item to save.
*/
public function save_item_data( &$item ) {
$id = $item->get_id();
$changes = $item->get_changes();
$meta_key_to_props = array(
'method_id' => 'method_id',
'cost' => 'total',
'total_tax' => 'total_tax',
'taxes' => 'taxes',
'method_id' => 'method_id',
'instance_id' => 'instance_id',
'cost' => 'total',
'total_tax' => 'total_tax',
'taxes' => 'taxes',
);
$props_to_update = $this->get_props_to_update( $item, $meta_key_to_props, 'order_item' );
$props_to_update = $this->get_props_to_update( $item, $meta_key_to_props, 'order_item' );
foreach ( $props_to_update as $meta_key => $prop ) {
update_metadata( 'order_item', $id, $meta_key, $item->{"get_$prop"}( 'edit' ) );