->increment( $order->get_type(), OrderStatus::TRASH ); } /** * Update the cache when an order is deleted. * * @param int $order_id Order id. * @param WC_Order $order The order. */ public function update_on_order_deleted( $order_id, $order ) { if ( ! $this->order_count_cache->is_cached( $order->get_type(), $this->get_prefixed_status( $order->get_status() ) ) ) { return; } $this->order_count_cache->decrement( $order->get_type(), $this->get_prefixed_status( $order->get_status() ) ); } /** * Update the cache whenver an order status changes. * * @param int $order_id Order id. * @param string $previous_status the old WooCommerce order status. * @param string $next_status the new WooCommerce order status. * @param WC_Order $order The order. */ public function update_on_order_status_changed( $order_id, $previous_status, $next_status, $order ) { if ( ! $this->order_count_cache->is_cached( $order->get_type(), $this->get_prefixed_status( $next_status ) ) || ! $this->order_count_cache->is_cached( $order->get_type(), $this->get_prefixed_status( $previous_status ) ) ) { return; } // If the order status count has already been incremented, we can skip incrementing it again. if ( isset( $this->order_statuses[ $order_id ] ) && $this->order_statuses[ $order_id ] === $next_status ) { return; } $this->order_statuses[ $order_id ] = $next_status; $was_decremented = $this->order_count_cache->decrement( $order->get_type(), $this->get_prefixed_status( $previous_status ) ); $this->order_count_cache->increment( $order->get_type(), $this->get_prefixed_status( $next_status ) ); // Set the initial order status in case this is a new order and the previous status should not be decremented. if ( ! isset( $this->initial_order_statuses[ $order_id ] ) && $was_decremented ) { $this->initial_order_statuses[ $order_id ] = $previous_status; } } /** * Get the prefixed status. * * @param string $status The status. * @return string */ private function get_prefixed_status( $status ) { $status = 'wc-' . $status; $special_statuses = array( 'wc-' . OrderStatus::AUTO_DRAFT => OrderStatus::AUTO_DRAFT, 'wc-' . OrderStatus::TRASH => OrderStatus::TRASH, ); if ( isset( $special_statuses[ $status ] ) ) { return $special_statuses[ $status ]; } return $status; } }