| @@ -12,9 +12,8 @@ | ||
| 12 | 12 | if ( ! class_exists( 'WC_REST_Orders_Controller' ) ) { |
| 13 | 13 | return; |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | -use WCPOS\WooCommercePOS\Services\Permission_Rules; | |
| 17 | 16 | use Automattic\WooCommerce\Utilities\OrderUtil; |
| 18 | 17 | use Exception; |
| 19 | 18 | use WC_Abstract_Order; |
| 20 | 19 | use WC_Data; |
| @@ -21,20 +20,21 @@ | ||
| 21 | 20 | use WC_Email_Customer_Invoice; |
| 22 | 21 | use WC_Order; |
| 23 | 22 | use WC_Order_Item; |
| 24 | 23 | 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; | |
| 29 | 28 | use WCPOS\WooCommercePOS\Services\Pos_Order_Audit; |
| 30 | 29 | use WCPOS\WooCommercePOS\Services\Settings as SettingsService; |
| 31 | 30 | use WCPOS\WooCommercePOS\Services\Stock_Validator; |
| 32 | 31 | use WCPOS\WooCommercePOS\Services\Tax_Id_Reader; |
| 32 | +use WCPOS\WooCommercePOS\Services\Tax_Id_Types; | |
| 33 | +use WCPOS\WooCommercePOS\Services\Tax_Id_Writer; | |
| 33 | 34 | use WCPOS\WooCommercePOS\Sync\Collection_Rules; |
| 34 | 35 | use WCPOS\WooCommercePOS\Sync\Collection_Rules_Plan; |
| 35 | 36 | 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,26 +84,25 @@ | ||
| 84 | 84 | */ |
| 85 | 85 | protected $wcpos_request; |
| 86 | 86 | |
| 87 | 87 | /** |
| 88 | - * Whether High Performance Orders is enabled. | |
| 88 | + * The order object being created by the current request. | |
| 89 | 89 | * |
| 90 | - * @var bool | |
| 90 | + * @var WC_Abstract_Order|null | |
| 91 | 91 | */ |
| 92 | - private $hpos_enabled = false; | |
| 92 | + private $creating_order; | |
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | - * Shared date validation and tax-ID persistence for the order write lanes. | |
| 95 | + * Whether High Performance Orders is enabled. | |
| 96 | 96 | * |
| 97 | - * @var Order_Write_Payload | |
| 97 | + * @var bool | |
| 98 | 98 | */ |
| 99 | - private $order_payload; | |
| 99 | + private $hpos_enabled = false; | |
| 100 | 100 | |
| 101 | 101 | /** |
| 102 | 102 | * Constructor. |
| 103 | 103 | */ |
| 104 | 104 | public function __construct() { |
| 105 | - $this->order_payload = new Order_Write_Payload(); | |
| 106 | 105 | $this->hpos_enabled = class_exists( OrderUtil::class ) && OrderUtil::custom_orders_table_usage_is_enabled(); |
| 107 | 106 | |
| 108 | 107 | if ( method_exists( parent::class, '__construct' ) ) { |
| 109 | 108 | parent::__construct(); |
| @@ -146,22 +145,72 @@ | ||
| 146 | 145 | $request->set_param( 'set_paid', $set_paid ); |
| 147 | 146 | } |
| 148 | 147 | } |
| 149 | 148 | |
| 150 | - /** Delegate the edit decision, preserving WooCommerce's request-dependent checks. | |
| 149 | + /** | |
| 150 | + * Check if the current user can update an order. | |
| 151 | 151 | * |
| 152 | - * @param \WP_REST_Request $request Full request details. | |
| 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 | |
| 153 | 162 | */ |
| 154 | 163 | public function update_item_permissions_check( $request ) { |
| 155 | - return Permission_Rules::verdict( 'orders', 'edit', (int) $request['id'], 0, 'v1', $request->get_params() ); | |
| 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; | |
| 156 | 183 | } |
| 157 | 184 | |
| 158 | - /** Delegate the delete decision, preserving WooCommerce's request-dependent checks. | |
| 185 | + /** | |
| 186 | + * Check if the current user can delete an order. | |
| 159 | 187 | * |
| 160 | - * @param \WP_REST_Request $request Full request details. | |
| 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 | |
| 161 | 193 | */ |
| 162 | 194 | public function delete_item_permissions_check( $request ) { |
| 163 | - return Permission_Rules::verdict( 'orders', 'delete', (int) $request['id'], 0, 'v1', $request->get_params() ); | |
| 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; | |
| 164 | 213 | } |
| 165 | 214 | |
| 166 | 215 | /** |
| 167 | 216 | * Delete a single order. |
| @@ -307,10 +356,8 @@ | ||
| 307 | 356 | } |
| 308 | 357 | |
| 309 | 358 | /** |
| 310 | 359 | * 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. | |
| 313 | 360 | */ |
| 314 | 361 | public function get_item_schema() { |
| 315 | 362 | $schema = parent::get_item_schema(); |
| 316 | 363 | |
| @@ -319,9 +366,30 @@ | ||
| 319 | 366 | $schema['properties']['tax_ids'] = array( |
| 320 | 367 | 'description' => __( 'Customer tax IDs snapshotted at sale time.', 'woocommerce-pos' ), |
| 321 | 368 | 'type' => 'array', |
| 322 | 369 | 'context' => array( 'view', 'edit' ), |
| 323 | - 'items' => array( 'type' => 'object' ), | |
| 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 | + ), | |
| 324 | 392 | ); |
| 325 | 393 | |
| 326 | 394 | // Check and remove email format validation from the billing property. |
| 327 | 395 | if ( isset( $schema['properties']['billing']['properties']['email']['format'] ) ) { |
| @@ -414,50 +482,122 @@ | ||
| 414 | 482 | if ( isset( $request['meta_data'] ) && \is_array( $request['meta_data'] ) ) { |
| 415 | 483 | $request->set_param( 'meta_data', Pos_Order_Audit::sanitize_create_meta( $request['meta_data'] ) ); |
| 416 | 484 | } |
| 417 | 485 | |
| 418 | - $this->wcpos_shape_request_payload( $request, $this->order_payload->for_create( $request->get_params() ) ); | |
| 486 | + $this->creating_order = null; | |
| 419 | 487 | |
| 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 | - ); | |
| 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 ); | |
| 436 | 490 | |
| 437 | - $this->wcpos_refresh_tax_ids_response( $response, $request, true ); | |
| 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 | + } | |
| 438 | 499 | |
| 500 | + $this->wcpos_snapshot_tax_ids_to_order( $response, $request, true ); | |
| 501 | + | |
| 439 | 502 | return $response; |
| 440 | 503 | } |
| 441 | 504 | |
| 442 | 505 | /** |
| 443 | - * Preserve the client creation time using the shared payload validator. | |
| 506 | + * Record the exact order object prepared for this create request. | |
| 444 | 507 | * |
| 445 | - * @param WC_Data|WP_Error $order Prepared order. | |
| 508 | + * @param WC_Data|WP_Error $order Order object prepared by WooCommerce. | |
| 446 | 509 | * @param WP_REST_Request $request Request object. |
| 447 | - * @param bool $creating Whether this is a create. | |
| 510 | + * @param bool $creating Whether a new order is being created. | |
| 511 | + * | |
| 448 | 512 | * @return WC_Data|WP_Error |
| 449 | 513 | */ |
| 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 | + */ | |
| 450 | 537 | public function wcpos_preserve_client_created_date_gmt( $order, WP_REST_Request $request, bool $creating ) { |
| 451 | 538 | if ( ! $creating || ! ( $order instanceof WC_Abstract_Order ) ) { |
| 452 | 539 | return $order; |
| 453 | 540 | } |
| 541 | + $this->creating_order = $order; | |
| 542 | + | |
| 454 | 543 | $body = $request->get_json_params(); |
| 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; | |
| 544 | + | |
| 545 | + if ( ! isset( $body['date_created_gmt'] ) ) { | |
| 546 | + return $order; | |
| 458 | 547 | } |
| 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 | + | |
| 459 | 598 | $order->set_date_created( $timestamp ); |
| 599 | + | |
| 460 | 600 | return $order; |
| 461 | 601 | } |
| 462 | 602 | |
| 463 | 603 | /** |
| @@ -491,62 +631,188 @@ | ||
| 491 | 631 | ) |
| 492 | 632 | ); |
| 493 | 633 | } |
| 494 | 634 | |
| 495 | - $this->wcpos_shape_request_payload( $request, $this->order_payload->for_partial_update( (int) $request['id'], $request->get_params() ) ); | |
| 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 ); | |
| 496 | 638 | |
| 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 ); | |
| 639 | + return $response; | |
| 640 | + } | |
| 641 | + | |
| 642 | + /** | |
| 643 | + * Persist tax_ids onto the order. | |
| 644 | + * | |
| 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). | |
| 648 | + * | |
| 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 | + * @param mixed $response Response from parent controller. | |
| 653 | + * @param WP_REST_Request $request Original request. | |
| 654 | + * @param bool $is_create True for create, false for update. | |
| 655 | + */ | |
| 656 | + protected function wcpos_snapshot_tax_ids_to_order( $response, WP_REST_Request $request, bool $is_create ): void { | |
| 657 | + if ( ! ( $response instanceof WP_REST_Response ) ) { | |
| 658 | + return; | |
| 659 | + } | |
| 660 | + | |
| 661 | + $data = $response->get_data(); | |
| 662 | + $order_id = isset( $data['id'] ) ? (int) $data['id'] : 0; | |
| 663 | + if ( $order_id <= 0 ) { | |
| 664 | + return; | |
| 665 | + } | |
| 666 | + $order = \wc_get_order( $order_id ); | |
| 667 | + if ( ! $order ) { | |
| 668 | + return; | |
| 669 | + } | |
| 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 ); | |
| 506 | 680 | } |
| 507 | - ); | |
| 508 | - $this->wcpos_refresh_tax_ids_response( $response, $request, false ); | |
| 681 | + } | |
| 509 | 682 | |
| 510 | - return $response; | |
| 683 | + $data['tax_ids'] = ( new Tax_Id_Reader() )->read_for_order( $order ); | |
| 684 | + $response->set_data( $data ); | |
| 511 | 685 | } |
| 512 | 686 | |
| 513 | 687 | /** |
| 514 | - * Replace the request fields touched by the shared payload shaper. | |
| 688 | + * Create or update a line item. | |
| 515 | 689 | * |
| 516 | - * @param WP_REST_Request $request Validated request. | |
| 517 | - * @param array $shaped Shaped create or partial-update payload. | |
| 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 | |
| 518 | 697 | */ |
| 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 ] ); | |
| 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 | + } | |
| 524 | 744 | } |
| 525 | 745 | } |
| 746 | + | |
| 747 | + return $item; | |
| 526 | 748 | } |
| 527 | 749 | |
| 528 | 750 | /** |
| 529 | - * Adapt the parent response to the shared snapshot and refresh its tax_ids. | |
| 751 | + * Maybe set item meta if posted. | |
| 530 | 752 | * |
| 531 | - * @param mixed $response Response from parent controller. | |
| 532 | - * @param WP_REST_Request $request Original request. | |
| 533 | - * @param bool $is_create True for create, false for update. | |
| 753 | + * @param WC_Order_Item $item Order item data. | |
| 754 | + * @param array $posted Request data. | |
| 534 | 755 | */ |
| 535 | - private function wcpos_refresh_tax_ids_response( $response, WP_REST_Request $request, bool $is_create ): void { | |
| 536 | - if ( ! ( $response instanceof WP_REST_Response ) ) { | |
| 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 ) ) { | |
| 537 | 766 | return; |
| 538 | 767 | } |
| 539 | 768 | |
| 540 | - $data = $response->get_data(); | |
| 541 | - $order_id = isset( $data['id'] ) ? (int) $data['id'] : 0; | |
| 542 | - if ( $order_id <= 0 ) { | |
| 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'] ) ) { | |
| 543 | 776 | return; |
| 544 | 777 | } |
| 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 ); | |
| 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 | + } | |
| 549 | 815 | } |
| 550 | 816 | } |
| 551 | 817 | |
| 552 | 818 | /** |
| @@ -566,8 +832,31 @@ | ||
| 566 | 832 | \WCPOS\WooCommercePOS\Orders::fee_after_calculate_taxes( $fee_item, $calculate_tax_for ); |
| 567 | 833 | } |
| 568 | 834 | |
| 569 | 835 | /** |
| 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 | + /** | |
| 570 | 859 | * Validate billing email. |
| 571 | 860 | * NOTE: we have removed the format check to allow empty email addresses. |
| 572 | 861 | * |
| 573 | 862 | * @param WP_REST_Request $request Full details about the request. |
| @@ -887,10 +1176,9 @@ | ||
| 887 | 1176 | * |
| 888 | 1177 | * @throws \WC_Data_Exception If order data is invalid. |
| 889 | 1178 | */ |
| 890 | 1179 | public function wcpos_before_order_object_save( WC_Abstract_Order $order ): void { |
| 891 | - $intent = Order_Write_Intent::current(); | |
| 892 | - $is_creating_order = null !== $intent && $intent->is_create() && $intent->is_subject( $order ); | |
| 1180 | + $is_creating_order = $order === $this->creating_order; | |
| 893 | 1181 | |
| 894 | 1182 | if ( $is_creating_order && method_exists( $order, 'set_created_via' ) ) { |
| 895 | 1183 | $order->set_created_via( PLUGIN_NAME ); |
| 896 | 1184 | // Record provenance only; receipt calculations continue to infer historical |