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
OrderSchema.php
393 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Schemas\V1; |
| 3 | |
| 4 | use Automattic\WooCommerce\Enums\OrderItemType; |
| 5 | use Automattic\WooCommerce\StoreApi\SchemaController; |
| 6 | use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema; |
| 7 | use Automattic\WooCommerce\StoreApi\Utilities\OrderController; |
| 8 | |
| 9 | /** |
| 10 | * OrderSchema class. |
| 11 | */ |
| 12 | class OrderSchema extends AbstractSchema { |
| 13 | /** |
| 14 | * The schema item name. |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | protected $title = 'order'; |
| 19 | |
| 20 | /** |
| 21 | * The schema item identifier. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | const IDENTIFIER = 'order'; |
| 26 | |
| 27 | /** |
| 28 | * Item schema instance. |
| 29 | * |
| 30 | * @var OrderItemSchema |
| 31 | */ |
| 32 | public $item_schema; |
| 33 | |
| 34 | /** |
| 35 | * Order controller class instance. |
| 36 | * |
| 37 | * @var OrderController |
| 38 | */ |
| 39 | protected $order_controller; |
| 40 | |
| 41 | /** |
| 42 | * Coupon schema instance. |
| 43 | * |
| 44 | * @var OrderCouponSchema |
| 45 | */ |
| 46 | public $coupon_schema; |
| 47 | |
| 48 | /** |
| 49 | * Product item schema instance representing cross-sell items. |
| 50 | * |
| 51 | * @var ProductSchema |
| 52 | */ |
| 53 | public $cross_sells_item_schema; |
| 54 | |
| 55 | /** |
| 56 | * Fee schema instance. |
| 57 | * |
| 58 | * @var OrderFeeSchema |
| 59 | */ |
| 60 | public $fee_schema; |
| 61 | |
| 62 | /** |
| 63 | * Shipping rates schema instance. |
| 64 | * |
| 65 | * @var CartShippingRateSchema |
| 66 | */ |
| 67 | public $shipping_rate_schema; |
| 68 | |
| 69 | /** |
| 70 | * Shipping address schema instance. |
| 71 | * |
| 72 | * @var ShippingAddressSchema |
| 73 | */ |
| 74 | public $shipping_address_schema; |
| 75 | |
| 76 | /** |
| 77 | * Billing address schema instance. |
| 78 | * |
| 79 | * @var BillingAddressSchema |
| 80 | */ |
| 81 | public $billing_address_schema; |
| 82 | |
| 83 | /** |
| 84 | * Error schema instance. |
| 85 | * |
| 86 | * @var ErrorSchema |
| 87 | */ |
| 88 | public $error_schema; |
| 89 | |
| 90 | /** |
| 91 | * Constructor. |
| 92 | * |
| 93 | * @param ExtendSchema $extend Rest Extending instance. |
| 94 | * @param SchemaController $controller Schema Controller instance. |
| 95 | */ |
| 96 | public function __construct( ExtendSchema $extend, SchemaController $controller ) { |
| 97 | parent::__construct( $extend, $controller ); |
| 98 | $this->item_schema = $this->controller->get( OrderItemSchema::IDENTIFIER ); |
| 99 | $this->coupon_schema = $this->controller->get( OrderCouponSchema::IDENTIFIER ); |
| 100 | $this->fee_schema = $this->controller->get( OrderFeeSchema::IDENTIFIER ); |
| 101 | $this->shipping_rate_schema = $this->controller->get( CartShippingRateSchema::IDENTIFIER ); |
| 102 | $this->shipping_address_schema = $this->controller->get( ShippingAddressSchema::IDENTIFIER ); |
| 103 | $this->billing_address_schema = $this->controller->get( BillingAddressSchema::IDENTIFIER ); |
| 104 | $this->error_schema = $this->controller->get( ErrorSchema::IDENTIFIER ); |
| 105 | $this->order_controller = new OrderController(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Order schema properties. |
| 110 | * |
| 111 | * @return array |
| 112 | */ |
| 113 | public function get_properties() { |
| 114 | return [ |
| 115 | 'id' => [ |
| 116 | 'description' => __( 'The order ID.', 'woocommerce' ), |
| 117 | 'type' => 'integer', |
| 118 | 'context' => [ 'view', 'edit' ], |
| 119 | 'readonly' => true, |
| 120 | ], |
| 121 | 'items' => [ |
| 122 | 'description' => __( 'Line items data.', 'woocommerce' ), |
| 123 | 'type' => 'array', |
| 124 | 'context' => [ 'view', 'edit' ], |
| 125 | 'items' => [ |
| 126 | 'type' => 'object', |
| 127 | 'properties' => $this->force_schema_readonly( $this->item_schema->get_properties() ), |
| 128 | ], |
| 129 | ], |
| 130 | 'totals' => [ |
| 131 | 'description' => __( 'Order totals.', 'woocommerce' ), |
| 132 | 'type' => 'object', |
| 133 | 'context' => [ 'view', 'edit' ], |
| 134 | 'readonly' => true, |
| 135 | 'properties' => array_merge( |
| 136 | $this->get_store_currency_properties(), |
| 137 | [ |
| 138 | 'subtotal' => [ |
| 139 | 'description' => __( 'Subtotal of the order.', 'woocommerce' ), |
| 140 | 'type' => 'string', |
| 141 | 'context' => [ 'view', 'edit' ], |
| 142 | 'readonly' => true, |
| 143 | ], |
| 144 | 'total_discount' => [ |
| 145 | 'description' => __( 'Total discount from applied coupons.', 'woocommerce' ), |
| 146 | 'type' => 'string', |
| 147 | 'context' => [ 'view', 'edit' ], |
| 148 | 'readonly' => true, |
| 149 | ], |
| 150 | 'total_shipping' => [ |
| 151 | 'description' => __( 'Total price of shipping.', 'woocommerce' ), |
| 152 | 'type' => [ 'string', 'null' ], |
| 153 | 'context' => [ 'view', 'edit' ], |
| 154 | 'readonly' => true, |
| 155 | ], |
| 156 | 'total_fees' => [ |
| 157 | 'description' => __( 'Total price of any applied fees.', 'woocommerce' ), |
| 158 | 'type' => 'string', |
| 159 | 'context' => [ 'view', 'edit' ], |
| 160 | 'readonly' => true, |
| 161 | ], |
| 162 | 'total_tax' => [ |
| 163 | 'description' => __( 'Total tax applied to the order.', 'woocommerce' ), |
| 164 | 'type' => 'string', |
| 165 | 'context' => [ 'view', 'edit' ], |
| 166 | 'readonly' => true, |
| 167 | ], |
| 168 | 'total_refund' => [ |
| 169 | 'description' => __( 'Total refund applied to the order.', 'woocommerce' ), |
| 170 | 'type' => 'string', |
| 171 | 'context' => [ 'view', 'edit' ], |
| 172 | 'readonly' => true, |
| 173 | ], |
| 174 | 'total_price' => [ |
| 175 | 'description' => __( 'Total price the customer will pay.', 'woocommerce' ), |
| 176 | 'type' => 'string', |
| 177 | 'context' => [ 'view', 'edit' ], |
| 178 | 'readonly' => true, |
| 179 | ], |
| 180 | 'total_items' => [ |
| 181 | 'description' => __( 'Total price of items in the order.', 'woocommerce' ), |
| 182 | 'type' => 'string', |
| 183 | 'context' => [ 'view', 'edit' ], |
| 184 | 'readonly' => true, |
| 185 | ], |
| 186 | 'total_items_tax' => [ |
| 187 | 'description' => __( 'Total tax on items in the order.', 'woocommerce' ), |
| 188 | 'type' => 'string', |
| 189 | 'context' => [ 'view', 'edit' ], |
| 190 | 'readonly' => true, |
| 191 | ], |
| 192 | 'total_fees_tax' => [ |
| 193 | 'description' => __( 'Total tax on fees.', 'woocommerce' ), |
| 194 | 'type' => 'string', |
| 195 | 'context' => [ 'view', 'edit' ], |
| 196 | 'readonly' => true, |
| 197 | ], |
| 198 | 'total_discount_tax' => [ |
| 199 | 'description' => __( 'Total tax removed due to discount from applied coupons.', 'woocommerce' ), |
| 200 | 'type' => 'string', |
| 201 | 'context' => [ 'view', 'edit' ], |
| 202 | 'readonly' => true, |
| 203 | ], |
| 204 | 'total_shipping_tax' => [ |
| 205 | 'description' => __( 'Total tax on shipping. If shipping has not been calculated, a null response will be sent.', 'woocommerce' ), |
| 206 | 'type' => [ 'string', 'null' ], |
| 207 | 'context' => [ 'view', 'edit' ], |
| 208 | 'readonly' => true, |
| 209 | ], |
| 210 | 'tax_lines' => [ |
| 211 | 'description' => __( 'Lines of taxes applied to items and shipping.', 'woocommerce' ), |
| 212 | 'type' => 'array', |
| 213 | 'context' => [ 'view', 'edit' ], |
| 214 | 'readonly' => true, |
| 215 | 'items' => [ |
| 216 | 'type' => 'object', |
| 217 | 'properties' => [ |
| 218 | 'name' => [ |
| 219 | 'description' => __( 'The name of the tax.', 'woocommerce' ), |
| 220 | 'type' => 'string', |
| 221 | 'context' => [ 'view', 'edit' ], |
| 222 | 'readonly' => true, |
| 223 | ], |
| 224 | 'price' => [ |
| 225 | 'description' => __( 'The amount of tax charged.', 'woocommerce' ), |
| 226 | 'type' => 'string', |
| 227 | 'context' => [ 'view', 'edit' ], |
| 228 | 'readonly' => true, |
| 229 | ], |
| 230 | 'rate' => [ |
| 231 | 'description' => __( 'The rate at which tax is applied.', 'woocommerce' ), |
| 232 | 'type' => 'string', |
| 233 | 'context' => [ 'view', 'edit' ], |
| 234 | 'readonly' => true, |
| 235 | ], |
| 236 | ], |
| 237 | ], |
| 238 | ], |
| 239 | ] |
| 240 | ), |
| 241 | ], |
| 242 | 'coupons' => [ |
| 243 | 'description' => __( 'List of applied cart coupons.', 'woocommerce' ), |
| 244 | 'type' => 'array', |
| 245 | 'context' => [ 'view', 'edit' ], |
| 246 | 'readonly' => true, |
| 247 | 'items' => [ |
| 248 | 'type' => 'object', |
| 249 | 'properties' => $this->force_schema_readonly( $this->coupon_schema->get_properties() ), |
| 250 | ], |
| 251 | ], |
| 252 | 'shipping_address' => [ |
| 253 | 'description' => __( 'Current set shipping address for the customer.', 'woocommerce' ), |
| 254 | 'type' => 'object', |
| 255 | 'context' => [ 'view', 'edit' ], |
| 256 | 'readonly' => true, |
| 257 | 'properties' => $this->force_schema_readonly( $this->shipping_address_schema->get_properties() ), |
| 258 | ], |
| 259 | 'billing_address' => [ |
| 260 | 'description' => __( 'Current set billing address for the customer.', 'woocommerce' ), |
| 261 | 'type' => 'object', |
| 262 | 'context' => [ 'view', 'edit' ], |
| 263 | 'readonly' => true, |
| 264 | 'properties' => $this->force_schema_readonly( $this->billing_address_schema->get_properties() ), |
| 265 | ], |
| 266 | 'needs_payment' => [ |
| 267 | 'description' => __( 'True if the cart needs payment. False for carts with only free products and no shipping costs.', 'woocommerce' ), |
| 268 | 'type' => 'boolean', |
| 269 | 'context' => [ 'view', 'edit' ], |
| 270 | 'readonly' => true, |
| 271 | ], |
| 272 | 'needs_shipping' => [ |
| 273 | 'description' => __( 'True if the cart needs shipping. False for carts with only digital goods or stores with no shipping methods set-up.', 'woocommerce' ), |
| 274 | 'type' => 'boolean', |
| 275 | 'context' => [ 'view', 'edit' ], |
| 276 | 'readonly' => true, |
| 277 | ], |
| 278 | 'errors' => [ |
| 279 | 'description' => __( 'List of cart item errors, for example, items in the cart which are out of stock.', 'woocommerce' ), |
| 280 | 'type' => 'array', |
| 281 | 'context' => [ 'view', 'edit' ], |
| 282 | 'readonly' => true, |
| 283 | 'items' => [ |
| 284 | 'type' => 'object', |
| 285 | 'properties' => $this->force_schema_readonly( $this->error_schema->get_properties() ), |
| 286 | ], |
| 287 | ], |
| 288 | 'payment_requirements' => [ |
| 289 | 'description' => __( 'List of required payment gateway features to process the order.', 'woocommerce' ), |
| 290 | 'type' => 'array', |
| 291 | 'context' => [ 'view', 'edit' ], |
| 292 | 'readonly' => true, |
| 293 | ], |
| 294 | 'status' => [ |
| 295 | 'description' => __( 'Status of the order.', 'woocommerce' ), |
| 296 | 'type' => 'string', |
| 297 | 'context' => [ 'view', 'edit' ], |
| 298 | 'readonly' => true, |
| 299 | ], |
| 300 | ]; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Get an order for response. |
| 305 | * |
| 306 | * @param \WC_Order $order Order instance. |
| 307 | * @return array |
| 308 | */ |
| 309 | public function get_item_response( $order ) { |
| 310 | $order_id = $order->get_id(); |
| 311 | $errors = []; |
| 312 | $failed_order_stock_error = $this->order_controller->get_failed_order_stock_error( $order_id ); |
| 313 | if ( $failed_order_stock_error ) { |
| 314 | $errors[] = $failed_order_stock_error; |
| 315 | } |
| 316 | |
| 317 | return [ |
| 318 | 'id' => $order_id, |
| 319 | 'status' => $order->get_status(), |
| 320 | 'items' => $this->get_item_responses_from_schema( $this->item_schema, $order->get_items() ), |
| 321 | 'coupons' => $this->get_item_responses_from_schema( $this->coupon_schema, $order->get_items( OrderItemType::COUPON ) ), |
| 322 | 'fees' => $this->get_item_responses_from_schema( $this->fee_schema, $order->get_items( OrderItemType::FEE ) ), |
| 323 | 'totals' => (object) $this->prepare_currency_response( $this->get_totals( $order ) ), |
| 324 | 'shipping_address' => (object) $this->shipping_address_schema->get_item_response( $order ), |
| 325 | 'billing_address' => (object) $this->billing_address_schema->get_item_response( $order ), |
| 326 | 'needs_payment' => $order->needs_payment(), |
| 327 | 'needs_shipping' => $order->needs_shipping_address(), |
| 328 | 'payment_requirements' => $this->extend->get_payment_requirements(), |
| 329 | 'errors' => $errors, |
| 330 | ]; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Get total data. |
| 335 | * |
| 336 | * @param \WC_Order $order Order instance. |
| 337 | * @return array |
| 338 | */ |
| 339 | protected function get_totals( $order ) { |
| 340 | return [ |
| 341 | 'subtotal' => $this->prepare_money_response( $order->get_subtotal() ), |
| 342 | 'total_discount' => $this->prepare_money_response( $order->get_total_discount() ), |
| 343 | 'total_shipping' => $this->prepare_money_response( $order->get_total_shipping() ), |
| 344 | 'total_fees' => $this->prepare_money_response( $order->get_total_fees() ), |
| 345 | 'total_tax' => $this->prepare_money_response( $order->get_total_tax() ), |
| 346 | 'total_refund' => $this->prepare_money_response( $order->get_total_refunded() ), |
| 347 | 'total_price' => $this->prepare_money_response( $order->get_total() ), |
| 348 | 'total_items' => $this->prepare_money_response( |
| 349 | array_sum( |
| 350 | array_map( |
| 351 | function( $item ) { |
| 352 | return $item->get_total(); |
| 353 | }, |
| 354 | array_values( $order->get_items( OrderItemType::LINE_ITEM ) ) |
| 355 | ) |
| 356 | ) |
| 357 | ), |
| 358 | 'total_items_tax' => $this->prepare_money_response( |
| 359 | array_sum( |
| 360 | array_map( |
| 361 | function( $item ) { |
| 362 | return $item->get_tax_total(); |
| 363 | }, |
| 364 | array_values( $order->get_items( OrderItemType::TAX ) ) |
| 365 | ) |
| 366 | ) |
| 367 | ), |
| 368 | 'total_fees_tax' => $this->prepare_money_response( |
| 369 | array_sum( |
| 370 | array_map( |
| 371 | function( $item ) { |
| 372 | return $item->get_total_tax(); |
| 373 | }, |
| 374 | array_values( $order->get_items( OrderItemType::FEE ) ) |
| 375 | ) |
| 376 | ) |
| 377 | ), |
| 378 | 'total_discount_tax' => $this->prepare_money_response( $order->get_discount_tax() ), |
| 379 | 'total_shipping_tax' => $this->prepare_money_response( $order->get_shipping_tax() ), |
| 380 | 'tax_lines' => array_map( |
| 381 | function( $item ) { |
| 382 | return [ |
| 383 | 'name' => $item->get_label(), |
| 384 | 'price' => $this->prepare_money_response( $item->get_tax_total() ), |
| 385 | 'rate' => strval( $item->get_rate_percent() ), |
| 386 | ]; |
| 387 | }, |
| 388 | array_values( $order->get_items( OrderItemType::TAX ) ) |
| 389 | ), |
| 390 | ]; |
| 391 | } |
| 392 | } |
| 393 |