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 |