Automattic\WooCommerce\StoreApi\Utilities

OrderController::get_failed_order_stock_error()publicWC 1.0

Get errors for order stock on failed orders.

Method of the class: OrderController{}

Return

null. Nothing (null).

Usage

$OrderController = new OrderController();
$OrderController->get_failed_order_stock_error( $order_id );
$order_id(int) (required)
Order ID.

OrderController::get_failed_order_stock_error() code WC 9.5.1

public function get_failed_order_stock_error( $order_id ) {
	$order = wc_get_order( $order_id );

	// Ensure order items are still stocked if paying for a failed order. Pending orders do not need this check because stock is held.
	if ( ! $order->has_status( wc_get_is_pending_statuses() ) ) {
		$quantities = array();

		foreach ( $order->get_items() as $item_key => $item ) {
			if ( $item && is_callable( array( $item, 'get_product' ) ) ) {
				$product = $item->get_product();

				if ( ! $product ) {
					continue;
				}

				$quantities[ $product->get_stock_managed_by_id() ] = isset( $quantities[ $product->get_stock_managed_by_id() ] ) ? $quantities[ $product->get_stock_managed_by_id() ] + $item->get_quantity() : $item->get_quantity();
			}
		}

		// Stock levels may already have been adjusted for this order (in which case we don't need to worry about checking for low stock).
		if ( ! $order->get_data_store()->get_stock_reduced( $order->get_id() ) ) {
			foreach ( $order->get_items() as $item_key => $item ) {
				if ( $item && is_callable( array( $item, 'get_product' ) ) ) {
					$product = $item->get_product();

					if ( ! $product ) {
						continue;
					}

					/**
					 * Filters whether or not the product is in stock for this pay for order.
					 *
					 * @param boolean True if in stock.
					 * @param \WC_Product $product Product.
					 * @param \WC_Order $order Order.
					 *
					 * @since 9.8.0-dev
					 */
					if ( ! apply_filters( 'woocommerce_pay_order_product_in_stock', $product->is_in_stock(), $product, $order ) ) {
						return array(
							'code'    => 'woocommerce_rest_out_of_stock',
							/* translators: %s: product name */
							'message' => sprintf( __( 'Sorry, "%s" is no longer in stock so this order cannot be paid for. We apologize for any inconvenience caused.', 'woocommerce' ), $product->get_name() ),
						);
					}

					// We only need to check products managing stock, with a limited stock qty.
					if ( ! $product->managing_stock() || $product->backorders_allowed() ) {
						continue;
					}

					// Check stock based on all items in the cart and consider any held stock within pending orders.
					$held_stock     = wc_get_held_stock_quantity( $product, $order->get_id() );
					$required_stock = $quantities[ $product->get_stock_managed_by_id() ];

					/**
					 * Filters whether or not the product has enough stock.
					 *
					 * @param boolean True if has enough stock.
					 * @param \WC_Product $product Product.
					 * @param \WC_Order $order Order.
					 *
					 * @since 9.8.0-dev
					 */
					if ( ! apply_filters( 'woocommerce_pay_order_product_has_enough_stock', ( $product->get_stock_quantity() >= ( $held_stock + $required_stock ) ), $product, $order ) ) {
						/* translators: 1: product name 2: quantity in stock */
						return array(
							'code'    => 'woocommerce_rest_out_of_stock',
							/* translators: %s: product name */
							'message' => sprintf( __( 'Sorry, we do not have enough "%1$s" in stock to fulfill your order (%2$s available). We apologize for any inconvenience caused.', 'woocommerce' ), $product->get_name(), wc_format_stock_quantity_for_display( $product->get_stock_quantity() - $held_stock, $product ) ),
						);
					}
				}
			}
		}
	}

	return null;
}