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