diff --git a/includes/abstracts/abstract-wc-order.php b/includes/abstracts/abstract-wc-order.php index 0f75607e39b..0da59900fee 100644 --- a/includes/abstracts/abstract-wc-order.php +++ b/includes/abstracts/abstract-wc-order.php @@ -51,13 +51,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { * @since 2.7.0 * @var array */ - protected $items = array( - 'line_items' => null, - 'coupon_lines' => null, - 'shipping_lines' => null, - 'fee_lines' => null, - 'tax_lines' => null, - ); + protected $items = array(); /** * Order items that need deleting are stored here. @@ -642,6 +636,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { /** * Remove all line items (products, coupons, shipping, taxes) from the order. + * * @param string $type Order item type. Default null. */ public function remove_order_items( $type = null ) { @@ -649,22 +644,17 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { $this->data_store->delete_items( $this, $type ); if ( $group = $this->type_to_group( $type ) ) { - $this->items[ $group ] = null; + unset( $this->items[ $group ] ); } } else { $this->data_store->delete_items( $this ); - $this->items = array( - 'line_items' => null, - 'coupon_lines' => null, - 'shipping_lines' => null, - 'fee_lines' => null, - 'tax_lines' => null, - ); + $this->items = array(); } } /** * Convert a type to a types group. + * * @param string $type * @return string group */ @@ -681,6 +671,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { /** * Return an array of items/products within this order. + * * @param string|array $types Types of line items to get (array or string). * @return Array of WC_Order_item */ @@ -690,7 +681,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 ] ) ) { + if ( ! isset( $this->items[ $group ] ) ) { $this->items[ $group ] = $this->data_store->read_items( $this, $type ); } // Don't use array_merge here because keys are numeric @@ -703,6 +694,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { /** * Return an array of fees within this order. + * * @return array */ public function get_fees() { @@ -711,6 +703,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { /** * Return an array of taxes within this order. + * * @return array */ public function get_taxes() { @@ -719,6 +712,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { /** * Return an array of shipping costs within this order. + * * @return array */ public function get_shipping_methods() { @@ -727,6 +721,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { /** * Gets formatted shipping method title. + * * @return string */ public function get_shipping_method() { @@ -739,6 +734,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { /** * Get coupon codes only. + * * @return array */ public function get_used_coupons() { @@ -770,6 +766,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { /** * Get an order item object, based on it's type. + * * @since 2.7.0 * @param int $item_id * @return WC_Order_Item @@ -780,6 +777,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { /** * Get key for where a certain item type is stored in _items. + * * @since 2.7.0 * @param $item object Order item (product, shipping, fee, coupon, tax) * @return string @@ -802,6 +800,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { /** * Remove item from the order. + * * @param int $item_id */ public function remove_item( $item_id ) { @@ -818,6 +817,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { /** * Adds an order item to this order. The order item will not persist until save. + * * @since 2.7.0 * @param WC_Order_Item Order item object (product, shipping, fee, coupon, tax) */ @@ -827,7 +827,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { } // Make sure existing items are loaded so we can append this new one. - if ( is_null( $this->items[ $items_key ] ) ) { + if ( ! isset( $this->items[ $items_key ] ) ) { $this->items[ $items_key ] = $this->get_items( $item->get_type() ); } @@ -845,6 +845,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { /** * Add a product line item to the order. This is the only line item type with * it's own method because it saves looking up order amounts (costs are added up for you). + * * @param \WC_Product $product * @param int $qty * @param array $args diff --git a/includes/class-wc-order-factory.php b/includes/class-wc-order-factory.php index 63ec1d6b2a7..1d25200ca7a 100644 --- a/includes/class-wc-order-factory.php +++ b/includes/class-wc-order-factory.php @@ -45,7 +45,15 @@ class WC_Order_Factory { } try { - return new $classname( $order_id ); + // Try to get from cache, otherwise create a new object, + $order = wp_cache_get( 'order-' . $order_id, 'orders' ); + + if ( ! is_a( $order, 'WC_Order' ) ) { + $order = new $classname( $order_id ); + wp_cache_set( 'order-' . $order_id, $order, 'orders' ); + } + + return $order; } catch ( Exception $e ) { return false; } @@ -97,7 +105,15 @@ class WC_Order_Factory { } if ( $classname ) { try { - return new $classname( $id ); + // Try to get from cache, otherwise create a new object, + $item = wp_cache_get( 'order-item-' . $id, 'order-items' ); + + if ( ! is_a( $item, 'WC_Order_Item' ) ) { + $item = new $classname( $id ); + wp_cache_set( 'order-item-' . $id, $item, 'order-items' ); + } + + return $item; } catch ( Exception $e ) { return false; } diff --git a/includes/class-wc-order-item-product.php b/includes/class-wc-order-item-product.php index 902dde5366a..e8d971dc8d0 100644 --- a/includes/class-wc-order-item-product.php +++ b/includes/class-wc-order-item-product.php @@ -196,8 +196,9 @@ class WC_Order_Item_Product extends WC_Order_Item { * Set meta data for backordered products. */ public function set_backorder_meta() { - if ( $this->get_product()->backorders_require_notification() && $this->get_product()->is_on_backorder( $this->get_quantity() ) ) { - $this->add_meta_data( apply_filters( 'woocommerce_backordered_item_meta_name', __( 'Backordered', 'woocommerce' ) ), $this->get_quantity() - max( 0, $this->get_product()->get_stock_quantity() ), true ); + $product = $this->get_product(); + if ( $product && $product->backorders_require_notification() && $product->is_on_backorder( $this->get_quantity() ) ) { + $this->add_meta_data( apply_filters( 'woocommerce_backordered_item_meta_name', __( 'Backordered', 'woocommerce' ) ), $this->get_quantity() - max( 0, $product->get_stock_quantity() ), true ); } } diff --git a/includes/data-stores/abstract-wc-order-data-store-cpt.php b/includes/data-stores/abstract-wc-order-data-store-cpt.php index d09aed05f37..d8983cf526d 100644 --- a/includes/data-stores/abstract-wc-order-data-store-cpt.php +++ b/includes/data-stores/abstract-wc-order-data-store-cpt.php @@ -242,6 +242,7 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme protected function clear_caches( &$order ) { clean_post_cache( $order->get_id() ); wc_delete_shop_order_transients( $order ); + wp_cache_delete( 'order-' . $order->get_id(), 'orders' ); } /** diff --git a/includes/data-stores/abstract-wc-order-item-type-data-store.php b/includes/data-stores/abstract-wc-order-item-type-data-store.php index 1db17c22477..a9fc5536a34 100644 --- a/includes/data-stores/abstract-wc-order-item-type-data-store.php +++ b/includes/data-stores/abstract-wc-order-item-type-data-store.php @@ -45,6 +45,7 @@ abstract class Abstract_WC_Order_Item_Type_Data_Store extends WC_Data_Store_WP i $this->save_item_data( $item ); $item->save_meta_data(); $item->apply_changes(); + $this->clear_cache( $item ); do_action( 'woocommerce_new_order_item', $item->get_id(), $item, $item->get_order_id() ); } @@ -67,6 +68,7 @@ abstract class Abstract_WC_Order_Item_Type_Data_Store extends WC_Data_Store_WP i $this->save_item_data( $item ); $item->save_meta_data(); $item->apply_changes(); + $this->clear_cache( $item ); do_action( 'woocommerce_update_order_item', $item->get_id(), $item, $item->get_order_id() ); } @@ -126,6 +128,6 @@ abstract class Abstract_WC_Order_Item_Type_Data_Store extends WC_Data_Store_WP i * Clear meta cachce. */ public function clear_cache( &$item ) { - WC_Cache_Helper::incr_cache_prefix( 'orders' ); + wp_cache_delete( 'order-item-' . $item->get_id(), 'order-items' ); } } diff --git a/includes/data-stores/class-wc-order-item-coupon-data-store.php b/includes/data-stores/class-wc-order-item-coupon-data-store.php index b4ee16a62a7..202e699eb5b 100644 --- a/includes/data-stores/class-wc-order-item-coupon-data-store.php +++ b/includes/data-stores/class-wc-order-item-coupon-data-store.php @@ -51,6 +51,5 @@ class WC_Order_Item_Coupon_Data_Store extends Abstract_WC_Order_Item_Type_Data_S foreach ( $save_values as $key => $value ) { update_metadata( 'order_item', $id, $key, $value ); } - $this->clear_cache( $item ); } } diff --git a/includes/data-stores/class-wc-order-item-fee-data-store.php b/includes/data-stores/class-wc-order-item-fee-data-store.php index 76a7d0ab00b..bb638631f3c 100644 --- a/includes/data-stores/class-wc-order-item-fee-data-store.php +++ b/includes/data-stores/class-wc-order-item-fee-data-store.php @@ -55,6 +55,5 @@ class WC_Order_Item_Fee_Data_Store extends Abstract_WC_Order_Item_Type_Data_Stor foreach ( $save_values as $key => $value ) { update_metadata( 'order_item', $id, $key, $value ); } - $this->clear_cache( $item ); } } diff --git a/includes/data-stores/class-wc-order-item-product-store.php b/includes/data-stores/class-wc-order-item-product-store.php index 4050fc005b7..748db13bf3c 100644 --- a/includes/data-stores/class-wc-order-item-product-store.php +++ b/includes/data-stores/class-wc-order-item-product-store.php @@ -62,7 +62,6 @@ class WC_Order_Item_Product_Data_Store extends Abstract_WC_Order_Item_Type_Data_ foreach ( $save_values as $key => $value ) { update_metadata( 'order_item', $id, $key, $value ); } - $this->clear_cache( $item ); } /** 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 7588aaf1a3a..af52d77b14c 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 @@ -53,6 +53,5 @@ class WC_Order_Item_Shipping_Data_Store extends Abstract_WC_Order_Item_Type_Data foreach ( $save_values as $key => $value ) { update_metadata( 'order_item', $id, $key, $value ); } - $this->clear_cache( $item ); } } diff --git a/includes/data-stores/class-wc-order-item-tax-data-store.php b/includes/data-stores/class-wc-order-item-tax-data-store.php index d620cc2f4c0..96df2129e55 100644 --- a/includes/data-stores/class-wc-order-item-tax-data-store.php +++ b/includes/data-stores/class-wc-order-item-tax-data-store.php @@ -56,6 +56,5 @@ class WC_Order_Item_Tax_Data_Store extends Abstract_WC_Order_Item_Type_Data_Stor foreach ( $save_values as $key => $value ) { update_metadata( 'order_item', $id, $key, $value ); } - $this->clear_cache( $item ); } }