Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldsSchema

DocumentObject::get_cart_dataprotectedWC 1.0

Gets a subset of cart data.

Method of the class: DocumentObject{}

No Hooks.

Returns

Array. The cart data.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_cart_data();

DocumentObject::get_cart_data() code WC 9.9.4

protected function get_cart_data() {
	$cart_data               = StoreApi::container()->get( SchemaController::class )->get( CartSchema::IDENTIFIER )->get_item_response( $this->cart );
	$selected_shipping_rates = array_filter(
		array_map(
			function ( $package ) {
				$selected_rate = array_search( true, array_column( $package['shipping_rates'], 'selected' ), true );
				return false !== $selected_rate && isset( $package['shipping_rates'][ $selected_rate ] ) ? $package['shipping_rates'][ $selected_rate ] : null;
			},
			$cart_data['shipping_rates']
		)
	);
	$local_pickup_method_ids = LocalPickupUtils::get_local_pickup_method_ids();

	return wp_parse_args(
		$this->request_data['cart'] ?? [],
		[
			'coupons'            => array_values( wc_list_pluck( $cart_data['coupons'], 'code' ) ),
			'shipping_rates'     => array_values( wc_list_pluck( $selected_shipping_rates, 'rate_id' ) ),
			'items'              => array_merge(
				...array_map(
					function ( $item ) {
						return array_fill( 0, $item['quantity'], $item['id'] );
					},
					$cart_data['items']
				)
			),
			'items_type'         => array_unique( array_values( wc_list_pluck( $cart_data['items'], 'type' ) ) ),
			'items_count'        => $cart_data['items_count'],
			'items_weight'       => $cart_data['items_weight'],
			'needs_shipping'     => $cart_data['needs_shipping'],
			'prefers_collection' => count( array_intersect( $local_pickup_method_ids, wc_list_pluck( $selected_shipping_rates, 'method_id' ) ) ) > 0,
			'totals'             => [
				'total_price' => $cart_data['totals']->total_price,
				'total_tax'   => $cart_data['totals']->total_tax,
			],
			'extensions'         => (array) $cart_data['extensions'],
		]
	);
}