PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.2
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.2
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
woocommerce-pos / includes / Services / Customer_Meta_Parity.php

Customer_Meta_Parity.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.2, at includes/Services/Customer_Meta_Parity.php

96 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * POS customer response parity (v1 → v2).
4 *
5 * @package WCPOS\WooCommercePOS
6 */
7
8 namespace WCPOS\WooCommercePOS\Services;
9
10 use Exception;
11 use WC_Customer;
12 use WCPOS\WooCommercePOS\Logger;
13 use WP_REST_Response;
14 use WP_User;
15
16 /**
17 * Restores the v1 customer response contract on POS-marked wc/v3 customer
18 * serializations (#1309, ruling 2026-07-29: v1.9 parity).
19 *
20 * Stock wc/v3 withholds the entire `meta_data` key from non-administrators
21 * (`wc_current_user_has_role( 'administrator' )` in
22 * WC_REST_Customers_V2_Controller::get_formatted_item_data_core), and never
23 * serves a top-level `tax_ids` field. V1\Customers_Controller::wcpos_customer_response()
24 * re-added non-protected meta for cashiers and built `tax_ids` via
25 * Tax_Id_Reader; the v2 catalog proxy and push echo forward wc/v3's
26 * serialization untouched, so both fields silently vanished for POS clients.
27 *
28 * Hooking `woocommerce_rest_prepare_customer` gated on wcpos_request() covers
29 * every v2 surface at once — proxy list/targeted pulls AND the /push/customers
30 * write echo — while leaving non-POS wc/v3 traffic exactly stock. Priority 20
31 * runs after v1's own response filter on v1 dispatches; both compute the same
32 * result from the datastore, so double-processing is idempotent.
33 *
34 * The `_woocommerce_pos_uuid` entry is NOT this class's concern: it is
35 * protected meta, filtered out here just as it was in v1's re-add, and served
36 * by Proxy_Uuid_Stamper (proxy) / the write-path stamper instead.
37 */
38 class Customer_Meta_Parity {
39 /**
40 * Register the customer response filter.
41 */
42 public function __construct() {
43 add_filter( 'woocommerce_rest_prepare_customer', array( $this, 'add_pos_customer_fields' ), 20, 3 );
44 }
45
46 /**
47 * Mirror v1's customer response shape onto POS-marked serializations:
48 * non-protected meta_data for every POS user, plus top-level tax_ids.
49 *
50 * @param WP_REST_Response $response The response object.
51 * @param mixed $user_data User object used to create the response.
52 * @param mixed $request Request object.
53 *
54 * @return WP_REST_Response
55 */
56 public function add_pos_customer_fields( $response, $user_data, $request ) {
57 if ( ! \wcpos_request() || ! $response instanceof WP_REST_Response || ! $user_data instanceof WP_User ) {
58 return $response;
59 }
60
61 $data = $response->get_data();
62
63 try {
64 $customer = new WC_Customer( $user_data->ID );
65
66 $filtered_meta_data = array_filter(
67 $customer->get_meta_data(),
68 static function ( $meta ) {
69 return ! is_protected_meta( $meta->key, 'user' );
70 }
71 );
72
73 // Convert to the WC REST API expected format.
74 $data['meta_data'] = array_map(
75 static function ( $meta ) {
76 return array(
77 'id' => $meta->id,
78 'key' => $meta->key,
79 'value' => $meta->value,
80 );
81 },
82 array_values( $filtered_meta_data )
83 );
84 } catch ( Exception $e ) {
85 Logger::log( 'Error getting customer meta data: ' . $e->getMessage() );
86 }
87
88 // Structured tax_ids list (read fallback across legacy plugin meta keys).
89 $data['tax_ids'] = ( new Tax_Id_Reader() )->read_for_user( $user_data->ID );
90
91 $response->set_data( $data );
92
93 return $response;
94 }
95 }
96