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 5.2.0-beta.1 10.0.2 5.2.0-rc.1 10.0.3 5.2.0-rc.2 10.0.4 5.2.1 10.0.5 5.2.2 10.0.6 5.2.3 10.1.0 5.2.4 10.1.0-rc.1 5.2.5 10.1.0-rc.2 5.3.0 10.1.0-rc.3 5.3.0-beta.1 10.1.0-rc.4 5.3.0-rc.1 10.1.1 5.3.0-rc.2 10.1.2 5.3.1 10.1.3 5.3.2 10.1.4 5.3.3 10.2.0 5.4.0 10.2.0-beta.1 5.4.0-beta.1 10.2.0-beta.2 5.4.0-rc.1 10.2.0-rc.1 5.4.1 10.2.1 5.4.2 10.2.2 5.4.3 10.2.3 5.4.4 10.2.4 5.4.5 10.3.0 5.5.0 10.3.0-beta.1 5.5.0-beta.1 10.3.0-beta.2 5.5.0-rc.1 10.3.0-rc.1 5.5.0-rc.2 10.3.0-rc.2 5.5.1 10.3.1 5.5.2 10.3.2 5.5.3 10.3.3 5.5.4 10.3.4 5.5.5 10.3.5 5.6.0 10.3.6 5.6.0-beta.1 10.3.7 5.6.0-rc.1 10.3.8 5.6.0-rc.2 10.4.0 5.6.1 10.4.0-beta.1 5.6.2 10.4.0-beta.2 5.6.3 10.4.0-rc.1 5.7.0 10.4.1 5.7.0-beta.1 10.4.2 5.7.0-rc.1 10.4.3 5.7.1 10.4.4 5.7.2 10.5.0 5.7.3 10.5.0-beta.1 5.8.0 10.5.0-beta.2 5.8.0-beta.1 10.5.0-rc.1 5.8.0-beta.2 10.5.0-rc.2 5.8.0-rc.1 10.5.0-rc.3 5.8.1 10.5.1 5.8.2 10.5.2 5.9.0 10.5.3 5.9.0-beta.1 10.6.0 5.9.0-rc.1 10.6.0-beta.1 5.9.0-rc.2 10.6.0-beta.2 5.9.1 10.6.0-rc.1 5.9.2 10.6.1 6.0.0 10.6.2 6.0.0-beta.1 10.7.0 6.0.0-rc.1 10.7.0-beta.1 6.0.1 10.7.0-beta.2 6.0.2 10.7.0-rc.1 6.1.0 3.0.0 6.1.0-beta.1 3.0.1 6.1.0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7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / StoreApi / Schemas / V1 / CartSchema.php
woocommerce / src / StoreApi / Schemas / V1 Last commit date
AI 1 year ago Agentic 9 months ago AbstractAddressSchema.php 1 month ago AbstractSchema.php 2 months ago BatchSchema.php 2 years ago BillingAddressSchema.php 1 month ago CartCouponSchema.php 1 year ago CartExtensionsSchema.php 2 years ago CartFeeSchema.php 2 years ago CartItemSchema.php 2 months ago CartSchema.php 1 month ago CartShippingRateSchema.php 1 month ago CheckoutOrderSchema.php 2 years ago CheckoutSchema.php 1 month ago ErrorSchema.php 2 years ago ImageAttachmentSchema.php 5 months ago ItemSchema.php 1 year ago OrderCouponSchema.php 2 years ago OrderFeeSchema.php 2 years ago OrderItemSchema.php 2 months ago OrderSchema.php 3 months ago PatternsSchema.php 1 year ago ProductAttributeSchema.php 2 years ago ProductAttributeTermSchema.php 2 months ago ProductBrandSchema.php 1 year ago ProductCategorySchema.php 2 years ago ProductCollectionDataSchema.php 1 year ago ProductReviewSchema.php 2 years ago ProductSchema.php 1 month ago ShippingAddressSchema.php 2 years ago ShopperListItemSchema.php 2 months ago ShopperListSchema.php 2 months ago TermSchema.php 2 years ago
CartSchema.php
459 lines
1 <?php
2 namespace Automattic\WooCommerce\StoreApi\Schemas\V1;
3
4 use Automattic\WooCommerce\Enums\WeightUnit;
5 use Automattic\WooCommerce\Internal\Tax\TaxRateDataStore;
6 use Automattic\WooCommerce\Internal\Utilities\ProductUtil;
7 use Automattic\WooCommerce\StoreApi\SchemaController;
8 use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema;
9 use Automattic\WooCommerce\StoreApi\Utilities\CartController;
10
11 /**
12 * CartSchema class.
13 */
14 class CartSchema extends AbstractSchema {
15 /**
16 * The schema item name.
17 *
18 * @var string
19 */
20 protected $title = 'cart';
21
22 /**
23 * The schema item identifier.
24 *
25 * @var string
26 */
27 const IDENTIFIER = 'cart';
28
29 /**
30 * Item schema instance.
31 *
32 * @var CartItemSchema
33 */
34 public $item_schema;
35
36 /**
37 * Coupon schema instance.
38 *
39 * @var CartCouponSchema
40 */
41 public $coupon_schema;
42
43 /**
44 * Product item schema instance representing cross-sell items.
45 *
46 * @var ProductSchema
47 */
48 public $cross_sells_item_schema;
49
50 /**
51 * Fee schema instance.
52 *
53 * @var CartFeeSchema
54 */
55 public $fee_schema;
56
57 /**
58 * Shipping rates schema instance.
59 *
60 * @var CartShippingRateSchema
61 */
62 public $shipping_rate_schema;
63
64 /**
65 * Shipping address schema instance.
66 *
67 * @var ShippingAddressSchema
68 */
69 public $shipping_address_schema;
70
71 /**
72 * Billing address schema instance.
73 *
74 * @var BillingAddressSchema
75 */
76 public $billing_address_schema;
77
78 /**
79 * Error schema instance.
80 *
81 * @var ErrorSchema
82 */
83 public $error_schema;
84
85 /**
86 * Constructor.
87 *
88 * @param ExtendSchema $extend Rest Extending instance.
89 * @param SchemaController $controller Schema Controller instance.
90 */
91 public function __construct( ExtendSchema $extend, SchemaController $controller ) {
92 parent::__construct( $extend, $controller );
93 $this->item_schema = $this->controller->get( CartItemSchema::IDENTIFIER );
94 $this->cross_sells_item_schema = $this->controller->get( ProductSchema::IDENTIFIER );
95 $this->coupon_schema = $this->controller->get( CartCouponSchema::IDENTIFIER );
96 $this->fee_schema = $this->controller->get( CartFeeSchema::IDENTIFIER );
97 $this->shipping_rate_schema = $this->controller->get( CartShippingRateSchema::IDENTIFIER );
98 $this->shipping_address_schema = $this->controller->get( ShippingAddressSchema::IDENTIFIER );
99 $this->billing_address_schema = $this->controller->get( BillingAddressSchema::IDENTIFIER );
100 $this->error_schema = $this->controller->get( ErrorSchema::IDENTIFIER );
101 }
102
103 /**
104 * Cart schema properties.
105 *
106 * @return array
107 */
108 public function get_properties() {
109 return [
110 'coupons' => [
111 'description' => __( 'List of applied cart coupons.', 'woocommerce' ),
112 'type' => 'array',
113 'context' => [ 'view', 'edit' ],
114 'readonly' => true,
115 'items' => [
116 'type' => 'object',
117 'properties' => $this->force_schema_readonly( $this->coupon_schema->get_properties() ),
118 ],
119 ],
120 'shipping_rates' => [
121 'description' => __( 'List of available shipping rates for the cart.', 'woocommerce' ),
122 'type' => 'array',
123 'context' => [ 'view', 'edit' ],
124 'readonly' => true,
125 'items' => [
126 'type' => 'object',
127 'properties' => $this->force_schema_readonly( $this->shipping_rate_schema->get_properties() ),
128 ],
129 ],
130 'shipping_address' => [
131 'description' => __( 'Current set shipping address for the customer.', 'woocommerce' ),
132 'type' => 'object',
133 'context' => [ 'view', 'edit' ],
134 'readonly' => true,
135 'properties' => $this->force_schema_readonly( $this->shipping_address_schema->get_properties() ),
136 ],
137 'billing_address' => [
138 'description' => __( 'Current set billing address for the customer.', 'woocommerce' ),
139 'type' => 'object',
140 'context' => [ 'view', 'edit' ],
141 'readonly' => true,
142 'properties' => $this->force_schema_readonly( $this->billing_address_schema->get_properties() ),
143 ],
144 'items' => [
145 'description' => __( 'List of cart items.', 'woocommerce' ),
146 'type' => 'array',
147 'context' => [ 'view', 'edit' ],
148 'readonly' => true,
149 'items' => [
150 'type' => 'object',
151 'properties' => $this->force_schema_readonly( $this->item_schema->get_properties() ),
152 ],
153 ],
154 'items_count' => [
155 'description' => __( 'Number of items in the cart.', 'woocommerce' ),
156 'type' => 'integer',
157 'context' => [ 'view', 'edit' ],
158 'readonly' => true,
159 ],
160 'items_weight' => [
161 'description' => __( 'Total weight (in grams) of all products in the cart.', 'woocommerce' ),
162 'type' => 'number',
163 'context' => [ 'view', 'edit' ],
164 'readonly' => true,
165 ],
166 'cross_sells' => [
167 'description' => __( 'List of cross-sells items related to cart items.', 'woocommerce' ),
168 'type' => 'array',
169 'context' => [ 'view', 'edit' ],
170 'readonly' => true,
171 'items' => [
172 'type' => 'object',
173 'properties' => $this->force_schema_readonly( $this->cross_sells_item_schema->get_properties() ),
174 ],
175 ],
176 'needs_payment' => [
177 'description' => __( 'True if the cart needs payment. False for carts with only free products and no shipping costs.', 'woocommerce' ),
178 'type' => 'boolean',
179 'context' => [ 'view', 'edit' ],
180 'readonly' => true,
181 ],
182 'needs_shipping' => [
183 'description' => __( 'True if the cart needs shipping. False for carts with only digital goods or stores with no shipping methods set-up.', 'woocommerce' ),
184 'type' => 'boolean',
185 'context' => [ 'view', 'edit' ],
186 'readonly' => true,
187 ],
188 'has_calculated_shipping' => [
189 'description' => __( 'True if the cart meets the criteria for showing shipping costs, and rates have been calculated and included in the totals.', 'woocommerce' ),
190 'type' => 'boolean',
191 'context' => [ 'view', 'edit' ],
192 'readonly' => true,
193 ],
194 'fees' => [
195 'description' => __( 'List of cart fees.', 'woocommerce' ),
196 'type' => 'array',
197 'context' => [ 'view', 'edit' ],
198 'readonly' => true,
199 'items' => [
200 'type' => 'object',
201 'properties' => $this->force_schema_readonly( $this->fee_schema->get_properties() ),
202 ],
203 ],
204 'totals' => [
205 'description' => __( 'Cart total amounts provided using the smallest unit of the currency.', 'woocommerce' ),
206 'type' => 'object',
207 'context' => [ 'view', 'edit' ],
208 'readonly' => true,
209 'properties' => array_merge(
210 $this->get_store_currency_properties(),
211 [
212 'total_items' => [
213 'description' => __( 'Total price of items in the cart.', 'woocommerce' ),
214 'type' => 'string',
215 'context' => [ 'view', 'edit' ],
216 'readonly' => true,
217 ],
218 'total_items_tax' => [
219 'description' => __( 'Total tax on items in the cart.', 'woocommerce' ),
220 'type' => 'string',
221 'context' => [ 'view', 'edit' ],
222 'readonly' => true,
223 ],
224 'total_fees' => [
225 'description' => __( 'Total price of any applied fees.', 'woocommerce' ),
226 'type' => 'string',
227 'context' => [ 'view', 'edit' ],
228 'readonly' => true,
229 ],
230 'total_fees_tax' => [
231 'description' => __( 'Total tax on fees.', 'woocommerce' ),
232 'type' => 'string',
233 'context' => [ 'view', 'edit' ],
234 'readonly' => true,
235 ],
236 'total_discount' => [
237 'description' => __( 'Total discount from applied coupons.', 'woocommerce' ),
238 'type' => 'string',
239 'context' => [ 'view', 'edit' ],
240 'readonly' => true,
241 ],
242 'total_discount_tax' => [
243 'description' => __( 'Total tax removed due to discount from applied coupons.', 'woocommerce' ),
244 'type' => 'string',
245 'context' => [ 'view', 'edit' ],
246 'readonly' => true,
247 ],
248 'total_shipping' => [
249 'description' => __( 'Total price of shipping. If shipping has not been calculated, a null response will be sent.', 'woocommerce' ),
250 'type' => [ 'string', 'null' ],
251 'context' => [ 'view', 'edit' ],
252 'readonly' => true,
253 ],
254 'total_shipping_tax' => [
255 'description' => __( 'Total tax on shipping. If shipping has not been calculated, a null response will be sent.', 'woocommerce' ),
256 'type' => [ 'string', 'null' ],
257 'context' => [ 'view', 'edit' ],
258 'readonly' => true,
259 ],
260 'total_price' => [
261 'description' => __( 'Total price the customer will pay.', 'woocommerce' ),
262 'type' => 'string',
263 'context' => [ 'view', 'edit' ],
264 'readonly' => true,
265 ],
266 'total_tax' => [
267 'description' => __( 'Total tax applied to items and shipping.', 'woocommerce' ),
268 'type' => 'string',
269 'context' => [ 'view', 'edit' ],
270 'readonly' => true,
271 ],
272 'tax_lines' => [
273 'description' => __( 'Lines of taxes applied to items and shipping.', 'woocommerce' ),
274 'type' => 'array',
275 'context' => [ 'view', 'edit' ],
276 'readonly' => true,
277 'items' => [
278 'type' => 'object',
279 'properties' => [
280 'name' => [
281 'description' => __( 'The name of the tax.', 'woocommerce' ),
282 'type' => 'string',
283 'context' => [ 'view', 'edit' ],
284 'readonly' => true,
285 ],
286 'price' => [
287 'description' => __( 'The amount of tax charged.', 'woocommerce' ),
288 'type' => 'string',
289 'context' => [ 'view', 'edit' ],
290 'readonly' => true,
291 ],
292 'rate' => [
293 'description' => __( 'The rate at which tax is applied.', 'woocommerce' ),
294 'type' => 'string',
295 'context' => [ 'view', 'edit' ],
296 'readonly' => true,
297 ],
298 ],
299 ],
300 ],
301 ]
302 ),
303 ],
304 'errors' => [
305 'description' => __( 'List of cart item errors, for example, items in the cart which are out of stock.', 'woocommerce' ),
306 'type' => 'array',
307 'context' => [ 'view', 'edit' ],
308 'readonly' => true,
309 'items' => [
310 'type' => 'object',
311 'properties' => $this->force_schema_readonly( $this->error_schema->get_properties() ),
312 ],
313 ],
314 'payment_methods' => [
315 'description' => __( 'List of available payment method IDs that can be used to process the order.', 'woocommerce' ),
316 'type' => 'array',
317 'context' => [ 'view', 'edit' ],
318 'readonly' => true,
319 ],
320 'payment_requirements' => [
321 'description' => __( 'List of required payment gateway features to process the order.', 'woocommerce' ),
322 'type' => 'array',
323 'context' => [ 'view', 'edit' ],
324 'readonly' => true,
325 ],
326 self::EXTENDING_KEY => $this->get_extended_schema( self::IDENTIFIER ),
327 ];
328 }
329
330 /**
331 * Convert a woo cart into an object suitable for the response.
332 *
333 * @param \WC_Cart $cart Cart class instance.
334 * @return array
335 */
336 public function get_item_response( $cart ) {
337 $controller = new CartController();
338
339 // Get cart errors first so if recalculations are performed, it's reflected in the response.
340 $cart_errors = $this->get_cart_errors( $cart );
341
342 // Get shipping packages to return in the response from the cart.
343 $shipping_packages = $cart->has_calculated_shipping() ? $controller->get_shipping_packages() : [];
344
345 // Get visible cross sells products.
346 $cross_sells = array();
347 $cross_sell_ids = $cart->get_cross_sells();
348 if ( ! empty( $cross_sell_ids ) ) {
349 // Prime caches to reduce future queries.
350 _prime_post_caches( $cross_sell_ids );
351 $cross_sells = array_values( array_filter( array_map( 'wc_get_product', $cross_sell_ids ), 'wc_products_array_filter_visible' ) );
352 /** @var \WC_Product[] $cross_sells */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort
353 }
354
355 $cart_all_items = $cart->get_cart();
356 $cart_line_items = array_values( array_filter( $cart_all_items, static fn( $item ) => ( $item['data'] ?? null ) instanceof \WC_Product ) );
357
358 // Batch-prime image attachment caches for cross-sells and cart line items in one query.
359 wc_get_container()->get( ProductUtil::class )->prime_image_caches( array_merge( $cross_sells, array_column( $cart_line_items, 'data' ) ) );
360
361 return [
362 'items' => $this->get_item_responses_from_schema( $this->item_schema, $cart_all_items ),
363 'coupons' => $this->get_item_responses_from_schema( $this->coupon_schema, $cart->get_applied_coupons() ),
364 'fees' => $this->get_item_responses_from_schema( $this->fee_schema, $cart->get_fees() ),
365 'totals' => (object) $this->prepare_currency_response( $this->get_totals( $cart ) ),
366 'shipping_address' => (object) $this->shipping_address_schema->get_item_response( wc()->customer ),
367 'billing_address' => (object) $this->billing_address_schema->get_item_response( wc()->customer ),
368 'needs_payment' => $cart->needs_payment(),
369 'needs_shipping' => $cart->needs_shipping(),
370 'payment_requirements' => $this->extend->get_payment_requirements(),
371 'has_calculated_shipping' => $cart->has_calculated_shipping(),
372 'shipping_rates' => $this->get_item_responses_from_schema( $this->shipping_rate_schema, $shipping_packages ),
373 'items_count' => $cart->get_cart_contents_count(),
374 'items_weight' => wc_get_weight( $cart->get_cart_contents_weight(), WeightUnit::GRAM ),
375 'cross_sells' => $this->get_item_responses_from_schema( $this->cross_sells_item_schema, $cross_sells ),
376 'errors' => $cart_errors,
377 'payment_methods' => array_values( wp_list_pluck( WC()->payment_gateways->get_available_payment_gateways(), 'id' ) ),
378 self::EXTENDING_KEY => $this->get_extended_data( self::IDENTIFIER ),
379 ];
380 }
381
382 /**
383 * Get total data.
384 *
385 * @param \WC_Cart $cart Cart class instance.
386 * @return array
387 */
388 protected function get_totals( $cart ) {
389 $decimals = wc_get_price_decimals();
390
391 return [
392 'total_items' => $this->prepare_money_response( $cart->get_subtotal(), $decimals ),
393 'total_items_tax' => $this->prepare_money_response( $cart->get_subtotal_tax(), $decimals ),
394 'total_fees' => $this->prepare_money_response( $cart->get_fee_total(), $decimals ),
395 'total_fees_tax' => $this->prepare_money_response( $cart->get_fee_tax(), $decimals ),
396 'total_discount' => $this->prepare_money_response( $cart->get_discount_total(), $decimals ),
397 'total_discount_tax' => $this->prepare_money_response( $cart->get_discount_tax(), $decimals ),
398 'total_shipping' => $cart->has_calculated_shipping() ? $this->prepare_money_response( $cart->get_shipping_total(), $decimals ) : null,
399 'total_shipping_tax' => $cart->has_calculated_shipping() ? $this->prepare_money_response( $cart->get_shipping_tax(), $decimals ) : null,
400 // Explicitly request context='edit'; default ('view') will render total as markup.
401 'total_price' => $this->prepare_money_response( $cart->get_total( 'edit' ), $decimals ),
402 'total_tax' => $this->prepare_money_response( $cart->get_total_tax(), $decimals ),
403 'tax_lines' => $this->get_tax_lines( $cart ),
404 ];
405 }
406
407 /**
408 * Get tax lines from the cart and format to match schema.
409 *
410 * @param \WC_Cart $cart Cart class instance.
411 * @return array
412 */
413 protected function get_tax_lines( $cart ) {
414 $tax_lines = [];
415
416 if ( 'itemized' !== get_option( 'woocommerce_tax_total_display' ) ) {
417 return $tax_lines;
418 }
419
420 $cart_tax_totals = $cart->get_tax_totals();
421 $tax_rate_objects = wc_get_container()->get( TaxRateDataStore::class )->get_rate_objects_for_ids( array_column( $cart_tax_totals, 'tax_rate_id' ) );
422 $decimals = wc_get_price_decimals();
423
424 foreach ( $cart_tax_totals as $cart_tax_total ) {
425 $tax_lines[] = array(
426 'name' => $cart_tax_total->label,
427 'price' => $this->prepare_money_response( $cart_tax_total->amount, $decimals ),
428 'rate' => \WC_Tax::get_rate_percent( $tax_rate_objects[ $cart_tax_total->tax_rate_id ] ?? $cart_tax_total->tax_rate_id ),
429 );
430 }
431
432 return $tax_lines;
433 }
434
435 /**
436 * Get cart validation errors.
437 *
438 * @param \WC_Cart $cart Cart class instance.
439 * @return array
440 */
441 protected function get_cart_errors( $cart ) {
442 $controller = new CartController();
443 $errors = $controller->get_cart_errors();
444 $cart_errors = [];
445
446 foreach ( (array) $errors->errors as $code => $messages ) {
447 foreach ( (array) $messages as $message ) {
448 $cart_errors[] = new \WP_Error(
449 $code,
450 $message,
451 $errors->get_error_data( $code )
452 );
453 }
454 }
455
456 return array_values( array_map( [ $this->error_schema, 'get_item_response' ], $cart_errors ) );
457 }
458 }
459