Use data store for methods
This commit is contained in:
parent
81a2d6345f
commit
eafd7c9fa5
|
@ -408,7 +408,7 @@ abstract class WC_Abstract_Legacy_Order extends WC_Data {
|
|||
} elseif ( 'post' === $key ) {
|
||||
return get_post( $this->get_id() );
|
||||
} elseif ( 'status' === $key || 'post_status' === $key ) {
|
||||
return $this->get_status();
|
||||
return 'wc-' . $this->get_status();
|
||||
} elseif ( 'customer_message' === $key || 'customer_note' === $key ) {
|
||||
return $this->get_customer_note();
|
||||
} elseif ( in_array( $key, array( 'user_id', 'customer_user' ) ) ) {
|
||||
|
|
|
@ -460,7 +460,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
|
|||
$new_status = 'pending';
|
||||
}
|
||||
|
||||
$this->set_prop( 'status', 'wc-' . $new_status ); // 'wc-' === substr( $status, 0, 3 ) ? substr( $status, 3 ) @todo removed this from getter.
|
||||
$this->set_prop( 'status', $new_status );
|
||||
|
||||
// If the old status is set but unknown (e.g. draft) assume its pending for action usage.
|
||||
if ( $old_status && ! in_array( 'wc-' . $old_status, array_keys( wc_get_order_statuses() ) ) ) {
|
||||
|
@ -617,16 +617,14 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
|
|||
* @param string $type Order item type. Default null.
|
||||
*/
|
||||
public function remove_order_items( $type = null ) {
|
||||
global $wpdb;
|
||||
if ( ! empty( $type ) ) {
|
||||
$wpdb->query( $wpdb->prepare( "DELETE FROM itemmeta USING {$wpdb->prefix}woocommerce_order_itemmeta itemmeta INNER JOIN {$wpdb->prefix}woocommerce_order_items items WHERE itemmeta.order_item_id = items.order_item_id AND items.order_id = %d AND items.order_item_type = %s", $this->get_id(), $type ) );
|
||||
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d AND order_item_type = %s", $this->get_id(), $type ) );
|
||||
$this->data_store->delete_items( $this, $type );
|
||||
|
||||
if ( $group = $this->type_to_group( $type ) ) {
|
||||
$this->items[ $group ] = null;
|
||||
}
|
||||
} else {
|
||||
$wpdb->query( $wpdb->prepare( "DELETE FROM itemmeta USING {$wpdb->prefix}woocommerce_order_itemmeta itemmeta INNER JOIN {$wpdb->prefix}woocommerce_order_items items WHERE itemmeta.order_item_id = items.order_item_id and items.order_id = %d", $this->get_id() ) );
|
||||
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d", $this->get_id() ) );
|
||||
$this->data_store->delete_items( $this );
|
||||
$this->items = array(
|
||||
'line_items' => null,
|
||||
'coupon_lines' => null,
|
||||
|
@ -665,7 +663,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
|
|||
foreach ( $types as $type ) {
|
||||
if ( $group = $this->type_to_group( $type ) ) {
|
||||
if ( is_null( $this->items[ $group ] ) ) {
|
||||
$this->items[ $group ] = $this->get_items_from_db( $type );
|
||||
$this->items[ $group ] = $this->data_store->read_items( $this, $type );
|
||||
}
|
||||
// Don't use array_merge here because keys are numeric
|
||||
$items = $items + $this->items[ $group ];
|
||||
|
@ -675,26 +673,6 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
|
|||
return apply_filters( 'woocommerce_order_get_items', $items, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets items from the database by type.
|
||||
* @param string $type
|
||||
* @return array
|
||||
*/
|
||||
protected function get_items_from_db( $type ) {
|
||||
global $wpdb;
|
||||
|
||||
$get_items_sql = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d AND order_item_type = %s ORDER BY order_item_id;", $this->get_id(), $type );
|
||||
$items = $wpdb->get_results( $get_items_sql );
|
||||
|
||||
if ( ! empty( $items ) ) {
|
||||
$items = array_map( array( $this, 'get_item' ), array_combine( wp_list_pluck( $items, 'order_item_id' ), $items ) );
|
||||
} else {
|
||||
$items = array();
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of fees within this order.
|
||||
* @return array
|
||||
|
@ -905,15 +883,10 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
|
|||
return false;
|
||||
}
|
||||
|
||||
$token_ids = get_post_meta( $this->get_id(), '_payment_tokens', true );
|
||||
|
||||
if ( empty( $token_ids ) ) {
|
||||
$token_ids = array();
|
||||
}
|
||||
|
||||
$token_ids = $this->data_store->get_payment_token_ids( $this );
|
||||
$token_ids[] = $token->get_id();
|
||||
$this->data_store->update_payment_token_ids( $this, $token_ids );
|
||||
|
||||
update_post_meta( $this->get_id(), '_payment_tokens', $token_ids );
|
||||
do_action( 'woocommerce_payment_token_added_to_order', $this->get_id(), $token->get_id(), $token, $token_ids );
|
||||
return $token->get_id();
|
||||
}
|
||||
|
@ -981,8 +954,8 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
|
|||
* @param $args array Added in 2.7.0 to pass things like location.
|
||||
*/
|
||||
public function calculate_taxes( $args = array() ) {
|
||||
$tax_based_on = get_option( 'woocommerce_tax_based_on' );
|
||||
$args = wp_parse_args( $args, array(
|
||||
$tax_based_on = get_option( 'woocommerce_tax_based_on' );
|
||||
$args = wp_parse_args( $args, array(
|
||||
'country' => 'billing' === $tax_based_on ? $this->get_billing_country() : $this->get_shipping_country(),
|
||||
'state' => 'billing' === $tax_based_on ? $this->get_billing_state() : $this->get_shipping_state(),
|
||||
'postcode' => 'billing' === $tax_based_on ? $this->get_billing_postcode() : $this->get_shipping_postcode(),
|
||||
|
|
|
@ -126,53 +126,6 @@ class WC_Order_Data_Store_CPT extends WC_Data_Store_CPT implements WC_Object_Dat
|
|||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* Save all order items which are part of this order. @todo
|
||||
*/
|
||||
protected function save_items( $order ) {
|
||||
// remove items
|
||||
foreach ( $this->items_to_delete as $item ) {
|
||||
$item->delete();
|
||||
}
|
||||
|
||||
$this->items_to_delete = array();
|
||||
|
||||
// Add/save items
|
||||
foreach ( $this->items as $item_group => $items ) {
|
||||
if ( is_array( $items ) ) {
|
||||
foreach ( $items as $item_key => $item ) {
|
||||
$item->set_order_id( $this->get_id() );
|
||||
$item_id = $item->save();
|
||||
|
||||
// If ID changed (new item saved to DB)...
|
||||
if ( $item_id !== $item_key ) {
|
||||
$this->items[ $item_group ][ $item_id ] = $item;
|
||||
unset( $this->items[ $item_group ][ $item_key ] );
|
||||
|
||||
// Legacy action handler
|
||||
switch ( $item_group ) {
|
||||
case 'fee_lines' :
|
||||
if ( isset( $item->legacy_fee, $item->legacy_fee_key ) ) {
|
||||
wc_do_deprecated_action( 'woocommerce_add_order_fee_meta', array( $this->get_id(), $item_id, $item->legacy_fee, $item->legacy_fee_key ), '2.7', 'Use woocommerce_new_order_item action instead.' );
|
||||
}
|
||||
break;
|
||||
case 'shipping_lines' :
|
||||
if ( isset( $item->legacy_package_key ) ) {
|
||||
wc_do_deprecated_action( 'woocommerce_add_shipping_order_item', array( $item_id, $item->legacy_package_key ), '2.7', 'Use woocommerce_new_order_item action instead.' );
|
||||
}
|
||||
break;
|
||||
case 'line_items' :
|
||||
if ( isset( $item->legacy_values, $item->legacy_cart_item_key ) ) {
|
||||
wc_do_deprecated_action( 'woocommerce_add_order_item_meta', array( $item_id, $item->legacy_values, $item->legacy_cart_item_key ), '2.7', 'Use woocommerce_new_order_item action instead.' );
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read order data. Can be overridden by child classes to load other props.
|
||||
*
|
||||
|
@ -261,4 +214,110 @@ class WC_Order_Data_Store_CPT extends WC_Data_Store_CPT implements WC_Object_Dat
|
|||
clean_post_cache( $order->get_id() );
|
||||
wc_delete_shop_order_transients( $order->get_id() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Save all order items which are part of this order. @todo
|
||||
*/
|
||||
protected function save_items( $order ) {
|
||||
// remove items
|
||||
foreach ( $this->items_to_delete as $item ) {
|
||||
$item->delete();
|
||||
}
|
||||
|
||||
$this->items_to_delete = array();
|
||||
|
||||
// Add/save items
|
||||
foreach ( $this->items as $item_group => $items ) {
|
||||
if ( is_array( $items ) ) {
|
||||
foreach ( $items as $item_key => $item ) {
|
||||
$item->set_order_id( $this->get_id() );
|
||||
$item_id = $item->save();
|
||||
|
||||
// If ID changed (new item saved to DB)...
|
||||
if ( $item_id !== $item_key ) {
|
||||
$this->items[ $item_group ][ $item_id ] = $item;
|
||||
unset( $this->items[ $item_group ][ $item_key ] );
|
||||
|
||||
// Legacy action handler
|
||||
switch ( $item_group ) {
|
||||
case 'fee_lines' :
|
||||
if ( isset( $item->legacy_fee, $item->legacy_fee_key ) ) {
|
||||
wc_do_deprecated_action( 'woocommerce_add_order_fee_meta', array( $this->get_id(), $item_id, $item->legacy_fee, $item->legacy_fee_key ), '2.7', 'Use woocommerce_new_order_item action instead.' );
|
||||
}
|
||||
break;
|
||||
case 'shipping_lines' :
|
||||
if ( isset( $item->legacy_package_key ) ) {
|
||||
wc_do_deprecated_action( 'woocommerce_add_shipping_order_item', array( $item_id, $item->legacy_package_key ), '2.7', 'Use woocommerce_new_order_item action instead.' );
|
||||
}
|
||||
break;
|
||||
case 'line_items' :
|
||||
if ( isset( $item->legacy_values, $item->legacy_cart_item_key ) ) {
|
||||
wc_do_deprecated_action( 'woocommerce_add_order_item_meta', array( $item_id, $item->legacy_values, $item->legacy_cart_item_key ), '2.7', 'Use woocommerce_new_order_item action instead.' );
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read order items of a specific type from the database for this order.
|
||||
* @param WC_Order $order
|
||||
* @param string $type
|
||||
* @return array
|
||||
*/
|
||||
public function read_items( $order, $type ) {
|
||||
global $wpdb;
|
||||
|
||||
$get_items_sql = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d AND order_item_type = %s ORDER BY order_item_id;", $this->get_id(), $type );
|
||||
$items = $wpdb->get_results( $get_items_sql );
|
||||
|
||||
if ( ! empty( $items ) ) {
|
||||
$items = array_map( array( WC_Order_Factory, 'get_order_item' ), array_combine( wp_list_pluck( $items, 'order_item_id' ), $items ) );
|
||||
} else {
|
||||
$items = array();
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all line items (products, coupons, shipping, taxes) from the order.
|
||||
*
|
||||
* @param WC_Order
|
||||
* @param string $type Order item type. Default null.
|
||||
*/
|
||||
public function delete_items( $order, $type = null ) { // @todo BW compat legacy
|
||||
global $wpdb;
|
||||
if ( ! empty( $type ) ) {
|
||||
$wpdb->query( $wpdb->prepare( "DELETE FROM itemmeta USING {$wpdb->prefix}woocommerce_order_itemmeta itemmeta INNER JOIN {$wpdb->prefix}woocommerce_order_items items WHERE itemmeta.order_item_id = items.order_item_id AND items.order_id = %d AND items.order_item_type = %s", $order->get_id(), $type ) );
|
||||
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d AND order_item_type = %s", $order->get_id(), $type ) );
|
||||
} else {
|
||||
$wpdb->query( $wpdb->prepare( "DELETE FROM itemmeta USING {$wpdb->prefix}woocommerce_order_itemmeta itemmeta INNER JOIN {$wpdb->prefix}woocommerce_order_items items WHERE itemmeta.order_item_id = items.order_item_id and items.order_id = %d", $order->get_id() ) );
|
||||
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d", $order->get_id() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get token ids for an order.
|
||||
*
|
||||
* @param WC_Order
|
||||
* @return array
|
||||
*/
|
||||
public function get_payment_token_ids( $order ) {
|
||||
$token_ids = array_filter( (array) get_post_meta( $order->get_id(), '_payment_tokens', true ) );
|
||||
return $token_ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update token ids for an order.
|
||||
*
|
||||
* @param WC_Order
|
||||
* @param array $token_ids
|
||||
*/
|
||||
public function update_payment_token_ids( $order, $token_ids ) {
|
||||
update_post_meta( $order->get_id(), '_payment_tokens', $token_ids );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue