Improved WC_Order::get_item()

Do not load all items, just load the items with a given order_item_type.
This commit is contained in:
Cesar Rodas 2017-05-23 20:12:47 -04:00
parent b612bab38b
commit ea9c573f2c
2 changed files with 13 additions and 10 deletions

View File

@ -800,11 +800,9 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
}
// The item was not found in memory, we load in memory all the items related to this order
$types = $this->data_store->get_order_item_types( $this );
foreach ( $types as $type ) {
if ( ! ( $group = $this->type_to_group( $type ) ) || ! empty( $this->items[ $group ] ) ) {
continue;
}
$type = $this->data_store->get_order_item_type( $this, $item_id );
$group = $type ? $this->type_to_group( $type ) : false;
if ( $group ) {
$this->items[ $group ] = $this->data_store->read_items( $this, $type );
if ( ! empty( $this->items[ $group ][ $item_id ] ) ) {
return $this->items[ $group ][ $item_id ];

View File

@ -354,15 +354,20 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
}
/**
* Get all the order items type in a given order
* Return the order type of a given item which belongs to WC_Order
*
* @param WC_Order $order Order Object
* @param int $order_id
*
* @return Array A list with all order_item_type
* @return string Order Item type
*/
public function get_order_item_types( WC_Order $order ) {
public function get_order_item_type( WC_Order $order, $order_item_id ) {
global $wpdb;
$query = $wpdb->prepare( "SELECT DISTINCT order_item_type FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d", $order->get_id() );
return wp_list_pluck( $wpdb->get_results( $query ), 'order_item_type' );
$query = $wpdb->prepare(
"SELECT DISTINCT order_item_type FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d and order_item_id",
$order->get_id(),
$order_item_id
);
return $wpdb->get_var( $query );
}
}