PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.19
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.19
1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 All 163 releases
← All changes | includes/API/V1/Orders_Controller.php +139 -362 1.10.21.10.19 View file →
@@ -12,27 +12,29 @@
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 use WC_Email_Customer_Invoice;
22 +use WC_Order;
21 23 use WC_Order_Item;
22 24 use WC_Order_Item_Fee;
23 -use WC_Order_Item_Product;
24 25 use WC_REST_Orders_Controller;
25 26 use WC_Tax;
26 27 use WCPOS\WooCommercePOS\Logger;
28 +use WCPOS\WooCommercePOS\Services\Order_Write_Intent;
27 29 use WCPOS\WooCommercePOS\Services\Pos_Order_Audit;
28 30 use WCPOS\WooCommercePOS\Services\Settings as SettingsService;
31 +use WCPOS\WooCommercePOS\Services\Stock_Validator;
29 32 use WCPOS\WooCommercePOS\Services\Tax_Id_Reader;
30 -use WCPOS\WooCommercePOS\Services\Tax_Id_Types;
31 -use WCPOS\WooCommercePOS\Services\Tax_Id_Writer;
32 33 use WCPOS\WooCommercePOS\Sync\Collection_Rules;
33 34 use WCPOS\WooCommercePOS\Sync\Collection_Rules_Plan;
34 35 use WCPOS\WooCommercePOS\Sync\Order_Serializer;
36 +use WCPOS\WooCommercePOS\Sync\Order_Write_Payload;
35 37 use const WCPOS\WooCommercePOS\PLUGIN_NAME;
36 38 use const WCPOS\WooCommercePOS\VERSION;
37 39 use WP_Error;
38 40 use WP_REST_Request;
@@ -82,25 +84,26 @@
82 84 */
83 85 protected $wcpos_request;
84 86
85 87 /**
86 - * The order object being created by the current request.
88 + * Whether High Performance Orders is enabled.
87 89 *
88 - * @var WC_Abstract_Order|null
90 + * @var bool
89 91 */
90 - private $creating_order;
92 + private $hpos_enabled = false;
91 93
92 94 /**
93 - * Whether High Performance Orders is enabled.
95 + * Shared date validation and tax-ID persistence for the order write lanes.
94 96 *
95 - * @var bool
97 + * @var Order_Write_Payload
96 98 */
97 - private $hpos_enabled = false;
99 + private $order_payload;
98 100
99 101 /**
100 102 * Constructor.
101 103 */
102 104 public function __construct() {
105 + $this->order_payload = new Order_Write_Payload();
103 106 $this->hpos_enabled = class_exists( OrderUtil::class ) && OrderUtil::custom_orders_table_usage_is_enabled();
104 107
105 108 if ( method_exists( parent::class, '__construct' ) ) {
106 109 parent::__construct();
@@ -107,71 +110,58 @@
107 110 }
108 111 }
109 112
110 113 /**
111 - * Check if the current user can update an order.
114 + * Persist new checkout orders as pending until stock is reserved atomically.
112 115 *
113 - * Overrides the parent to fix HPOS compatibility. When HPOS is enabled with
114 - * sync disabled, get_post() returns a shop_order_placehold post type that has
115 - * map_meta_cap = false and no capability_type, causing WordPress to check the
116 - * generic 'edit_post' capability instead of 'edit_shop_order'. Non-admin roles
117 - * like cashier have 'edit_shop_orders' but not the generic 'edit_posts', so the
118 - * permission check fails.
119 - *
120 - * @param WP_REST_Request $request Full details about the request.
121 - *
122 - * @return bool|WP_Error
116 + * @param WP_REST_Request $request Full request details.
117 + * @param bool $creating Whether a new order is being created.
118 + * @return WC_Data|WP_Error
119 + * @throws \Throwable If checkout stock validation cannot be completed.
123 120 */
124 - public function update_item_permissions_check( $request ) {
125 - $result = parent::update_item_permissions_check( $request );
126 -
127 - if ( ! is_wp_error( $result ) ) {
128 - return $result;
121 + protected function save_object( $request, $creating = false ) {
122 + $validator = Stock_Validator::instance();
123 + if ( ! $creating || ! \wcpos_request() || ! SettingsService::instance()->prevent_overselling_enabled() || ! $validator->should_validate_create_request( $request ) ) {
124 + return parent::save_object( $request, $creating );
129 125 }
130 126
131 - // Parent check failed - try direct capability check for HPOS compatibility.
132 - $id = (int) $request['id'];
133 - $order = wc_get_order( $id );
127 + $target_status = $request->get_param( 'status' );
128 + $set_paid = $request->get_param( 'set_paid' );
134 129
135 - if ( ! $order ) {
136 - return $result;
137 - }
130 + try {
131 + return $validator->around_paid_create(
132 + array(
133 + 'status' => $target_status,
134 + 'set_paid' => rest_sanitize_boolean( $set_paid ),
135 + 'transaction_id' => $request->get_param( 'transaction_id' ),
136 + ),
137 + function ( array $neutralised ) use ( $request, $creating ) {
138 + $request->set_param( 'status', $neutralised['status'] );
139 + $request->set_param( 'set_paid', $neutralised['set_paid'] );
138 140
139 - if ( ! current_user_can( 'edit_shop_orders' ) ) {
140 - return $result;
141 + return parent::save_object( $request, $creating );
142 + }
143 + );
144 + } finally {
145 + $request->set_param( 'status', $target_status );
146 + $request->set_param( 'set_paid', $set_paid );
141 147 }
148 + }
142 149
143 - return true;
150 + /** Delegate the edit decision, preserving WooCommerce's request-dependent checks.
151 + *
152 + * @param \WP_REST_Request $request Full request details.
153 + */
154 + public function update_item_permissions_check( $request ) {
155 + return Permission_Rules::verdict( 'orders', 'edit', (int) $request['id'], 0, 'v1', $request->get_params() );
144 156 }
145 157
146 - /**
147 - * Check if the current user can delete an order.
158 + /** Delegate the delete decision, preserving WooCommerce's request-dependent checks.
148 159 *
149 - * Same HPOS fix as update_item_permissions_check.
150 - *
151 - * @param WP_REST_Request $request Full details about the request.
152 - *
153 - * @return bool|WP_Error
160 + * @param \WP_REST_Request $request Full request details.
154 161 */
155 162 public function delete_item_permissions_check( $request ) {
156 - $result = parent::delete_item_permissions_check( $request );
157 -
158 - if ( ! is_wp_error( $result ) ) {
159 - return $result;
160 - }
161 -
162 - $id = (int) $request['id'];
163 - $order = wc_get_order( $id );
164 -
165 - if ( ! $order ) {
166 - return $result;
167 - }
168 -
169 - if ( ! current_user_can( 'delete_shop_orders' ) ) {
170 - return $result;
171 - }
172 -
173 - return true;
163 + return Permission_Rules::verdict( 'orders', 'delete', (int) $request['id'], 0, 'v1', $request->get_params() );
174 164 }
175 165
176 166 /**
177 167 * Delete a single order.
@@ -297,9 +287,9 @@
297 287 ),
298 288 )
299 289 ),
300 290 ),
301 - 'schema' => array(),
291 + 'schema' => array( $this, 'wcpos_get_public_send_email_schema' ),
302 292 )
303 293 );
304 294
305 295 register_rest_route(
@@ -317,8 +307,10 @@
317 307 }
318 308
319 309 /**
320 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.
321 313 */
322 314 public function get_item_schema() {
323 315 $schema = parent::get_item_schema();
324 316
@@ -327,30 +319,9 @@
327 319 $schema['properties']['tax_ids'] = array(
328 320 'description' => __( 'Customer tax IDs snapshotted at sale time.', 'woocommerce-pos' ),
329 321 'type' => 'array',
330 322 'context' => array( 'view', 'edit' ),
331 - 'items' => array(
332 - 'type' => 'object',
333 - 'properties' => array(
334 - 'type' => array(
335 - 'type' => 'string',
336 - 'enum' => Tax_Id_Types::all_types(),
337 - 'description' => /* translators: REST API schema field label or error message. */ __( 'Tax ID type.', 'woocommerce-pos' ),
338 - ),
339 - 'value' => array(
340 - 'type' => 'string',
341 - 'description' => /* translators: REST API schema field label or error message. */ __( 'Tax ID value.', 'woocommerce-pos' ),
342 - ),
343 - 'country' => array(
344 - 'type' => array( 'string', 'null' ),
345 - 'description' => __( 'ISO 3166-1 alpha-2 country code.', 'woocommerce-pos' ),
346 - ),
347 - 'label' => array(
348 - 'type' => array( 'string', 'null' ),
349 - 'description' => /* translators: REST API schema field label or error message. */ __( 'Optional human-readable label.', 'woocommerce-pos' ),
350 - ),
351 - ),
352 - ),
323 + 'items' => array( 'type' => 'object' ),
353 324 );
354 325
355 326 // Check and remove email format validation from the billing property.
356 327 if ( isset( $schema['properties']['billing']['properties']['email']['format'] ) ) {
@@ -443,122 +414,50 @@
443 414 if ( isset( $request['meta_data'] ) && \is_array( $request['meta_data'] ) ) {
444 415 $request->set_param( 'meta_data', Pos_Order_Audit::sanitize_create_meta( $request['meta_data'] ) );
445 416 }
446 417
447 - $this->creating_order = null;
418 + $this->wcpos_shape_request_payload( $request, $this->order_payload->for_create( $request->get_params() ) );
448 419
449 - add_filter( 'woocommerce_rest_pre_insert_shop_order_object', array( $this, 'wcpos_track_creating_order' ), 9, 3 );
450 - 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 + );
451 436
452 - try {
453 - // Proceed with the parent method to handle the creation.
454 - $response = parent::create_item( $request );
455 - } finally {
456 - remove_filter( 'woocommerce_rest_pre_insert_shop_order_object', array( $this, 'wcpos_preserve_client_created_date_gmt' ), 10 );
457 - remove_filter( 'woocommerce_rest_pre_insert_shop_order_object', array( $this, 'wcpos_track_creating_order' ), 9 );
458 - $this->creating_order = null;
459 - }
437 + $this->wcpos_refresh_tax_ids_response( $response, $request, true );
460 438
461 - $this->wcpos_snapshot_tax_ids_to_order( $response, $request, true );
462 -
463 439 return $response;
464 440 }
465 441
466 442 /**
467 - * Record the exact order object prepared for this create request.
443 + * Preserve the client creation time using the shared payload validator.
468 444 *
469 - * @param WC_Data|WP_Error $order Order object prepared by WooCommerce.
445 + * @param WC_Data|WP_Error $order Prepared order.
470 446 * @param WP_REST_Request $request Request object.
471 - * @param bool $creating Whether a new order is being created.
472 - *
447 + * @param bool $creating Whether this is a create.
473 448 * @return WC_Data|WP_Error
474 449 */
475 - public function wcpos_track_creating_order( $order, WP_REST_Request $request, bool $creating ) {
476 - if ( $creating && $order instanceof WC_Abstract_Order ) {
477 - $this->creating_order = $order;
478 - }
479 -
480 - return $order;
481 - }
482 -
483 - /**
484 - * Preserve client-provided order creation time for offline-created orders.
485 - *
486 - * WooCommerce marks date_created/date_created_gmt as read-only in the REST
487 - * schema, so those fields are removed before the parent controller prepares
488 - * the order. WCPOS clients can create orders offline and later sync the full
489 - * local document; read the raw JSON payload here so the server keeps the
490 - * transaction time instead of the sync time.
491 - *
492 - * @param WC_Data|WP_Error $order Order object prepared by WooCommerce.
493 - * @param WP_REST_Request $request Request object.
494 - * @param bool $creating Whether a new order is being created.
495 - *
496 - * @return WC_Data|WP_Error
497 - */
498 450 public function wcpos_preserve_client_created_date_gmt( $order, WP_REST_Request $request, bool $creating ) {
499 451 if ( ! $creating || ! ( $order instanceof WC_Abstract_Order ) ) {
500 452 return $order;
501 453 }
502 - $this->creating_order = $order;
503 -
504 454 $body = $request->get_json_params();
505 -
506 - if ( ! isset( $body['date_created_gmt'] ) ) {
507 - 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;
508 458 }
509 -
510 - if ( ! is_scalar( $body['date_created_gmt'] ) ) {
511 - return new WP_Error(
512 - 'woocommerce_pos_rest_invalid_date_created_gmt',
513 - __( 'date_created_gmt must be a valid ISO 8601 UTC date.', 'woocommerce-pos' ),
514 - array( 'status' => 400 )
515 - );
516 - }
517 -
518 - $client_date_gmt = wc_clean( wp_unslash( (string) $body['date_created_gmt'] ) );
519 -
520 - if ( '' === $client_date_gmt ) {
521 - return $order;
522 - }
523 -
524 - if ( 1 !== preg_match( '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z?$/i', $client_date_gmt ) ) {
525 - return new WP_Error(
526 - 'woocommerce_pos_rest_invalid_date_created_gmt',
527 - __( 'date_created_gmt must be a valid ISO 8601 UTC date.', 'woocommerce-pos' ),
528 - array( 'status' => 400 )
529 - );
530 - }
531 -
532 - // WooCommerce serializes *_gmt fields without a timezone suffix; treat bare values as UTC.
533 - $parse_date_gmt = 'Z' === strtoupper( substr( $client_date_gmt, -1 ) )
534 - ? $client_date_gmt
535 - : $client_date_gmt . 'Z';
536 - $timestamp = rest_parse_date(
537 - $parse_date_gmt,
538 - true
539 - );
540 -
541 - if ( false === $timestamp ) {
542 - return new WP_Error(
543 - 'woocommerce_pos_rest_invalid_date_created_gmt',
544 - __( 'date_created_gmt must be a valid ISO 8601 UTC date.', 'woocommerce-pos' ),
545 - array( 'status' => 400 )
546 - );
547 - }
548 -
549 - $maximum_future_timestamp = time() + DAY_IN_SECONDS;
550 -
551 - if ( $timestamp > $maximum_future_timestamp ) {
552 - return new WP_Error(
553 - 'woocommerce_pos_rest_future_date_created_gmt',
554 - __( 'date_created_gmt cannot be more than 24 hours in the future.', 'woocommerce-pos' ),
555 - array( 'status' => 400 )
556 - );
557 - }
558 -
559 459 $order->set_date_created( $timestamp );
560 -
561 460 return $order;
562 461 }
563 462
564 463 /**
@@ -592,30 +491,49 @@
592 491 )
593 492 );
594 493 }
595 494
596 - // Proceed with the parent method to handle the update.
597 - $response = parent::update_item( $request );
598 - $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() ) );
599 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 +
600 510 return $response;
601 511 }
602 512
603 513 /**
604 - * Persist tax_ids onto the order.
514 + * Replace the request fields touched by the shared payload shaper.
605 515 *
606 - * On create: if the request did not provide `tax_ids`, snapshot from the
607 - * resolved customer record so the order is self-contained. If the request
608 - * 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.
609 530 *
610 - * On update: only write what the request explicitly provided; never
611 - * re-snapshot, since editing a customer must not mutate historical orders.
612 - *
613 531 * @param mixed $response Response from parent controller.
614 532 * @param WP_REST_Request $request Original request.
615 533 * @param bool $is_create True for create, false for update.
616 534 */
617 - 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 {
618 536 if ( ! ( $response instanceof WP_REST_Response ) ) {
619 537 return;
620 538 }
621 539
@@ -623,161 +541,16 @@
623 541 $order_id = isset( $data['id'] ) ? (int) $data['id'] : 0;
624 542 if ( $order_id <= 0 ) {
625 543 return;
626 544 }
627 - $order = \wc_get_order( $order_id );
628 - if ( ! $order ) {
629 - 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 );
630 549 }
631 -
632 - $tax_ids = $request->get_param( 'tax_ids' );
633 - $writer = new Tax_Id_Writer();
634 -
635 - if ( \is_array( $tax_ids ) ) {
636 - $writer->write_for_order( $order, $tax_ids );
637 - } elseif ( $is_create ) {
638 - $customer_id = (int) $order->get_customer_id();
639 - if ( $customer_id > 0 ) {
640 - $writer->snapshot_from_user_to_order( $order, $customer_id );
641 - }
642 - }
643 -
644 - $data['tax_ids'] = ( new Tax_Id_Reader() )->read_for_order( $order );
645 - $response->set_data( $data );
646 550 }
647 551
648 552 /**
649 - * Create or update a line item.
650 - *
651 - * @param array $posted Line item data.
652 - * @param string $action 'create' to add line item or 'update' to update it.
653 - * @param object $item Passed when updating an item. Null during creation.
654 - *
655 - * @throws \WC_REST_Exception Invalid data, server error.
656 - *
657 - * @return WC_Order_Item_Product
658 - */
659 - public function prepare_line_items( $posted, $action = 'create', $item = null ) {
660 - $item = parent::prepare_line_items( $posted, $action, $item );
661 -
662 - /**
663 - * If you send a variation with meta_data, the meta_data will be duplicated
664 - * WooCommerce attempts to delete the duped meta_data in $item->set_product( $variation )
665 - * but later it gets added right back in $this->maybe_set_item_meta_data.
666 - *
667 - * To fix this we check for a variation_id and remove the meta_data before setting the product
668 - */
669 - if ( 'create' !== $action && $item->get_variation_id() ) {
670 - $attributes = wc_get_product_variation_attributes( $item->get_variation_id() );
671 -
672 - // Loop through attributes and remove any duplicates.
673 - foreach ( $attributes as $key => $value ) {
674 - $attribute = str_replace( 'attribute_', '', $key );
675 - $meta_data = $item->get_meta( $attribute, false );
676 -
677 - if ( \is_array( $meta_data ) && \count( $meta_data ) > 1 ) {
678 - $meta_to_keep = null;
679 -
680 - // Check each meta to find one with an ID to keep.
681 - foreach ( $meta_data as $meta ) {
682 - if ( isset( $meta->id ) ) {
683 - $meta_to_keep = $meta;
684 -
685 - break;
686 - }
687 - }
688 -
689 - // If no meta with an ID is found, keep the first one.
690 - if ( ! $meta_to_keep ) {
691 - $meta_to_keep = $meta_data[0];
692 - }
693 -
694 - // Remove all other meta data for this attribute.
695 - foreach ( $meta_data as $meta ) {
696 - if ( $meta !== $meta_to_keep ) {
697 - if ( $meta->id ) {
698 - $item->delete_meta_data_by_mid( $meta->id );
699 - } else {
700 - $meta->value = null;
701 - }
702 - }
703 - }
704 - }
705 - }
706 - }
707 -
708 - return $item;
709 - }
710 -
711 - /**
712 - * Maybe set item meta if posted.
713 - *
714 - * @param WC_Order_Item $item Order item data.
715 - * @param array $posted Request data.
716 - */
717 - public function maybe_set_item_meta_data( $item, $posted ): void {
718 - /*
719 - * Call the parent method first to handle standard meta data
720 - * This will populate the attribute key, eg: 'pa_color' or 'logo'
721 - * BUT: if the attribute can be 'any' then we need to handle that
722 - */
723 - parent::maybe_set_item_meta_data( $item, $posted );
724 -
725 - // Ensure this is a product line item, not a fee or shipping.
726 - if ( ! \is_object( $item ) || 'WC_Order_Item_Product' !== \get_class( $item ) ) {
727 - return;
728 - }
729 -
730 - // SKU meta is not stored by default, we will add it for 'miscellaneous' products.
731 - if ( isset( $posted['sku'] ) && 0 === $item->get_product_id() ) {
732 - $item->add_meta_data( '_sku', $posted['sku'], true );
733 - }
734 -
735 - // Only proceed if there's a variation ID and we have posted meta.
736 - if ( ! $item->get_variation_id() || empty( $posted['meta_data'] ) || ! \is_array( $posted['meta_data'] ) ) {
737 - return;
738 - }
739 -
740 - $attributes = wc_get_product_variation_attributes( $item->get_variation_id() );
741 - $product_id = $item->get_product_id();
742 - $product = wc_get_product( $product_id );
743 - $parent_attributes = $product->get_attributes();
744 -
745 - foreach ( $attributes as $key => $value ) {
746 - if ( '' === $value ) {
747 - $slug = str_replace( 'attribute_', '', $key );
748 -
749 - if ( ! isset( $parent_attributes[ $slug ] ) ) {
750 - continue;
751 - }
752 -
753 - $name = $parent_attributes[ $slug ]['name'] ?? $slug;
754 - if ( $name === $slug ) {
755 - $name = wc_attribute_label( $slug );
756 - }
757 -
758 - // find the value from $posted['meta_data'].
759 - foreach ( $posted['meta_data'] as $meta ) {
760 - // Match posted attribute label to the $name we just determined.
761 - if ( isset( $meta['display_key'], $meta['display_value'] ) && $meta['display_key'] === $name ) {
762 - $posted_value = $meta['display_value'];
763 - // Only update if the posted value is non-empty.
764 - if ( $posted_value ) {
765 - $item->update_meta_data(
766 - $slug,
767 - $posted_value,
768 - $meta['id'] ?? ''
769 - );
770 -
771 - break; // Stop searching once found.
772 - }
773 - }
774 - }
775 - }
776 - }
777 - }
778 -
779 - /**
780 553 * The way WooCommerce handles negative fees is ... weird.
781 554 * They by-pass the normal tax calculation, disregard the tax_status and tax_class, and apply the taxes to the fee line.
782 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
783 556 * the wrong result.
@@ -793,31 +566,8 @@
793 566 \WCPOS\WooCommercePOS\Orders::fee_after_calculate_taxes( $fee_item, $calculate_tax_for );
794 567 }
795 568
796 569 /**
797 - * Gets the product ID from posted ID.
798 - *
799 - * @param array $posted Request data.
800 - * @param string $action 'create' to add line item or 'update' to update it.
801 - *
802 - * @throws WC_REST_Exception When SKU or ID is not valid.
803 - *
804 - * @return int
805 - */
806 - public function get_product_id( $posted, $action = 'create' ) {
807 - // If id = 0, ie: miscellaneaous product, just return 0.
808 - if ( isset( $posted['product_id'] ) && 0 == $posted['product_id'] ) {
809 - return 0;
810 - }
811 -
812 - // Bypass the sku check. Some users have products with duplicated SKUs, esp. variable/variations.
813 - $data = $posted;
814 - unset( $data['sku'] );
815 -
816 - return parent::get_product_id( $data, $action );
817 - }
818 -
819 - /**
820 570 * Validate billing email.
821 571 * NOTE: we have removed the format check to allow empty email addresses.
822 572 *
823 573 * @param WP_REST_Request $request Full details about the request.
@@ -980,8 +730,34 @@
980 730 );
981 731 }
982 732
983 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 + /**
984 760 * Modify the order response.
985 761 *
986 762 * @param WP_REST_Response $response The response object.
987 763 * @param WC_Abstract_Order $order Object data.
@@ -1111,9 +887,10 @@
1111 887 *
1112 888 * @throws \WC_Data_Exception If order data is invalid.
1113 889 */
1114 890 public function wcpos_before_order_object_save( WC_Abstract_Order $order ): void {
1115 - $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 );
1116 893
1117 894 if ( $is_creating_order && method_exists( $order, 'set_created_via' ) ) {
1118 895 $order->set_created_via( PLUGIN_NAME );
1119 896 // Record provenance only; receipt calculations continue to infer historical