wc_update_total_sales_counts()WC 3.0.0

Update total sales amount for each product within a paid order.

Хуки из функции

Возвращает

null. Ничего (null).

Использование

wc_update_total_sales_counts( $order_id );
$order_id(int) (обязательный)
Order ID.

Список изменений

С версии 3.0.0 Введена.

Код wc_update_total_sales_counts() WC 8.7.0

function wc_update_total_sales_counts( $order_id ) {
	$order = wc_get_order( $order_id );

	if ( ! $order ) {
		return;
	}

	$recorded_sales  = $order->get_data_store()->get_recorded_sales( $order );
	$reflected_order = in_array( $order->get_status(), array( 'cancelled', 'trash' ), true );

	if ( ! $reflected_order && 'woocommerce_before_delete_order' === current_action() ) {
		$reflected_order = true;
	}

	if ( $recorded_sales xor $reflected_order ) {
		return;
	}

	$operation = $recorded_sales && $reflected_order ? 'decrease' : 'increase';

	if ( count( $order->get_items() ) > 0 ) {
		foreach ( $order->get_items() as $item ) {
			$product_id = $item->get_product_id();

			if ( $product_id ) {
				$data_store = WC_Data_Store::load( 'product' );
				$data_store->update_product_sales( $product_id, absint( $item->get_quantity() ), $operation );
			}
		}
	}

	if ( 'decrease' === $operation ) {
		$order->get_data_store()->set_recorded_sales( $order, false );
	} else {
		$order->get_data_store()->set_recorded_sales( $order, true );
	}

	/**
	 * Called when sales for an order are recorded
	 *
	 * @param int $order_id order id
	 */
	do_action( 'woocommerce_recorded_sales', $order_id );
}