Automattic\WooCommerce\StoreApi\Routes\V1

Checkout::create_or_update_draft_order()privateWC 1.0

Create or update a draft order based on the cart.

{} Это метод класса: Checkout{}

Возвращает

null. Ничего.

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

// private - только в коде основоного (родительского) класса
$result = $this->create_or_update_draft_order( $request );
$request(\WP_REST_Request) (обязательный)
Full details about the request.

Код Checkout::create_or_update_draft_order() WC 6.7.0

private function create_or_update_draft_order( \WP_REST_Request $request ) {
	$this->order = $this->get_draft_order();

	if ( ! $this->order ) {
		$this->order = $this->order_controller->create_order_from_cart();
	} else {
		$this->order_controller->update_order_from_cart( $this->order );
	}

	wc_do_deprecated_action(
		'__experimental_woocommerce_blocks_checkout_update_order_meta',
		array(
			$this->order,
		),
		'6.3.0',
		'woocommerce_store_api_checkout_update_order_meta',
		'This action was deprecated in WooCommerce Blocks version 6.3.0. Please use woocommerce_store_api_checkout_update_order_meta instead.'
	);

	wc_do_deprecated_action(
		'woocommerce_blocks_checkout_update_order_meta',
		array(
			$this->order,
		),
		'7.2.0',
		'woocommerce_store_api_checkout_update_order_meta',
		'This action was deprecated in WooCommerce Blocks version 7.2.0. Please use woocommerce_store_api_checkout_update_order_meta instead.'
	);

	/**
	 * Fires when the Checkout Block/Store API updates an order's meta data.
	 *
	 * This hook gives extensions the chance to add or update meta data on the $order.
	 * Throwing an exception from a callback attached to this action will make the Checkout Block render in a warning state, effectively preventing checkout.
	 *
	 * This is similar to existing core hook woocommerce_checkout_update_order_meta.
	 * We're using a new action:
	 * - To keep the interface focused (only pass $order, not passing request data).
	 * - This also explicitly indicates these orders are from checkout block/StoreAPI.
	 *
	 * @see https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/3686
	 *
	 * @param \WC_Order $order Order object.
	 */
	do_action( 'woocommerce_store_api_checkout_update_order_meta', $this->order );

	// Confirm order is valid before proceeding further.
	if ( ! $this->order instanceof \WC_Order ) {
		throw new RouteException(
			'woocommerce_rest_checkout_missing_order',
			__( 'Unable to create order', 'woocommerce' ),
			500
		);
	}

	// Store order ID to session.
	$this->set_draft_order_id( $this->order->get_id() );

	/**
	 * Try to reserve stock for the order.
	 *
	 * If creating a draft order on checkout entry, set the timeout to 10 mins.
	 * If POSTing to the checkout (attempting to pay), set the timeout to 60 mins (using the woocommerce_hold_stock_minutes option).
	 */
	try {
		$reserve_stock = new ReserveStock();
		$duration      = $request->get_method() === 'POST' ? (int) get_option( 'woocommerce_hold_stock_minutes', 60 ) : 10;
		$reserve_stock->reserve_stock_for_order( $this->order, $duration );
	} catch ( ReserveStockException $e ) {
		throw new RouteException(
			$e->getErrorCode(),
			$e->getMessage(),
			$e->getCode()
		);
	}
}