Set_props helper to load data from DB and handle errors
This commit is contained in:
parent
077f57f139
commit
25883f8687
|
@ -27,12 +27,6 @@ abstract class WC_Data {
|
|||
*/
|
||||
protected $_data = array();
|
||||
|
||||
/**
|
||||
* True when reading from the database.
|
||||
* @var bool
|
||||
*/
|
||||
protected $_reading = false;
|
||||
|
||||
/**
|
||||
* Stores meta in cache for future reads.
|
||||
* A group must be set to to enable caching.
|
||||
|
@ -397,6 +391,28 @@ abstract class WC_Data {
|
|||
return $prop;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a collection of props in one go, collect any errors, and return the result.
|
||||
* @param array $props Key value pairs to set. Key is the prop and should map to a setter function name.
|
||||
* @return WP_Error|bool
|
||||
*/
|
||||
public function set_props( $props ) {
|
||||
$errors = new WP_Error();
|
||||
|
||||
foreach ( $props as $prop => $value ) {
|
||||
try {
|
||||
$setter = "set_$prop";
|
||||
if ( ! is_null( $value ) && is_callable( array( $this, $setter ) ) ) {
|
||||
$this->{$setter}( wc_clean( wp_unslash( $value ) ) );
|
||||
}
|
||||
} catch ( WC_Data_Exception $e ) {
|
||||
$errors->add( $e->getErrorCode(), $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
return sizeof( $errors->get_error_codes() ) ? $errors : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set internal data prop to specified value.
|
||||
* @param int ...$param Prop keys followed by value to set.
|
||||
|
@ -404,7 +420,7 @@ abstract class WC_Data {
|
|||
*/
|
||||
protected function set_prop() {
|
||||
if ( func_num_args() < 2 ) {
|
||||
$this->invalid_data( 'invalid_value', 'set_prop requires at least 2 parameters' );
|
||||
$this->error( 'invalid_value', __( 'set_prop() requires at least 2 parameters', 'woocommerce' ) );
|
||||
}
|
||||
|
||||
$args = func_get_args();
|
||||
|
@ -421,12 +437,10 @@ abstract class WC_Data {
|
|||
/**
|
||||
* When invalid data is found, throw an exception unless reading from the DB.
|
||||
* @param string $error_code Error code.
|
||||
* @param string $data $error_message Error message.
|
||||
* @param string $error_message Error message.
|
||||
* @throws WC_Data_Exception
|
||||
*/
|
||||
protected function invalid_data( $error_code, $error_message ) {
|
||||
if ( ! $this->_reading ) {
|
||||
protected function error( $error_code, $error_message ) {
|
||||
throw new WC_Data_Exception( $error_code, $error_message );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -158,7 +158,7 @@ abstract class WC_Abstract_Legacy_Order extends WC_Data {
|
|||
}
|
||||
|
||||
$item->set_order_id( $this->get_id() );
|
||||
$item->set_all( $args );
|
||||
$item->set_props( $args );
|
||||
$item->save();
|
||||
do_action( 'woocommerce_order_edit_product', $this->get_id(), $item->get_id(), $args, $product );
|
||||
|
||||
|
@ -193,7 +193,7 @@ abstract class WC_Abstract_Legacy_Order extends WC_Data {
|
|||
}
|
||||
|
||||
$item->set_order_id( $this->get_id() );
|
||||
$item->set_all( $args );
|
||||
$item->set_props( $args );
|
||||
$item->save();
|
||||
|
||||
do_action( 'woocommerce_order_update_coupon', $this->get_id(), $item->get_id(), $args );
|
||||
|
@ -229,7 +229,7 @@ abstract class WC_Abstract_Legacy_Order extends WC_Data {
|
|||
}
|
||||
|
||||
$item->set_order_id( $this->get_id() );
|
||||
$item->set_all( $args );
|
||||
$item->set_props( $args );
|
||||
$item->save();
|
||||
$this->calculate_shipping();
|
||||
|
||||
|
@ -261,7 +261,7 @@ abstract class WC_Abstract_Legacy_Order extends WC_Data {
|
|||
}
|
||||
|
||||
$item->set_order_id( $this->get_id() );
|
||||
$item->set_all( $args );
|
||||
$item->set_props( $args );
|
||||
$item->save();
|
||||
|
||||
do_action( 'woocommerce_order_update_fee', $this->get_id(), $item->get_id(), $args );
|
||||
|
@ -292,7 +292,7 @@ abstract class WC_Abstract_Legacy_Order extends WC_Data {
|
|||
}
|
||||
|
||||
$item->set_order_id( $this->get_id() );
|
||||
$item->set_all( $args );
|
||||
$item->set_props( $args );
|
||||
$item->save();
|
||||
|
||||
do_action( 'woocommerce_order_update_tax', $this->get_id(), $item->get_id(), $args );
|
||||
|
|
|
@ -201,28 +201,24 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
|
|||
return;
|
||||
}
|
||||
|
||||
$this->_reading = true;
|
||||
// Map standard post data
|
||||
$this->set_id( $post_object->ID );
|
||||
$this->set_parent_id( $post_object->post_parent );
|
||||
$this->set_date_created( $post_object->post_date );
|
||||
$this->set_date_modified( $post_object->post_modified );
|
||||
$this->set_status( $post_object->post_status );
|
||||
$this->set_currency( get_post_meta( $this->get_id(), '_order_currency', true ) );
|
||||
$this->set_discount_total( get_post_meta( $this->get_id(), '_cart_discount', true ) );
|
||||
$this->set_discount_tax( get_post_meta( $this->get_id(), '_cart_discount_tax', true ) );
|
||||
$this->set_shipping_total( get_post_meta( $this->get_id(), '_order_shipping', true ) );
|
||||
$this->set_shipping_tax( get_post_meta( $this->get_id(), '_order_shipping_tax', true ) );
|
||||
$this->set_cart_tax( get_post_meta( $this->get_id(), '_order_tax', true ) );
|
||||
$this->set_total( get_post_meta( $this->get_id(), '_order_total', true ) );
|
||||
$this->set_version( get_post_meta( $this->get_id(), '_order_version', true ) );
|
||||
$this->set_props( array(
|
||||
'id' => $post_object->ID,
|
||||
'parent_id' => $post_object->post_parent,
|
||||
'date_created' => $post_object->post_date,
|
||||
'date_modified' => $post_object->post_modified,
|
||||
'status' => $post_object->post_status,
|
||||
'currency' => get_post_meta( $this->get_id(), '_order_currency', true ),
|
||||
'discount_total' => get_post_meta( $this->get_id(), '_cart_discount', true ),
|
||||
'discount_tax' => get_post_meta( $this->get_id(), '_cart_discount_tax', true ),
|
||||
'shipping_total' => get_post_meta( $this->get_id(), '_order_shipping', true ),
|
||||
'shipping_tax' => get_post_meta( $this->get_id(), '_order_shipping_tax', true ),
|
||||
'cart_tax' => get_post_meta( $this->get_id(), '_order_tax', true ),
|
||||
'total' => get_post_meta( $this->get_id(), '_order_total', true ),
|
||||
'version' => get_post_meta( $this->get_id(), '_order_version', true ),
|
||||
'prices_include_tax' => metadata_exists( 'post', $this->get_id(), '_prices_include_tax' ) ? 'yes' === get_post_meta( $this->get_id(), '_prices_include_tax', true ) : 'yes' === get_option( 'woocommerce_prices_include_tax' ),
|
||||
) );
|
||||
|
||||
// Orders store the state of prices including tax when created.
|
||||
$this->set_prices_include_tax( metadata_exists( 'post', $this->get_id(), '_prices_include_tax' ) ? 'yes' === get_post_meta( $this->get_id(), '_prices_include_tax', true ) : 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
|
||||
|
||||
// Load meta data
|
||||
$this->read_meta_data();
|
||||
$this->_reading = false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -609,7 +605,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
|
|||
*/
|
||||
public function set_parent_id( $value ) {
|
||||
if ( $value && ! get_post( $value ) ) {
|
||||
$this->invalid_data( 'order_invalid_parent_id', __( 'Invalid parent ID', 'woocommerce' ) );
|
||||
$this->error( 'order_invalid_parent_id', __( 'Invalid parent ID', 'woocommerce' ) );
|
||||
}
|
||||
$this->set_prop( 'parent_id', absint( $value ) );
|
||||
}
|
||||
|
@ -658,7 +654,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
|
|||
*/
|
||||
public function set_currency( $value ) {
|
||||
if ( $value && ! in_array( $value, array_keys( get_woocommerce_currencies() ) ) ) {
|
||||
$this->invalid_data( 'order_invalid_currency', __( 'Invalid currency code', 'woocommerce' ) );
|
||||
$this->error( 'order_invalid_currency', __( 'Invalid currency code', 'woocommerce' ) );
|
||||
}
|
||||
$this->set_prop( 'currency', $value );
|
||||
}
|
||||
|
@ -1176,8 +1172,6 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
|
|||
if ( $item->is_type( 'line_item' ) ) {
|
||||
$subtotal = $item->get_subtotal();
|
||||
$subtotal_taxes = WC_Tax::calc_tax( $subtotal, $tax_rates, false );
|
||||
$subtotal_tax = max( 0, array_sum( $subtotal_taxes ) );
|
||||
$item->set_subtotal_tax( $subtotal_tax );
|
||||
$item->set_taxes( array( 'total' => $taxes, 'subtotal' => $subtotal_taxes ) );
|
||||
} else {
|
||||
$item->set_taxes( array( 'total' => $taxes ) );
|
||||
|
|
|
@ -182,6 +182,15 @@ function wc_save_order_items( $order_id, $items ) {
|
|||
do_action( 'woocommerce_before_save_order_items', $order_id, $items );
|
||||
|
||||
$order = wc_get_order( $order_id );
|
||||
$data_keys = array(
|
||||
'line_tax' => array(),
|
||||
'line_subtotal_tax' => array(),
|
||||
'order_item_name' => '',
|
||||
'order_item_qty' => 1,
|
||||
'order_item_tax_class' => '',
|
||||
'line_total' => 0,
|
||||
'line_subtotal' => 0,
|
||||
);
|
||||
|
||||
// Line items and fees
|
||||
if ( isset( $items['order_item_id'] ) ) {
|
||||
|
@ -190,46 +199,39 @@ function wc_save_order_items( $order_id, $items ) {
|
|||
continue;
|
||||
}
|
||||
|
||||
$line_tax = isset( $items['line_tax'][ $item_id ] ) ? $items['line_tax'][ $item_id ] : array();
|
||||
$line_subtotal_tax = isset( $items['line_subtotal_tax'][ $item_id ] ) ? $items['line_subtotal_tax'][ $item_id ]: $line_tax;
|
||||
$set_data = array(
|
||||
'name' => isset( $items['order_item_name'][ $item_id ] ) ? $items['order_item_name'][ $item_id ] : null,
|
||||
'quantity' => isset( $items['order_item_qty'][ $item_id ] ) ? $items['order_item_qty'][ $item_id ] : null,
|
||||
'tax_class' => isset( $items['order_item_tax_class'][ $item_id ] ) ? $items['order_item_tax_class'][ $item_id ] : null,
|
||||
'total' => isset( $items['line_total'][ $item_id ] ) ? $items['line_total'][ $item_id ] : 0,
|
||||
'total_tax' => array_sum( $line_tax ),
|
||||
'subtotal' => isset( $items['line_subtotal'][ $item_id ] ) ? $items['line_subtotal'][ $item_id ] : $item->get_total(),
|
||||
'subtotal_tax' => array_sum( $line_subtotal_tax ),
|
||||
'taxes' => array( 'total' => $line_tax, 'subtotal' => $line_subtotal_tax ),
|
||||
);
|
||||
$item_data = array();
|
||||
|
||||
if ( '0' === $set_data['quantity'] ) {
|
||||
foreach ( $data_keys as $key => $default ) {
|
||||
$item_data[ $key ] = isset( $items[ $key ][ $item_id ] ) ? $items[ $key ][ $item_id ] : $default;
|
||||
}
|
||||
|
||||
if ( '0' === $item_data['order_item_qty'] ) {
|
||||
$item->delete();
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ( $set_data as $prop => $value ) {
|
||||
try {
|
||||
$setter = "set_$prop";
|
||||
if ( ! is_null( $value ) && is_callable( array( $item, $setter ) ) ) {
|
||||
$item->{$setter}( wc_clean( wp_unslash( $value ) ) );
|
||||
}
|
||||
} catch ( WC_Data_Exception $e ) {
|
||||
unset( $e ); // Skip prop and leave set to default
|
||||
}
|
||||
}
|
||||
$item->set_props( array(
|
||||
'name' => $item_data['order_item_name'],
|
||||
'quantity' => $item_data['order_item_qty'],
|
||||
'tax_class' => $item_data['order_item_tax_class'],
|
||||
'total' => $item_data['line_total'],
|
||||
'subtotal' => $item_data['line_subtotal'],
|
||||
'taxes' => array(
|
||||
'total' => $item_data['line_tax'],
|
||||
'subtotal' => $item_data['line_subtotal_tax'],
|
||||
),
|
||||
) );
|
||||
|
||||
if ( isset( $items['meta_key'][ $item_id ], $items['meta_value'][ $item_id ] ) ) {
|
||||
foreach ( $items['meta_key'][ $item_id ] as $meta_id => $meta_key ) {
|
||||
$meta_value = isset( $items['meta_value'][ $item_id ][ $meta_id ] ) ? $items['meta_value'][ $item_id ][ $meta_id ] : '';
|
||||
|
||||
if ( strstr( $meta_id, 'new-' ) ) {
|
||||
if ( $meta_key === '' && $meta_value === '' ) {
|
||||
continue;
|
||||
}
|
||||
$item->add_meta_data( $meta_key, $meta_value, false );
|
||||
} elseif ( $meta_key === '' && $meta_value === '' ) {
|
||||
if ( ! strstr( $meta_id, 'new-' ) ) {
|
||||
$item->delete_meta_data_by_mid( $meta_id );
|
||||
}
|
||||
} elseif ( strstr( $meta_id, 'new-' ) ) {
|
||||
$item->add_meta_data( $meta_key, $meta_value, false );
|
||||
} else {
|
||||
$item->update_meta_data( $meta_key, $meta_value, $meta_id );
|
||||
}
|
||||
|
|
|
@ -81,12 +81,15 @@ class WC_Order_Item_Coupon extends WC_Order_Item {
|
|||
*/
|
||||
public function read( $id ) {
|
||||
parent::read( $id );
|
||||
if ( $this->get_id() ) {
|
||||
$this->_reading = true;
|
||||
$this->set_discount( get_metadata( 'order_item', $this->get_id(), 'discount_amount', true ) );
|
||||
$this->set_discount_tax( get_metadata( 'order_item', $this->get_id(), 'discount_amount_tax', true ) );
|
||||
$this->_reading = false;
|
||||
|
||||
if ( ! $this->get_id() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->set_props( array(
|
||||
'discount' => get_metadata( 'order_item', $this->get_id(), 'discount_amount', true ),
|
||||
'discount_tax' => get_metadata( 'order_item', $this->get_id(), 'discount_amount_tax', true ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -90,15 +90,17 @@ class WC_Order_Item_Fee extends WC_Order_Item {
|
|||
*/
|
||||
public function read( $id ) {
|
||||
parent::read( $id );
|
||||
if ( $this->get_id() ) {
|
||||
$this->_reading = true;
|
||||
$this->set_tax_class( get_metadata( 'order_item', $this->get_id(), '_tax_class', true ) );
|
||||
$this->set_tax_status( get_metadata( 'order_item', $this->get_id(), '_tax_status', true ) );
|
||||
$this->set_total( get_metadata( 'order_item', $this->get_id(), '_line_total', true ) );
|
||||
$this->set_total_tax( get_metadata( 'order_item', $this->get_id(), '_line_tax', true ) );
|
||||
$this->set_taxes( get_metadata( 'order_item', $this->get_id(), '_line_tax_data', true ) );
|
||||
$this->_reading = false;
|
||||
|
||||
if ( ! $this->get_id() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->set_props( array(
|
||||
'tax_class' => get_metadata( 'order_item', $this->get_id(), '_tax_class', true ),
|
||||
'tax_status' => get_metadata( 'order_item', $this->get_id(), '_tax_status', true ),
|
||||
'total' => get_metadata( 'order_item', $this->get_id(), '_line_total', true ),
|
||||
'taxes' => get_metadata( 'order_item', $this->get_id(), '_line_tax_data', true ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -139,7 +141,7 @@ class WC_Order_Item_Fee extends WC_Order_Item {
|
|||
*/
|
||||
public function set_tax_class( $value ) {
|
||||
if ( $value && ! in_array( $value, WC_Tax::get_tax_classes() ) ) {
|
||||
$this->invalid_data( 'order_item_fee_invalid_tax_class', __( 'Invalid tax class', 'woocommerce' ) );
|
||||
$this->error( 'order_item_fee_invalid_tax_class', __( 'Invalid tax class', 'woocommerce' ) );
|
||||
}
|
||||
$this->set_prop( 'tax_class', $value );
|
||||
}
|
||||
|
@ -171,7 +173,7 @@ class WC_Order_Item_Fee extends WC_Order_Item {
|
|||
* @param string $value
|
||||
* @throws WC_Data_Exception
|
||||
*/
|
||||
public function set_total_tax( $value ) {
|
||||
protected function set_total_tax( $value ) {
|
||||
$this->set_prop( 'total_tax', wc_format_decimal( $value ) );
|
||||
}
|
||||
|
||||
|
@ -191,6 +193,7 @@ class WC_Order_Item_Fee extends WC_Order_Item {
|
|||
$tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data['total'] );
|
||||
}
|
||||
$this->set_prop( 'taxes', $tax_data );
|
||||
$this->set_total_tax( array_sum( $tax_data['total'] ) );
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -103,19 +103,20 @@ class WC_Order_Item_Product extends WC_Order_Item {
|
|||
*/
|
||||
public function read( $id ) {
|
||||
parent::read( $id );
|
||||
if ( $this->get_id() ) {
|
||||
$this->_reading = true;
|
||||
$this->set_product_id( get_metadata( 'order_item', $this->get_id(), '_product_id', true ) );
|
||||
$this->set_variation_id( get_metadata( 'order_item', $this->get_id(), '_variation_id', true ) );
|
||||
$this->set_quantity( get_metadata( 'order_item', $this->get_id(), '_qty', true ) );
|
||||
$this->set_tax_class( get_metadata( 'order_item', $this->get_id(), '_tax_class', true ) );
|
||||
$this->set_subtotal( get_metadata( 'order_item', $this->get_id(), '_line_subtotal', true ) );
|
||||
$this->set_subtotal_tax( get_metadata( 'order_item', $this->get_id(), '_line_subtotal_tax', true ) );
|
||||
$this->set_total( get_metadata( 'order_item', $this->get_id(), '_line_total', true ) );
|
||||
$this->set_total_tax( get_metadata( 'order_item', $this->get_id(), '_line_tax', true ) );
|
||||
$this->set_taxes( get_metadata( 'order_item', $this->get_id(), '_line_tax_data', true ) );
|
||||
$this->_reading = false;
|
||||
|
||||
if ( ! $this->get_id() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->set_props( array(
|
||||
'product_id' => get_metadata( 'order_item', $this->get_id(), '_product_id', true ),
|
||||
'variation_id' => get_metadata( 'order_item', $this->get_id(), '_variation_id', true ),
|
||||
'quantity' => get_metadata( 'order_item', $this->get_id(), '_qty', true ),
|
||||
'tax_class' => get_metadata( 'order_item', $this->get_id(), '_tax_class', true ),
|
||||
'subtotal' => get_metadata( 'order_item', $this->get_id(), '_line_subtotal', true ),
|
||||
'total' => get_metadata( 'order_item', $this->get_id(), '_line_total', true ),
|
||||
'taxes' => get_metadata( 'order_item', $this->get_id(), '_line_tax_data', true ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -245,7 +246,7 @@ class WC_Order_Item_Product extends WC_Order_Item {
|
|||
*/
|
||||
public function set_quantity( $value ) {
|
||||
if ( 0 >= $value ) {
|
||||
$this->invalid_data( 'order_item_product_invalid_quantity', __( 'Quantity must be positive', 'woocommerce' ) );
|
||||
$this->error( 'order_item_product_invalid_quantity', __( 'Quantity must be positive', 'woocommerce' ) );
|
||||
}
|
||||
$this->set_prop( 'quantity', wc_stock_amount( $value ) );
|
||||
}
|
||||
|
@ -257,7 +258,7 @@ class WC_Order_Item_Product extends WC_Order_Item {
|
|||
*/
|
||||
public function set_tax_class( $value ) {
|
||||
if ( $value && ! in_array( $value, WC_Tax::get_tax_classes() ) ) {
|
||||
$this->invalid_data( 'order_item_product_invalid_tax_class', __( 'Invalid tax class', 'woocommerce' ) );
|
||||
$this->error( 'order_item_product_invalid_tax_class', __( 'Invalid tax class', 'woocommerce' ) );
|
||||
}
|
||||
$this->set_prop( 'tax_class', $value );
|
||||
}
|
||||
|
@ -269,7 +270,7 @@ class WC_Order_Item_Product extends WC_Order_Item {
|
|||
*/
|
||||
public function set_product_id( $value ) {
|
||||
if ( $value > 0 && 'product' !== get_post_type( absint( $value ) ) ) {
|
||||
$this->invalid_data( 'order_item_product_invalid_product_id', __( 'Invalid product ID', 'woocommerce' ) );
|
||||
$this->error( 'order_item_product_invalid_product_id', __( 'Invalid product ID', 'woocommerce' ) );
|
||||
}
|
||||
$this->set_prop( 'product_id', absint( $value ) );
|
||||
}
|
||||
|
@ -281,7 +282,7 @@ class WC_Order_Item_Product extends WC_Order_Item {
|
|||
*/
|
||||
public function set_variation_id( $value ) {
|
||||
if ( $value > 0 && 'product_variation' !== get_post_type( $value ) ) {
|
||||
$this->invalid_data( 'order_item_product_invalid_variation_id', __( 'Invalid variation ID', 'woocommerce' ) );
|
||||
$this->error( 'order_item_product_invalid_variation_id', __( 'Invalid variation ID', 'woocommerce' ) );
|
||||
}
|
||||
$this->set_prop( 'variation_id', absint( $value ) );
|
||||
}
|
||||
|
@ -302,6 +303,11 @@ class WC_Order_Item_Product extends WC_Order_Item {
|
|||
*/
|
||||
public function set_total( $value ) {
|
||||
$this->set_prop( 'total', wc_format_decimal( $value ) );
|
||||
|
||||
// Subtotal cannot be less than total
|
||||
if ( empty( $this->get_subtotal() ) || $this->get_subtotal() < $this->get_total() ) {
|
||||
$this->set_subtotal( $value );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -309,7 +315,7 @@ class WC_Order_Item_Product extends WC_Order_Item {
|
|||
* @param string $value
|
||||
* @throws WC_Data_Exception
|
||||
*/
|
||||
public function set_subtotal_tax( $value ) {
|
||||
protected function set_subtotal_tax( $value ) {
|
||||
$this->set_prop( 'subtotal_tax', wc_format_decimal( $value ) );
|
||||
}
|
||||
|
||||
|
@ -318,12 +324,12 @@ class WC_Order_Item_Product extends WC_Order_Item {
|
|||
* @param string $value
|
||||
* @throws WC_Data_Exception
|
||||
*/
|
||||
public function set_total_tax( $value ) {
|
||||
protected function set_total_tax( $value ) {
|
||||
$this->set_prop( 'total_tax', wc_format_decimal( $value ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set line taxes.
|
||||
* Set line taxes and totals for passed in taxes.
|
||||
* @param array $raw_tax_data
|
||||
* @throws WC_Data_Exception
|
||||
*/
|
||||
|
@ -334,22 +340,27 @@ class WC_Order_Item_Product extends WC_Order_Item {
|
|||
'subtotal' => array()
|
||||
);
|
||||
if ( ! empty( $raw_tax_data['total'] ) && ! empty( $raw_tax_data['subtotal'] ) ) {
|
||||
$tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data['total'] );
|
||||
$tax_data['subtotal'] = array_map( 'wc_format_decimal', $raw_tax_data['subtotal'] );
|
||||
$tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data['total'] );
|
||||
|
||||
// Subtotal cannot be less than total!
|
||||
if ( array_sum( $tax_data['subtotal'] ) < array_sum( $tax_data['total'] ) ) {
|
||||
$tax_data['subtotal'] = $tax_data['total'];
|
||||
}
|
||||
}
|
||||
$this->set_prop( 'taxes', $tax_data );
|
||||
$this->set_total_tax( array_sum( $tax_data['total'] ) );
|
||||
$this->set_subtotal_tax( array_sum( $tax_data['subtotal'] ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set variation data (stored as meta data - write only).
|
||||
* @param array $data Key/Value pairs
|
||||
* @throws WC_Data_Exception
|
||||
*/
|
||||
public function set_variation( $data ) {
|
||||
foreach ( $data as $key => $value ) {
|
||||
$this->add_meta_data( str_replace( 'attribute_', '', $key ), $value, true );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -359,14 +370,13 @@ class WC_Order_Item_Product extends WC_Order_Item {
|
|||
*/
|
||||
public function set_product( $product ) {
|
||||
if ( ! is_a( $product, 'WC_Product' ) ) {
|
||||
$this->invalid_data( 'order_item_product_invalid_product', __( 'Invalid product', 'woocommerce' ) );
|
||||
$this->error( 'order_item_product_invalid_product', __( 'Invalid product', 'woocommerce' ) );
|
||||
}
|
||||
$this->set_product_id( $product->get_id() );
|
||||
$this->set_name( $product->get_title() );
|
||||
$this->set_tax_class( $product->get_tax_class() );
|
||||
$this->set_variation_id( is_callable( array( $product, 'get_variation_id' ) ) ? $product->get_variation_id() : 0 );
|
||||
$this->set_variation( is_callable( array( $product, 'get_variation_attributes' ) ) ? $product->get_variation_attributes() : array() );
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -81,14 +81,16 @@ class WC_Order_Item_Shipping extends WC_Order_Item {
|
|||
*/
|
||||
public function read( $id ) {
|
||||
parent::read( $id );
|
||||
if ( $this->get_id() ) {
|
||||
$this->_reading = true;
|
||||
$this->set_method_id( get_metadata( 'order_item', $this->get_id(), 'method_id', true ) );
|
||||
$this->set_total( get_metadata( 'order_item', $this->get_id(), 'cost', true ) );
|
||||
$this->set_total_tax( get_metadata( 'order_item', $this->get_id(), 'total_tax', true ) );
|
||||
$this->set_taxes( get_metadata( 'order_item', $this->get_id(), 'taxes', true ) );
|
||||
$this->_reading = false;
|
||||
|
||||
if ( ! $this->get_id() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->set_props( array(
|
||||
'method_id' => get_metadata( 'order_item', $this->get_id(), 'method_id', true ),
|
||||
'total' => get_metadata( 'order_item', $this->get_id(), 'cost', true ),
|
||||
'taxes' => get_metadata( 'order_item', $this->get_id(), 'taxes', true ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -162,7 +164,7 @@ class WC_Order_Item_Shipping extends WC_Order_Item {
|
|||
* @param string $value
|
||||
* @throws WC_Data_Exception
|
||||
*/
|
||||
public function set_total_tax( $value ) {
|
||||
protected function set_total_tax( $value ) {
|
||||
$this->set_prop( 'total_tax', wc_format_decimal( $value ) );
|
||||
}
|
||||
|
||||
|
|
|
@ -42,15 +42,18 @@ class WC_Order_Item_Tax extends WC_Order_Item {
|
|||
*/
|
||||
public function read( $id ) {
|
||||
parent::read( $id );
|
||||
if ( $this->get_id() ) {
|
||||
$this->_reading = true;
|
||||
$this->set_rate_id( get_metadata( 'order_item', $this->get_id(), 'rate_id', true ) );
|
||||
$this->set_label( get_metadata( 'order_item', $this->get_id(), 'label', true ) );
|
||||
$this->set_compound( get_metadata( 'order_item', $this->get_id(), 'compound', true ) );
|
||||
$this->set_tax_total( get_metadata( 'order_item', $this->get_id(), 'tax_amount', true ) );
|
||||
$this->set_shipping_tax_total( get_metadata( 'order_item', $this->get_id(), 'shipping_tax_amount', true ) );
|
||||
$this->_reading = false;
|
||||
|
||||
if ( ! $this->get_id() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->set_props( array(
|
||||
'rate_id' => get_metadata( 'order_item', $this->get_id(), 'rate_id', true ),
|
||||
'label' => get_metadata( 'order_item', $this->get_id(), 'label', true ),
|
||||
'compound' => get_metadata( 'order_item', $this->get_id(), 'compound', true ),
|
||||
'tax_total' => get_metadata( 'order_item', $this->get_id(), 'tax_amount', true ),
|
||||
'shipping_tax_total' => get_metadata( 'order_item', $this->get_id(), 'shipping_tax_amount', true ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -64,28 +64,15 @@ class WC_Order_Item extends WC_Data implements ArrayAccess {
|
|||
$this->set_defaults();
|
||||
if ( $item instanceof WC_Order_Item ) {
|
||||
if ( $this->is_type( $item->get_type() ) ) {
|
||||
$this->set_all( $item->get_data() );
|
||||
$this->set_props( $item->get_data() );
|
||||
}
|
||||
} elseif ( is_array( $item ) ) {
|
||||
$this->set_all( $item );
|
||||
$this->set_props( $item );
|
||||
} else {
|
||||
$this->read( $item );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set all data based on input array.
|
||||
* @param array $data
|
||||
* @access private
|
||||
*/
|
||||
public function set_all( $data ) {
|
||||
foreach ( $data as $key => $value ) {
|
||||
if ( is_callable( array( $this, "set_$key" ) ) ) {
|
||||
$this->{"set_$key"}( $value );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Type checking
|
||||
* @param string|array $Type
|
||||
|
@ -254,15 +241,17 @@ class WC_Order_Item extends WC_Data implements ArrayAccess {
|
|||
$data = false;
|
||||
}
|
||||
|
||||
if ( $data ) {
|
||||
$this->_reading = true;
|
||||
$this->set_order_id( $data->order_id );
|
||||
$this->set_id( $data->order_item_id );
|
||||
$this->set_name( $data->order_item_name );
|
||||
$this->set_type( $data->order_item_type );
|
||||
$this->read_meta_data();
|
||||
$this->_reading = false;
|
||||
if ( ! $data ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->set_props( array(
|
||||
'order_id' => $data->order_id,
|
||||
'id' => $data->order_item_id,
|
||||
'name' => $data->order_item_name,
|
||||
'type' => $data->order_item_type,
|
||||
) );
|
||||
$this->read_meta_data();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -63,19 +63,17 @@ class WC_Order_Refund extends WC_Abstract_Order {
|
|||
public function read( $id ) {
|
||||
parent::read( $id );
|
||||
|
||||
// Read additonal order data
|
||||
if ( $this->get_id() ) {
|
||||
$this->_reading = true;
|
||||
$post_object = get_post( $id );
|
||||
$this->set_amount( get_post_meta( $this->get_id(), '_refund_amount', true ) );
|
||||
|
||||
// post_author was used before refunded_by meta.
|
||||
$this->set_refunded_by( metadata_exists( 'post', $this->get_id(), '_refunded_by' ) ? get_post_meta( $this->get_id(), '_refunded_by', true ) : absint( $post_object->post_author ) );
|
||||
|
||||
// post_excerpt was used before refund_reason meta.
|
||||
$this->set_reason( metadata_exists( 'post', $this->get_id(), '_refund_reason' ) ? get_post_meta( $this->get_id(), '_refund_reason', true ) : absint( $post_object->post_excerpt ) );
|
||||
$this->_reading = false;
|
||||
if ( ! $this->get_id() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$post_object = get_post( $id );
|
||||
|
||||
$this->set_props( array(
|
||||
'amount' => get_post_meta( $this->get_id(), '_refund_amount', true ),
|
||||
'refunded_by' => metadata_exists( 'post', $this->get_id(), '_refunded_by' ) ? get_post_meta( $this->get_id(), '_refunded_by', true ) : absint( $post_object->post_author ),
|
||||
'reason' => metadata_exists( 'post', $this->get_id(), '_refund_reason' ) ? get_post_meta( $this->get_id(), '_refund_reason', true ) : $post_object->post_excerpt,
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -271,45 +271,45 @@ class WC_Order extends WC_Abstract_Order {
|
|||
return;
|
||||
}
|
||||
|
||||
$this->_reading = true;
|
||||
$post_object = get_post( $this->get_id() );
|
||||
|
||||
// Read additonal order data
|
||||
$this->set_order_key( get_post_meta( $this->get_id(), '_order_key', true ) );
|
||||
$this->set_customer_id( get_post_meta( $this->get_id(), '_customer_user', true ) );
|
||||
$this->set_billing_first_name( get_post_meta( $this->get_id(), '_billing_first_name', true ) );
|
||||
$this->set_billing_last_name( get_post_meta( $this->get_id(), '_billing_last_name', true ) );
|
||||
$this->set_billing_company( get_post_meta( $this->get_id(), '_billing_company', true ) );
|
||||
$this->set_billing_address_1( get_post_meta( $this->get_id(), '_billing_address_1', true ) );
|
||||
$this->set_billing_address_2( get_post_meta( $this->get_id(), '_billing_address_2', true ) );
|
||||
$this->set_billing_city( get_post_meta( $this->get_id(), '_billing_city', true ) );
|
||||
$this->set_billing_state( get_post_meta( $this->get_id(), '_billing_state', true ) );
|
||||
$this->set_billing_postcode( get_post_meta( $this->get_id(), '_billing_postcode', true ) );
|
||||
$this->set_billing_country( get_post_meta( $this->get_id(), '_billing_country', true ) );
|
||||
$this->set_billing_email( get_post_meta( $this->get_id(), '_billing_email', true ) );
|
||||
$this->set_billing_phone( get_post_meta( $this->get_id(), '_billing_phone', true ) );
|
||||
$this->set_shipping_first_name( get_post_meta( $this->get_id(), '_shipping_first_name', true ) );
|
||||
$this->set_shipping_last_name( get_post_meta( $this->get_id(), '_shipping_last_name', true ) );
|
||||
$this->set_shipping_company( get_post_meta( $this->get_id(), '_shipping_company', true ) );
|
||||
$this->set_shipping_address_1( get_post_meta( $this->get_id(), '_shipping_address_1', true ) );
|
||||
$this->set_shipping_address_2( get_post_meta( $this->get_id(), '_shipping_address_2', true ) );
|
||||
$this->set_shipping_city( get_post_meta( $this->get_id(), '_shipping_city', true ) );
|
||||
$this->set_shipping_state( get_post_meta( $this->get_id(), '_shipping_state', true ) );
|
||||
$this->set_shipping_postcode( get_post_meta( $this->get_id(), '_shipping_postcode', true ) );
|
||||
$this->set_shipping_country( get_post_meta( $this->get_id(), '_shipping_country', true ) );
|
||||
$this->set_payment_method( get_post_meta( $this->get_id(), '_payment_method', true ) );
|
||||
$this->set_payment_method_title( get_post_meta( $this->get_id(), '_payment_method_title', true ) );
|
||||
$this->set_transaction_id( get_post_meta( $this->get_id(), '_transaction_id', true ) );
|
||||
$this->set_customer_ip_address( get_post_meta( $this->get_id(), '_customer_ip_address', true ) );
|
||||
$this->set_customer_user_agent( get_post_meta( $this->get_id(), '_customer_user_agent', true ) );
|
||||
$this->set_created_via( get_post_meta( $this->get_id(), '_created_via', true ) );
|
||||
$this->set_customer_note( get_post_meta( $this->get_id(), '_customer_note', true ) );
|
||||
$this->set_date_completed( get_post_meta( $this->get_id(), '_completed_date', true ) );
|
||||
$this->set_date_paid( get_post_meta( $this->get_id(), '_paid_date', true ) );
|
||||
$this->set_cart_hash( get_post_meta( $this->get_id(), '_cart_hash', true ) );
|
||||
$this->set_customer_note( $post_object->post_excerpt );
|
||||
$this->set_props( array(
|
||||
'order_key' => get_post_meta( $this->get_id(), '_order_key', true ),
|
||||
'customer_id' => get_post_meta( $this->get_id(), '_customer_user', true ),
|
||||
'billing_first_name' => get_post_meta( $this->get_id(), '_billing_first_name', true ),
|
||||
'billing_last_name' => get_post_meta( $this->get_id(), '_billing_last_name', true ),
|
||||
'billing_company' => get_post_meta( $this->get_id(), '_billing_company', true ),
|
||||
'billing_address_1' => get_post_meta( $this->get_id(), '_billing_address_1', true ),
|
||||
'billing_address_2' => get_post_meta( $this->get_id(), '_billing_address_2', true ),
|
||||
'billing_city' => get_post_meta( $this->get_id(), '_billing_city', true ),
|
||||
'billing_state' => get_post_meta( $this->get_id(), '_billing_state', true ),
|
||||
'billing_postcode' => get_post_meta( $this->get_id(), '_billing_postcode', true ),
|
||||
'billing_country' => get_post_meta( $this->get_id(), '_billing_country', true ),
|
||||
'billing_email' => get_post_meta( $this->get_id(), '_billing_email', true ),
|
||||
'billing_phone' => get_post_meta( $this->get_id(), '_billing_phone', true ),
|
||||
'shipping_first_name' => get_post_meta( $this->get_id(), '_shipping_first_name', true ),
|
||||
'shipping_last_name' => get_post_meta( $this->get_id(), '_shipping_last_name', true ),
|
||||
'shipping_company' => get_post_meta( $this->get_id(), '_shipping_company', true ),
|
||||
'shipping_address_1' => get_post_meta( $this->get_id(), '_shipping_address_1', true ),
|
||||
'shipping_address_2' => get_post_meta( $this->get_id(), '_shipping_address_2', true ),
|
||||
'shipping_city' => get_post_meta( $this->get_id(), '_shipping_city', true ),
|
||||
'shipping_state' => get_post_meta( $this->get_id(), '_shipping_state', true ),
|
||||
'shipping_postcode' => get_post_meta( $this->get_id(), '_shipping_postcode', true ),
|
||||
'shipping_country' => get_post_meta( $this->get_id(), '_shipping_country', true ),
|
||||
'payment_method' => get_post_meta( $this->get_id(), '_payment_method', true ),
|
||||
'payment_method_title' => get_post_meta( $this->get_id(), '_payment_method_title', true ),
|
||||
'transaction_id' => get_post_meta( $this->get_id(), '_transaction_id', true ),
|
||||
'customer_ip_address' => get_post_meta( $this->get_id(), '_customer_ip_address', true ),
|
||||
'customer_user_agent' => get_post_meta( $this->get_id(), '_customer_user_agent', true ),
|
||||
'created_via' => get_post_meta( $this->get_id(), '_created_via', true ),
|
||||
'customer_note' => get_post_meta( $this->get_id(), '_customer_note', true ),
|
||||
'date_completed' => get_post_meta( $this->get_id(), '_completed_date', true ),
|
||||
'date_paid' => get_post_meta( $this->get_id(), '_paid_date', true ),
|
||||
'cart_hash' => get_post_meta( $this->get_id(), '_cart_hash', true ),
|
||||
'customer_note' => $post_object->post_excerpt,
|
||||
) );
|
||||
|
||||
$this->maybe_set_user_billing_email();
|
||||
$this->_reading = false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -953,7 +953,7 @@ class WC_Order extends WC_Abstract_Order {
|
|||
*/
|
||||
public function set_billing_email( $value ) {
|
||||
if ( $value && ! is_email( $value ) ) {
|
||||
$this->invalid_data( 'order_invalid_billing_email', __( 'Invalid order billing email address', 'woocommerce' ) );
|
||||
$this->error( 'order_invalid_billing_email', __( 'Invalid order billing email address', 'woocommerce' ) );
|
||||
}
|
||||
$this->set_prop( 'billing', 'email', sanitize_email( $value ) );
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue