| @@ -12,8 +12,9 @@ | ||
| 12 | 12 | if ( ! class_exists( 'WC_REST_Orders_Controller' ) ) { |
| 13 | 13 | return; |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | +use WCPOS\WooCommercePOS\Services\Permission_Rules; | |
| 16 | 17 | use Automattic\WooCommerce\Utilities\OrderUtil; |
| 17 | 18 | use Exception; |
| 18 | 19 | use WC_Abstract_Order; |
| 19 | 20 | use WC_Data; |
| @@ -20,21 +21,20 @@ | ||
| 20 | 21 | use WC_Email_Customer_Invoice; |
| 21 | 22 | use WC_Order; |
| 22 | 23 | use WC_Order_Item; |
| 23 | 24 | use WC_Order_Item_Fee; |
| 24 | -use WC_Order_Item_Product; | |
| 25 | 25 | use WC_REST_Orders_Controller; |
| 26 | 26 | use WC_Tax; |
| 27 | 27 | use WCPOS\WooCommercePOS\Logger; |
| 28 | +use WCPOS\WooCommercePOS\Services\Order_Write_Intent; | |
| 28 | 29 | use WCPOS\WooCommercePOS\Services\Pos_Order_Audit; |
| 29 | 30 | use WCPOS\WooCommercePOS\Services\Settings as SettingsService; |
| 30 | 31 | use WCPOS\WooCommercePOS\Services\Stock_Validator; |
| 31 | 32 | use WCPOS\WooCommercePOS\Services\Tax_Id_Reader; |
| 32 | -use WCPOS\WooCommercePOS\Services\Tax_Id_Types; | |
| 33 | -use WCPOS\WooCommercePOS\Services\Tax_Id_Writer; | |
| 34 | 33 | use WCPOS\WooCommercePOS\Sync\Collection_Rules; |
| 35 | 34 | use WCPOS\WooCommercePOS\Sync\Collection_Rules_Plan; |
| 36 | 35 | use WCPOS\WooCommercePOS\Sync\Order_Serializer; |
| 36 | +use WCPOS\WooCommercePOS\Sync\Order_Write_Payload; | |
| 37 | 37 | use const WCPOS\WooCommercePOS\PLUGIN_NAME; |
| 38 | 38 | use const WCPOS\WooCommercePOS\VERSION; |
| 39 | 39 | use WP_Error; |
| 40 | 40 | use WP_REST_Request; |
| @@ -84,25 +84,26 @@ | ||
| 84 | 84 | */ |
| 85 | 85 | protected $wcpos_request; |
| 86 | 86 | |
| 87 | 87 | /** |
| 88 | - * The order object being created by the current request. | |
| 88 | + * Whether High Performance Orders is enabled. | |
| 89 | 89 | * |
| 90 | - * @var WC_Abstract_Order|null | |
| 90 | + * @var bool | |
| 91 | 91 | */ |
| 92 | - private $creating_order; | |
| 92 | + private $hpos_enabled = false; | |
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | - * Whether High Performance Orders is enabled. | |
| 95 | + * Shared date validation and tax-ID persistence for the order write lanes. | |
| 96 | 96 | * |
| 97 | - * @var bool | |
| 97 | + * @var Order_Write_Payload | |
| 98 | 98 | */ |
| 99 | - private $hpos_enabled = false; | |
| 99 | + private $order_payload; | |
| 100 | 100 | |
| 101 | 101 | /** |
| 102 | 102 | * Constructor. |
| 103 | 103 | */ |
| 104 | 104 | public function __construct() { |
| 105 | + $this->order_payload = new Order_Write_Payload(); | |
| 105 | 106 | $this->hpos_enabled = class_exists( OrderUtil::class ) && OrderUtil::custom_orders_table_usage_is_enabled(); |
| 106 | 107 | |
| 107 | 108 | if ( method_exists( parent::class, '__construct' ) ) { |
| 108 | 109 | parent::__construct(); |
| @@ -145,72 +146,22 @@ | ||
| 145 | 146 | $request->set_param( 'set_paid', $set_paid ); |
| 146 | 147 | } |
| 147 | 148 | } |
| 148 | 149 | |
| 149 | - /** | |
| 150 | - * Check if the current user can update an order. | |
| 150 | + /** Delegate the edit decision, preserving WooCommerce's request-dependent checks. | |
| 151 | 151 | * |
| 152 | - * Overrides the parent to fix HPOS compatibility. When HPOS is enabled with | |
| 153 | - * sync disabled, get_post() returns a shop_order_placehold post type that has | |
| 154 | - * map_meta_cap = false and no capability_type, causing WordPress to check the | |
| 155 | - * generic 'edit_post' capability instead of 'edit_shop_order'. Non-admin roles | |
| 156 | - * like cashier have 'edit_shop_orders' but not the generic 'edit_posts', so the | |
| 157 | - * permission check fails. | |
| 158 | - * | |
| 159 | - * @param WP_REST_Request $request Full details about the request. | |
| 160 | - * | |
| 161 | - * @return bool|WP_Error | |
| 152 | + * @param \WP_REST_Request $request Full request details. | |
| 162 | 153 | */ |
| 163 | 154 | public function update_item_permissions_check( $request ) { |
| 164 | - $result = parent::update_item_permissions_check( $request ); | |
| 165 | - | |
| 166 | - if ( ! is_wp_error( $result ) ) { | |
| 167 | - return $result; | |
| 168 | - } | |
| 169 | - | |
| 170 | - // Parent check failed - try direct capability check for HPOS compatibility. | |
| 171 | - $id = (int) $request['id']; | |
| 172 | - $order = wc_get_order( $id ); | |
| 173 | - | |
| 174 | - if ( ! $order ) { | |
| 175 | - return $result; | |
| 176 | - } | |
| 177 | - | |
| 178 | - if ( ! current_user_can( 'edit_shop_orders' ) ) { | |
| 179 | - return $result; | |
| 180 | - } | |
| 181 | - | |
| 182 | - return true; | |
| 155 | + return Permission_Rules::verdict( 'orders', 'edit', (int) $request['id'], 0, 'v1', $request->get_params() ); | |
| 183 | 156 | } |
| 184 | 157 | |
| 185 | - /** | |
| 186 | - * Check if the current user can delete an order. | |
| 158 | + /** Delegate the delete decision, preserving WooCommerce's request-dependent checks. | |
| 187 | 159 | * |
| 188 | - * Same HPOS fix as update_item_permissions_check. | |
| 189 | - * | |
| 190 | - * @param WP_REST_Request $request Full details about the request. | |
| 191 | - * | |
| 192 | - * @return bool|WP_Error | |
| 160 | + * @param \WP_REST_Request $request Full request details. | |
| 193 | 161 | */ |
| 194 | 162 | public function delete_item_permissions_check( $request ) { |
| 195 | - $result = parent::delete_item_permissions_check( $request ); | |
| 196 | - | |
| 197 | - if ( ! is_wp_error( $result ) ) { | |
| 198 | - return $result; | |
| 199 | - } | |
| 200 | - | |
| 201 | - $id = (int) $request['id']; | |
| 202 | - $order = wc_get_order( $id ); | |
| 203 | - | |
| 204 | - if ( ! $order ) { | |
| 205 | - return $result; | |
| 206 | - } | |
| 207 | - | |
| 208 | - if ( ! current_user_can( 'delete_shop_orders' ) ) { | |
| 209 | - return $result; | |
| 210 | - } | |
| 211 | - | |
| 212 | - return true; | |
| 163 | + return Permission_Rules::verdict( 'orders', 'delete', (int) $request['id'], 0, 'v1', $request->get_params() ); | |
| 213 | 164 | } |
| 214 | 165 | |
| 215 | 166 | /** |
| 216 | 167 | * Delete a single order. |
| @@ -336,9 +287,9 @@ | ||
| 336 | 287 | ), |
| 337 | 288 | ) |
| 338 | 289 | ), |
| 339 | 290 | ), |
| 340 | - 'schema' => array(), | |
| 291 | + 'schema' => array( $this, 'wcpos_get_public_send_email_schema' ), | |
| 341 | 292 | ) |
| 342 | 293 | ); |
| 343 | 294 | |
| 344 | 295 | register_rest_route( |
| @@ -356,8 +307,10 @@ | ||
| 356 | 307 | } |
| 357 | 308 | |
| 358 | 309 | /** |
| 359 | 310 | * Add custom fields to the order schema. |
| 311 | + * | |
| 312 | + * Email, nullable parent_name, and decimal quantity relaxations let raw POS documents pass validation before payload shaping. | |
| 360 | 313 | */ |
| 361 | 314 | public function get_item_schema() { |
| 362 | 315 | $schema = parent::get_item_schema(); |
| 363 | 316 | |
| @@ -366,30 +319,9 @@ | ||
| 366 | 319 | $schema['properties']['tax_ids'] = array( |
| 367 | 320 | 'description' => __( 'Customer tax IDs snapshotted at sale time.', 'woocommerce-pos' ), |
| 368 | 321 | 'type' => 'array', |
| 369 | 322 | 'context' => array( 'view', 'edit' ), |
| 370 | - 'items' => array( | |
| 371 | - 'type' => 'object', | |
| 372 | - 'properties' => array( | |
| 373 | - 'type' => array( | |
| 374 | - 'type' => 'string', | |
| 375 | - 'enum' => Tax_Id_Types::all_types(), | |
| 376 | - 'description' => /* translators: REST API schema field label or error message. */ __( 'Tax ID type.', 'woocommerce-pos' ), | |
| 377 | - ), | |
| 378 | - 'value' => array( | |
| 379 | - 'type' => 'string', | |
| 380 | - 'description' => /* translators: REST API schema field label or error message. */ __( 'Tax ID value.', 'woocommerce-pos' ), | |
| 381 | - ), | |
| 382 | - 'country' => array( | |
| 383 | - 'type' => array( 'string', 'null' ), | |
| 384 | - 'description' => __( 'ISO 3166-1 alpha-2 country code.', 'woocommerce-pos' ), | |
| 385 | - ), | |
| 386 | - 'label' => array( | |
| 387 | - 'type' => array( 'string', 'null' ), | |
| 388 | - 'description' => /* translators: REST API schema field label or error message. */ __( 'Optional human-readable label.', 'woocommerce-pos' ), | |
| 389 | - ), | |
| 390 | - ), | |
| 391 | - ), | |
| 323 | + 'items' => array( 'type' => 'object' ), | |
| 392 | 324 | ); |
| 393 | 325 | |
| 394 | 326 | // Check and remove email format validation from the billing property. |
| 395 | 327 | if ( isset( $schema['properties']['billing']['properties']['email']['format'] ) ) { |
| @@ -482,122 +414,50 @@ | ||
| 482 | 414 | if ( isset( $request['meta_data'] ) && \is_array( $request['meta_data'] ) ) { |
| 483 | 415 | $request->set_param( 'meta_data', Pos_Order_Audit::sanitize_create_meta( $request['meta_data'] ) ); |
| 484 | 416 | } |
| 485 | 417 | |
| 486 | - $this->creating_order = null; | |
| 418 | + $this->wcpos_shape_request_payload( $request, $this->order_payload->for_create( $request->get_params() ) ); | |
| 487 | 419 | |
| 488 | - add_filter( 'woocommerce_rest_pre_insert_shop_order_object', array( $this, 'wcpos_track_creating_order' ), 9, 3 ); | |
| 489 | - add_filter( 'woocommerce_rest_pre_insert_shop_order_object', array( $this, 'wcpos_preserve_client_created_date_gmt' ), 10, 3 ); | |
| 420 | + $response = Order_Write_Intent::open( | |
| 421 | + array( | |
| 422 | + 'operation' => 'create', | |
| 423 | + 'requested_status' => (string) $request->get_param( 'status' ), | |
| 424 | + 'set_paid' => $request->has_param( 'set_paid' ) && rest_sanitize_boolean( $request->get_param( 'set_paid' ) ), | |
| 425 | + ), | |
| 426 | + function () use ( $request ) { | |
| 427 | + add_filter( 'woocommerce_rest_pre_insert_shop_order_object', array( $this, 'wcpos_preserve_client_created_date_gmt' ), 10, 3 ); | |
| 428 | + try { | |
| 429 | + // Proceed with the parent method to handle the creation. | |
| 430 | + return parent::create_item( $request ); | |
| 431 | + } finally { | |
| 432 | + remove_filter( 'woocommerce_rest_pre_insert_shop_order_object', array( $this, 'wcpos_preserve_client_created_date_gmt' ), 10 ); | |
| 433 | + } | |
| 434 | + } | |
| 435 | + ); | |
| 490 | 436 | |
| 491 | - try { | |
| 492 | - // Proceed with the parent method to handle the creation. | |
| 493 | - $response = parent::create_item( $request ); | |
| 494 | - } finally { | |
| 495 | - remove_filter( 'woocommerce_rest_pre_insert_shop_order_object', array( $this, 'wcpos_preserve_client_created_date_gmt' ), 10 ); | |
| 496 | - remove_filter( 'woocommerce_rest_pre_insert_shop_order_object', array( $this, 'wcpos_track_creating_order' ), 9 ); | |
| 497 | - $this->creating_order = null; | |
| 498 | - } | |
| 437 | + $this->wcpos_refresh_tax_ids_response( $response, $request, true ); | |
| 499 | 438 | |
| 500 | - $this->wcpos_snapshot_tax_ids_to_order( $response, $request, true ); | |
| 501 | - | |
| 502 | 439 | return $response; |
| 503 | 440 | } |
| 504 | 441 | |
| 505 | 442 | /** |
| 506 | - * Record the exact order object prepared for this create request. | |
| 443 | + * Preserve the client creation time using the shared payload validator. | |
| 507 | 444 | * |
| 508 | - * @param WC_Data|WP_Error $order Order object prepared by WooCommerce. | |
| 445 | + * @param WC_Data|WP_Error $order Prepared order. | |
| 509 | 446 | * @param WP_REST_Request $request Request object. |
| 510 | - * @param bool $creating Whether a new order is being created. | |
| 511 | - * | |
| 447 | + * @param bool $creating Whether this is a create. | |
| 512 | 448 | * @return WC_Data|WP_Error |
| 513 | 449 | */ |
| 514 | - public function wcpos_track_creating_order( $order, WP_REST_Request $request, bool $creating ) { | |
| 515 | - if ( $creating && $order instanceof WC_Abstract_Order ) { | |
| 516 | - $this->creating_order = $order; | |
| 517 | - } | |
| 518 | - | |
| 519 | - return $order; | |
| 520 | - } | |
| 521 | - | |
| 522 | - /** | |
| 523 | - * Preserve client-provided order creation time for offline-created orders. | |
| 524 | - * | |
| 525 | - * WooCommerce marks date_created/date_created_gmt as read-only in the REST | |
| 526 | - * schema, so those fields are removed before the parent controller prepares | |
| 527 | - * the order. WCPOS clients can create orders offline and later sync the full | |
| 528 | - * local document; read the raw JSON payload here so the server keeps the | |
| 529 | - * transaction time instead of the sync time. | |
| 530 | - * | |
| 531 | - * @param WC_Data|WP_Error $order Order object prepared by WooCommerce. | |
| 532 | - * @param WP_REST_Request $request Request object. | |
| 533 | - * @param bool $creating Whether a new order is being created. | |
| 534 | - * | |
| 535 | - * @return WC_Data|WP_Error | |
| 536 | - */ | |
| 537 | 450 | public function wcpos_preserve_client_created_date_gmt( $order, WP_REST_Request $request, bool $creating ) { |
| 538 | 451 | if ( ! $creating || ! ( $order instanceof WC_Abstract_Order ) ) { |
| 539 | 452 | return $order; |
| 540 | 453 | } |
| 541 | - $this->creating_order = $order; | |
| 542 | - | |
| 543 | 454 | $body = $request->get_json_params(); |
| 544 | - | |
| 545 | - if ( ! isset( $body['date_created_gmt'] ) ) { | |
| 546 | - return $order; | |
| 455 | + $timestamp = $this->order_payload->validate_client_created_gmt( is_array( $body ) ? $body : array() ); | |
| 456 | + if ( is_wp_error( $timestamp ) || null === $timestamp ) { | |
| 457 | + return is_wp_error( $timestamp ) ? $timestamp : $order; | |
| 547 | 458 | } |
| 548 | - | |
| 549 | - if ( ! is_scalar( $body['date_created_gmt'] ) ) { | |
| 550 | - return new WP_Error( | |
| 551 | - 'woocommerce_pos_rest_invalid_date_created_gmt', | |
| 552 | - __( 'date_created_gmt must be a valid ISO 8601 UTC date.', 'woocommerce-pos' ), | |
| 553 | - array( 'status' => 400 ) | |
| 554 | - ); | |
| 555 | - } | |
| 556 | - | |
| 557 | - $client_date_gmt = wc_clean( wp_unslash( (string) $body['date_created_gmt'] ) ); | |
| 558 | - | |
| 559 | - if ( '' === $client_date_gmt ) { | |
| 560 | - return $order; | |
| 561 | - } | |
| 562 | - | |
| 563 | - if ( 1 !== preg_match( '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z?$/i', $client_date_gmt ) ) { | |
| 564 | - return new WP_Error( | |
| 565 | - 'woocommerce_pos_rest_invalid_date_created_gmt', | |
| 566 | - __( 'date_created_gmt must be a valid ISO 8601 UTC date.', 'woocommerce-pos' ), | |
| 567 | - array( 'status' => 400 ) | |
| 568 | - ); | |
| 569 | - } | |
| 570 | - | |
| 571 | - // WooCommerce serializes *_gmt fields without a timezone suffix; treat bare values as UTC. | |
| 572 | - $parse_date_gmt = 'Z' === strtoupper( substr( $client_date_gmt, -1 ) ) | |
| 573 | - ? $client_date_gmt | |
| 574 | - : $client_date_gmt . 'Z'; | |
| 575 | - $timestamp = rest_parse_date( | |
| 576 | - $parse_date_gmt, | |
| 577 | - true | |
| 578 | - ); | |
| 579 | - | |
| 580 | - if ( false === $timestamp ) { | |
| 581 | - return new WP_Error( | |
| 582 | - 'woocommerce_pos_rest_invalid_date_created_gmt', | |
| 583 | - __( 'date_created_gmt must be a valid ISO 8601 UTC date.', 'woocommerce-pos' ), | |
| 584 | - array( 'status' => 400 ) | |
| 585 | - ); | |
| 586 | - } | |
| 587 | - | |
| 588 | - $maximum_future_timestamp = time() + DAY_IN_SECONDS; | |
| 589 | - | |
| 590 | - if ( $timestamp > $maximum_future_timestamp ) { | |
| 591 | - return new WP_Error( | |
| 592 | - 'woocommerce_pos_rest_future_date_created_gmt', | |
| 593 | - __( 'date_created_gmt cannot be more than 24 hours in the future.', 'woocommerce-pos' ), | |
| 594 | - array( 'status' => 400 ) | |
| 595 | - ); | |
| 596 | - } | |
| 597 | - | |
| 598 | 459 | $order->set_date_created( $timestamp ); |
| 599 | - | |
| 600 | 460 | return $order; |
| 601 | 461 | } |
| 602 | 462 | |
| 603 | 463 | /** |
| @@ -631,30 +491,49 @@ | ||
| 631 | 491 | ) |
| 632 | 492 | ); |
| 633 | 493 | } |
| 634 | 494 | |
| 635 | - // Proceed with the parent method to handle the update. | |
| 636 | - $response = parent::update_item( $request ); | |
| 637 | - $this->wcpos_snapshot_tax_ids_to_order( $response, $request, false ); | |
| 495 | + $this->wcpos_shape_request_payload( $request, $this->order_payload->for_partial_update( (int) $request['id'], $request->get_params() ) ); | |
| 638 | 496 | |
| 497 | + $response = Order_Write_Intent::open( | |
| 498 | + array( | |
| 499 | + 'operation' => 'update', | |
| 500 | + 'id' => (int) $request['id'], | |
| 501 | + 'requested_status' => (string) $request->get_param( 'status' ), | |
| 502 | + 'set_paid' => $request->has_param( 'set_paid' ) && rest_sanitize_boolean( $request->get_param( 'set_paid' ) ), | |
| 503 | + ), | |
| 504 | + function () use ( $request ) { | |
| 505 | + return parent::update_item( $request ); | |
| 506 | + } | |
| 507 | + ); | |
| 508 | + $this->wcpos_refresh_tax_ids_response( $response, $request, false ); | |
| 509 | + | |
| 639 | 510 | return $response; |
| 640 | 511 | } |
| 641 | 512 | |
| 642 | 513 | /** |
| 643 | - * Persist tax_ids onto the order. | |
| 514 | + * Replace the request fields touched by the shared payload shaper. | |
| 644 | 515 | * |
| 645 | - * On create: if the request did not provide `tax_ids`, snapshot from the | |
| 646 | - * resolved customer record so the order is self-contained. If the request | |
| 647 | - * provided `tax_ids`, write those (cashier-entered tax IDs override). | |
| 516 | + * @param WP_REST_Request $request Validated request. | |
| 517 | + * @param array $shaped Shaped create or partial-update payload. | |
| 518 | + */ | |
| 519 | + private function wcpos_shape_request_payload( WP_REST_Request $request, array $shaped ): void { | |
| 520 | + // Neither for_create nor for_partial_update removes a top-level key; present-key replacement is sufficient. | |
| 521 | + foreach ( array( 'billing', 'line_items', 'shipping_lines', 'fee_lines', 'coupon_lines', 'meta_data' ) as $key ) { | |
| 522 | + if ( array_key_exists( $key, $shaped ) ) { | |
| 523 | + $request->set_param( $key, $shaped[ $key ] ); | |
| 524 | + } | |
| 525 | + } | |
| 526 | + } | |
| 527 | + | |
| 528 | + /** | |
| 529 | + * Adapt the parent response to the shared snapshot and refresh its tax_ids. | |
| 648 | 530 | * |
| 649 | - * On update: only write what the request explicitly provided; never | |
| 650 | - * re-snapshot, since editing a customer must not mutate historical orders. | |
| 651 | - * | |
| 652 | 531 | * @param mixed $response Response from parent controller. |
| 653 | 532 | * @param WP_REST_Request $request Original request. |
| 654 | 533 | * @param bool $is_create True for create, false for update. |
| 655 | 534 | */ |
| 656 | - protected function wcpos_snapshot_tax_ids_to_order( $response, WP_REST_Request $request, bool $is_create ): void { | |
| 535 | + private function wcpos_refresh_tax_ids_response( $response, WP_REST_Request $request, bool $is_create ): void { | |
| 657 | 536 | if ( ! ( $response instanceof WP_REST_Response ) ) { |
| 658 | 537 | return; |
| 659 | 538 | } |
| 660 | 539 | |
| @@ -662,161 +541,16 @@ | ||
| 662 | 541 | $order_id = isset( $data['id'] ) ? (int) $data['id'] : 0; |
| 663 | 542 | if ( $order_id <= 0 ) { |
| 664 | 543 | return; |
| 665 | 544 | } |
| 666 | - $order = \wc_get_order( $order_id ); | |
| 667 | - if ( ! $order ) { | |
| 668 | - return; | |
| 545 | + $tax_ids = $this->order_payload->persist_tax_ids( $order_id, $request->get_params(), $is_create ); | |
| 546 | + if ( null !== $tax_ids ) { | |
| 547 | + $data['tax_ids'] = $tax_ids; | |
| 548 | + $response->set_data( $data ); | |
| 669 | 549 | } |
| 670 | - | |
| 671 | - $tax_ids = $request->get_param( 'tax_ids' ); | |
| 672 | - $writer = new Tax_Id_Writer(); | |
| 673 | - | |
| 674 | - if ( \is_array( $tax_ids ) ) { | |
| 675 | - $writer->write_for_order( $order, $tax_ids ); | |
| 676 | - } elseif ( $is_create ) { | |
| 677 | - $customer_id = (int) $order->get_customer_id(); | |
| 678 | - if ( $customer_id > 0 ) { | |
| 679 | - $writer->snapshot_from_user_to_order( $order, $customer_id ); | |
| 680 | - } | |
| 681 | - } | |
| 682 | - | |
| 683 | - $data['tax_ids'] = ( new Tax_Id_Reader() )->read_for_order( $order ); | |
| 684 | - $response->set_data( $data ); | |
| 685 | 550 | } |
| 686 | 551 | |
| 687 | 552 | /** |
| 688 | - * Create or update a line item. | |
| 689 | - * | |
| 690 | - * @param array $posted Line item data. | |
| 691 | - * @param string $action 'create' to add line item or 'update' to update it. | |
| 692 | - * @param object $item Passed when updating an item. Null during creation. | |
| 693 | - * | |
| 694 | - * @throws \WC_REST_Exception Invalid data, server error. | |
| 695 | - * | |
| 696 | - * @return WC_Order_Item_Product | |
| 697 | - */ | |
| 698 | - public function prepare_line_items( $posted, $action = 'create', $item = null ) { | |
| 699 | - $item = parent::prepare_line_items( $posted, $action, $item ); | |
| 700 | - | |
| 701 | - /** | |
| 702 | - * If you send a variation with meta_data, the meta_data will be duplicated | |
| 703 | - * WooCommerce attempts to delete the duped meta_data in $item->set_product( $variation ) | |
| 704 | - * but later it gets added right back in $this->maybe_set_item_meta_data. | |
| 705 | - * | |
| 706 | - * To fix this we check for a variation_id and remove the meta_data before setting the product | |
| 707 | - */ | |
| 708 | - if ( 'create' !== $action && $item->get_variation_id() ) { | |
| 709 | - $attributes = wc_get_product_variation_attributes( $item->get_variation_id() ); | |
| 710 | - | |
| 711 | - // Loop through attributes and remove any duplicates. | |
| 712 | - foreach ( $attributes as $key => $value ) { | |
| 713 | - $attribute = str_replace( 'attribute_', '', $key ); | |
| 714 | - $meta_data = $item->get_meta( $attribute, false ); | |
| 715 | - | |
| 716 | - if ( \is_array( $meta_data ) && \count( $meta_data ) > 1 ) { | |
| 717 | - $meta_to_keep = null; | |
| 718 | - | |
| 719 | - // Check each meta to find one with an ID to keep. | |
| 720 | - foreach ( $meta_data as $meta ) { | |
| 721 | - if ( isset( $meta->id ) ) { | |
| 722 | - $meta_to_keep = $meta; | |
| 723 | - | |
| 724 | - break; | |
| 725 | - } | |
| 726 | - } | |
| 727 | - | |
| 728 | - // If no meta with an ID is found, keep the first one. | |
| 729 | - if ( ! $meta_to_keep ) { | |
| 730 | - $meta_to_keep = $meta_data[0]; | |
| 731 | - } | |
| 732 | - | |
| 733 | - // Remove all other meta data for this attribute. | |
| 734 | - foreach ( $meta_data as $meta ) { | |
| 735 | - if ( $meta !== $meta_to_keep ) { | |
| 736 | - if ( $meta->id ) { | |
| 737 | - $item->delete_meta_data_by_mid( $meta->id ); | |
| 738 | - } else { | |
| 739 | - $meta->value = null; | |
| 740 | - } | |
| 741 | - } | |
| 742 | - } | |
| 743 | - } | |
| 744 | - } | |
| 745 | - } | |
| 746 | - | |
| 747 | - return $item; | |
| 748 | - } | |
| 749 | - | |
| 750 | - /** | |
| 751 | - * Maybe set item meta if posted. | |
| 752 | - * | |
| 753 | - * @param WC_Order_Item $item Order item data. | |
| 754 | - * @param array $posted Request data. | |
| 755 | - */ | |
| 756 | - public function maybe_set_item_meta_data( $item, $posted ): void { | |
| 757 | - /* | |
| 758 | - * Call the parent method first to handle standard meta data | |
| 759 | - * This will populate the attribute key, eg: 'pa_color' or 'logo' | |
| 760 | - * BUT: if the attribute can be 'any' then we need to handle that | |
| 761 | - */ | |
| 762 | - parent::maybe_set_item_meta_data( $item, $posted ); | |
| 763 | - | |
| 764 | - // Ensure this is a product line item, not a fee or shipping. | |
| 765 | - if ( ! \is_object( $item ) || 'WC_Order_Item_Product' !== \get_class( $item ) ) { | |
| 766 | - return; | |
| 767 | - } | |
| 768 | - | |
| 769 | - // SKU meta is not stored by default, we will add it for 'miscellaneous' products. | |
| 770 | - if ( isset( $posted['sku'] ) && 0 === $item->get_product_id() ) { | |
| 771 | - $item->add_meta_data( '_sku', $posted['sku'], true ); | |
| 772 | - } | |
| 773 | - | |
| 774 | - // Only proceed if there's a variation ID and we have posted meta. | |
| 775 | - if ( ! $item->get_variation_id() || empty( $posted['meta_data'] ) || ! \is_array( $posted['meta_data'] ) ) { | |
| 776 | - return; | |
| 777 | - } | |
| 778 | - | |
| 779 | - $attributes = wc_get_product_variation_attributes( $item->get_variation_id() ); | |
| 780 | - $product_id = $item->get_product_id(); | |
| 781 | - $product = wc_get_product( $product_id ); | |
| 782 | - $parent_attributes = $product->get_attributes(); | |
| 783 | - | |
| 784 | - foreach ( $attributes as $key => $value ) { | |
| 785 | - if ( '' === $value ) { | |
| 786 | - $slug = str_replace( 'attribute_', '', $key ); | |
| 787 | - | |
| 788 | - if ( ! isset( $parent_attributes[ $slug ] ) ) { | |
| 789 | - continue; | |
| 790 | - } | |
| 791 | - | |
| 792 | - $name = $parent_attributes[ $slug ]['name'] ?? $slug; | |
| 793 | - if ( $name === $slug ) { | |
| 794 | - $name = wc_attribute_label( $slug ); | |
| 795 | - } | |
| 796 | - | |
| 797 | - // find the value from $posted['meta_data']. | |
| 798 | - foreach ( $posted['meta_data'] as $meta ) { | |
| 799 | - // Match posted attribute label to the $name we just determined. | |
| 800 | - if ( isset( $meta['display_key'], $meta['display_value'] ) && $meta['display_key'] === $name ) { | |
| 801 | - $posted_value = $meta['display_value']; | |
| 802 | - // Only update if the posted value is non-empty. | |
| 803 | - if ( $posted_value ) { | |
| 804 | - $item->update_meta_data( | |
| 805 | - $slug, | |
| 806 | - $posted_value, | |
| 807 | - $meta['id'] ?? '' | |
| 808 | - ); | |
| 809 | - | |
| 810 | - break; // Stop searching once found. | |
| 811 | - } | |
| 812 | - } | |
| 813 | - } | |
| 814 | - } | |
| 815 | - } | |
| 816 | - } | |
| 817 | - | |
| 818 | - /** | |
| 819 | 553 | * The way WooCommerce handles negative fees is ... weird. |
| 820 | 554 | * They by-pass the normal tax calculation, disregard the tax_status and tax_class, and apply the taxes to the fee line. |
| 821 | 555 | * This is a problem because if people want to apply a negative fee to an order, and set tax_status to 'none', it will give |
| 822 | 556 | * the wrong result. |
| @@ -832,31 +566,8 @@ | ||
| 832 | 566 | \WCPOS\WooCommercePOS\Orders::fee_after_calculate_taxes( $fee_item, $calculate_tax_for ); |
| 833 | 567 | } |
| 834 | 568 | |
| 835 | 569 | /** |
| 836 | - * Gets the product ID from posted ID. | |
| 837 | - * | |
| 838 | - * @param array $posted Request data. | |
| 839 | - * @param string $action 'create' to add line item or 'update' to update it. | |
| 840 | - * | |
| 841 | - * @throws WC_REST_Exception When SKU or ID is not valid. | |
| 842 | - * | |
| 843 | - * @return int | |
| 844 | - */ | |
| 845 | - public function get_product_id( $posted, $action = 'create' ) { | |
| 846 | - // If id = 0, ie: miscellaneaous product, just return 0. | |
| 847 | - if ( isset( $posted['product_id'] ) && 0 == $posted['product_id'] ) { | |
| 848 | - return 0; | |
| 849 | - } | |
| 850 | - | |
| 851 | - // Bypass the sku check. Some users have products with duplicated SKUs, esp. variable/variations. | |
| 852 | - $data = $posted; | |
| 853 | - unset( $data['sku'] ); | |
| 854 | - | |
| 855 | - return parent::get_product_id( $data, $action ); | |
| 856 | - } | |
| 857 | - | |
| 858 | - /** | |
| 859 | 570 | * Validate billing email. |
| 860 | 571 | * NOTE: we have removed the format check to allow empty email addresses. |
| 861 | 572 | * |
| 862 | 573 | * @param WP_REST_Request $request Full details about the request. |
| @@ -1019,8 +730,34 @@ | ||
| 1019 | 730 | ); |
| 1020 | 731 | } |
| 1021 | 732 | |
| 1022 | 733 | /** |
| 734 | + * Get the route schema for the send-email action. | |
| 735 | + * | |
| 736 | + * Registered as the route-level `schema` callback. WordPress invokes it with | |
| 737 | + * `call_user_func()` whenever a namespace index is requested with | |
| 738 | + * `context=help`, so it must be a real callable — an empty array there | |
| 739 | + * passes `isset()` and then fatals with a TypeError. | |
| 740 | + * | |
| 741 | + * @return array | |
| 742 | + */ | |
| 743 | + public function wcpos_get_public_send_email_schema() { | |
| 744 | + return array( | |
| 745 | + '$schema' => 'http://json-schema.org/draft-04/schema#', | |
| 746 | + 'title' => 'order_email', | |
| 747 | + 'type' => 'object', | |
| 748 | + 'properties' => array( | |
| 749 | + 'success' => array( | |
| 750 | + 'description' => __( 'Whether the order email was sent.', 'woocommerce-pos' ), | |
| 751 | + 'type' => 'boolean', | |
| 752 | + 'context' => array( 'view', 'edit' ), | |
| 753 | + 'readonly' => true, | |
| 754 | + ), | |
| 755 | + ), | |
| 756 | + ); | |
| 757 | + } | |
| 758 | + | |
| 759 | + /** | |
| 1023 | 760 | * Modify the order response. |
| 1024 | 761 | * |
| 1025 | 762 | * @param WP_REST_Response $response The response object. |
| 1026 | 763 | * @param WC_Abstract_Order $order Object data. |
| @@ -1150,9 +887,10 @@ | ||
| 1150 | 887 | * |
| 1151 | 888 | * @throws \WC_Data_Exception If order data is invalid. |
| 1152 | 889 | */ |
| 1153 | 890 | public function wcpos_before_order_object_save( WC_Abstract_Order $order ): void { |
| 1154 | - $is_creating_order = $order === $this->creating_order; | |
| 891 | + $intent = Order_Write_Intent::current(); | |
| 892 | + $is_creating_order = null !== $intent && $intent->is_create() && $intent->is_subject( $order ); | |
| 1155 | 893 | |
| 1156 | 894 | if ( $is_creating_order && method_exists( $order, 'set_created_via' ) ) { |
| 1157 | 895 | $order->set_created_via( PLUGIN_NAME ); |
| 1158 | 896 | // Record provenance only; receipt calculations continue to infer historical |