| @@ -8,9 +8,9 @@ | ||
| 8 | 8 | namespace WCPOS\WooCommercePOS\Integrations; |
| 9 | 9 | |
| 10 | 10 | use WC_Abstract_Order; |
| 11 | 11 | use WC_Order; |
| 12 | -use WCPOS\WooCommercePOS\Services\Order_Write_Intent; | |
| 12 | +use WP_REST_Request; | |
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | 15 | * Keep WooCommerce Tax from restoring stale tax lines onto open POS orders. |
| 16 | 16 | * |
| @@ -89,14 +89,26 @@ | ||
| 89 | 89 | */ |
| 90 | 90 | private $suspended = array(); |
| 91 | 91 | |
| 92 | 92 | /** |
| 93 | + * The order a WCPOS REST write is about to save, and the status it asked for. | |
| 94 | + * | |
| 95 | + * WooCommerce's REST controller recalculates totals before it applies the | |
| 96 | + * requested status, so a request that reopens a paid order and changes its | |
| 97 | + * lines recalculates while the persisted status is still paid. | |
| 98 | + * | |
| 99 | + * @var array{order: WC_Abstract_Order, status: string}|null | |
| 100 | + */ | |
| 101 | + private $requested = null; | |
| 102 | + | |
| 103 | + /** | |
| 93 | 104 | * Constructor. |
| 94 | 105 | * |
| 95 | - * Prime at 8, suspend at 9 before the plugin's snapshot at 10, then resume | |
| 106 | + * Suspend at priority 9, before the plugin's snapshot callback at 10; resume | |
| 96 | 107 | * from the last priority so nothing re-added runs in the same pass. |
| 97 | 108 | */ |
| 98 | 109 | public function __construct() { |
| 110 | + add_filter( 'woocommerce_rest_pre_insert_shop_order_object', array( $this, 'note_requested_status' ), 10, 2 ); | |
| 99 | 111 | add_action( self::BEFORE_HOOK, array( $this, 'prime_tax_rates' ), 8, 2 ); |
| 100 | 112 | add_action( self::BEFORE_HOOK, array( $this, 'suspend_tax_preservation' ), 9, 2 ); |
| 101 | 113 | add_action( self::AFTER_HOOK, array( $this, 'resume_tax_preservation' ), self::RESUME_PRIORITY ); |
| 102 | 114 | } |
| @@ -101,8 +113,28 @@ | ||
| 101 | 113 | add_action( self::AFTER_HOOK, array( $this, 'resume_tax_preservation' ), self::RESUME_PRIORITY ); |
| 102 | 114 | } |
| 103 | 115 | |
| 104 | 116 | /** |
| 117 | + * Remember the status a REST write asked for, for the recalculation it triggers. | |
| 118 | + * | |
| 119 | + * @param mixed $order The order about to be saved. | |
| 120 | + * @param WP_REST_Request|null $request The request. | |
| 121 | + * | |
| 122 | + * @return mixed The order, unchanged. | |
| 123 | + */ | |
| 124 | + public function note_requested_status( $order, $request = null ) { | |
| 125 | + $this->requested = null; | |
| 126 | + if ( $order instanceof WC_Abstract_Order && $request instanceof WP_REST_Request ) { | |
| 127 | + $this->requested = array( | |
| 128 | + 'order' => $order, | |
| 129 | + 'status' => (string) $request->get_param( 'status' ), | |
| 130 | + ); | |
| 131 | + } | |
| 132 | + | |
| 133 | + return $order; | |
| 134 | + } | |
| 135 | + | |
| 136 | + /** | |
| 105 | 137 | * Prime the plugin's rates for the order before WooCommerce matches them. |
| 106 | 138 | * |
| 107 | 139 | * Mirrors the request the plugin's protected get_backend_line_items() builds |
| 108 | 140 | * (woocommerce-services 3.6.14), with one deliberate difference: items |
| @@ -296,11 +328,8 @@ | ||
| 296 | 328 | |
| 297 | 329 | /** |
| 298 | 330 | * Whether the order is, or is being put back to, still being built up at the till. |
| 299 | 331 | * |
| 300 | - * WooCommerce recalculates totals before applying the requested status, so | |
| 301 | - * reopening a paid order recalculates while its persisted status is still paid. | |
| 302 | - * | |
| 303 | 332 | * @param WC_Abstract_Order $order The order. |
| 304 | 333 | * |
| 305 | 334 | * @return bool |
| 306 | 335 | */ |
| @@ -308,11 +337,11 @@ | ||
| 308 | 337 | if ( \in_array( $order->get_status(), self::OPEN_STATUSES, true ) ) { |
| 309 | 338 | return true; |
| 310 | 339 | } |
| 311 | 340 | |
| 312 | - $intent = Order_Write_Intent::current(); | |
| 313 | - return null !== $intent && $intent->is_subject( $order ) | |
| 314 | - && \in_array( $intent->requested_status(), self::OPEN_STATUSES, true ); | |
| 341 | + return null !== $this->requested | |
| 342 | + && $this->requested['order'] === $order | |
| 343 | + && \in_array( $this->requested['status'], self::OPEN_STATUSES, true ); | |
| 315 | 344 | } |
| 316 | 345 | |
| 317 | 346 | /** |
| 318 | 347 | * The street line that belongs to the address WooCommerce is taxing. |