Automattic\WooCommerce\StoreApi\Schemas\V1

CartSchema::get_item_responsepublicWC 1.0

Convert a woo cart into an object suitable for the response.

Метод класса: CartSchema{}

Хуков нет.

Возвращает

Массив.

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

$CartSchema = new CartSchema();
$CartSchema->get_item_response( $cart );
$cart(WC_Cart) (обязательный)
Cart class instance.

Код CartSchema::get_item_response() WC 10.8.1

public function get_item_response( $cart ) {
	$controller = new CartController();

	// Get cart errors first so if recalculations are performed, it's reflected in the response.
	$cart_errors = $this->get_cart_errors( $cart );

	// Get shipping packages to return in the response from the cart.
	$shipping_packages = $cart->has_calculated_shipping() ? $controller->get_shipping_packages() : [];

	// Get visible cross sells products.
	$cross_sells    = array();
	$cross_sell_ids = $cart->get_cross_sells();
	$image_ids      = array();
	if ( ! empty( $cross_sell_ids ) ) {
		// Prime caches to reduce future queries.
		_prime_post_caches( $cross_sell_ids );
		$cross_sells = array_values( array_filter( array_map( 'wc_get_product', $cross_sell_ids ), 'wc_products_array_filter_visible' ) );
		/** @var \WC_Product[] $cross_sells */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort
		// Identify which images need priming.
		$ids         = array_map( static fn( $product ) => array( (int) $product->get_image_id(), ...$product->get_gallery_image_ids() ), $cross_sells );
		$image_ids[] = array_values( array_filter( array_merge( ...$ids ) ) );
	}

	$cart_all_items  = $cart->get_cart();
	$cart_line_items = array_values( array_filter( $cart_all_items, static fn( $item ) => ( $item['data'] ?? null ) instanceof \WC_Product ) );
	if ( ! empty( $cart_line_items ) ) {
		// Identify which images need priming.
		$ids         = array_map( static fn( $item ) => array( (int) $item['data']->get_image_id(), ...$item['data']->get_gallery_image_ids() ), $cart_line_items );
		$image_ids[] = array_values( array_filter( array_merge( ...array_values( $ids ) ) ) );
	}

	if ( ! empty( $image_ids ) ) {
		// Prime caches to reduce future queries.
		_prime_post_caches( array_unique( array_merge( ...$image_ids ) ) );
	}

	return [
		'items'                   => $this->get_item_responses_from_schema( $this->item_schema, $cart_all_items ),
		'coupons'                 => $this->get_item_responses_from_schema( $this->coupon_schema, $cart->get_applied_coupons() ),
		'fees'                    => $this->get_item_responses_from_schema( $this->fee_schema, $cart->get_fees() ),
		'totals'                  => (object) $this->prepare_currency_response( $this->get_totals( $cart ) ),
		'shipping_address'        => (object) $this->shipping_address_schema->get_item_response( wc()->customer ),
		'billing_address'         => (object) $this->billing_address_schema->get_item_response( wc()->customer ),
		'needs_payment'           => $cart->needs_payment(),
		'needs_shipping'          => $cart->needs_shipping(),
		'payment_requirements'    => $this->extend->get_payment_requirements(),
		'has_calculated_shipping' => $cart->has_calculated_shipping(),
		'shipping_rates'          => $this->get_item_responses_from_schema( $this->shipping_rate_schema, $shipping_packages ),
		'items_count'             => $cart->get_cart_contents_count(),
		'items_weight'            => wc_get_weight( $cart->get_cart_contents_weight(), 'g' ),
		'cross_sells'             => $this->get_item_responses_from_schema( $this->cross_sells_item_schema, $cross_sells ),
		'errors'                  => $cart_errors,
		'payment_methods'         => array_values( wp_list_pluck( WC()->payment_gateways->get_available_payment_gateways(), 'id' ) ),
		self::EXTENDING_KEY       => $this->get_extended_data( self::IDENTIFIER ),
	];
}