PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.63
Timetics – Appointment Booking Calendar & Scheduling v1.0.63
1.0.62 1.0.63 1.0.61 1.0.60 1.0.59 1.0.58 1.0.57 1.0.56 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 All 64 releases
timetics / core / integrations / woocommerce / status-mapper.php

status-mapper.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.63, at core/integrations/woocommerce/status-mapper.php

200 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Timetics\Core\Integrations\Woocommerce;
3
4 defined( 'ABSPATH' ) || exit;
5
6 /**
7 * Class Status_Mapper
8 *
9 * Maps booking statuses to WooCommerce order statuses and vice versa
10 *
11 */
12 class Status_Mapper {
13
14 /**
15 * Track currently syncing booking-order pairs in this request
16 *
17 * @var array
18 */
19 private static $syncing_pairs = [];
20
21 /**
22 * Map Timetics booking status to WooCommerce order status
23 *
24 * Mapping: ( Timetics -> Woocommerce )
25 *
26 * - pending -> pending
27 * - approved -> processing
28 * - completed -> completed
29 * - cancel -> cancelled
30 * - failed -> failed
31 *
32 * @param string $booking_status The Timetics booking status
33 * @return string The WooCommerce order status (without 'wc-' prefix)
34 */
35 public static function booking_to_order_status( $booking_status ) {
36 $mapping = [
37 'pending' => 'pending',
38 'approved' => 'processing',
39 'completed' => 'completed',
40 'cancel' => 'cancelled',
41 'failed' => 'failed',
42 ];
43
44 return isset( $mapping[ $booking_status ] ) ? $mapping[ $booking_status ] : $booking_status;
45 }
46
47 /**
48 * Map WooCommerce order status to Timetics booking status
49 *
50 * Mapping:
51 * - pending -> pending
52 * - processing -> approved
53 * - on-hold -> pending
54 * - completed -> completed
55 * - cancelled -> cancel
56 * - refunded -> cancel
57 * - failed -> failed
58 *
59 * @param string $order_status The WooCommerce order status (may or may not include 'wc-' prefix)
60 * @return string The Timetics booking status
61 */
62 public static function order_to_booking_status( $order_status ) {
63 // Remove 'wc-' prefix if present
64 $order_status = str_replace( 'wc-', '', $order_status );
65
66 $mapping = [
67 'pending' => 'pending',
68 'processing' => 'approved',
69 'on-hold' => 'pending',
70 'completed' => 'completed',
71 'cancelled' => 'cancel',
72 'refunded' => 'cancel',
73 'failed' => 'failed',
74 ];
75
76 return isset( $mapping[ $order_status ] ) ? $mapping[ $order_status ] : $order_status;
77 }
78
79 /**
80 * Check if a status transition should trigger sync
81 *
82 * @param string $from_status The previous status
83 * @param string $to_status The new status
84 *
85 * @return bool Whether the transition should be synced
86 */
87 public static function should_sync_status( $from_status, $to_status ) {
88 if ( $from_status === $to_status ) {
89 return false;
90 }
91
92 return true;
93 }
94
95 /**
96 * Get the WooCommerce order ID from booking meta
97 *
98 * @param int $booking_id The booking ID
99 *
100 * @return int|false The WooCommerce order ID or false if not found
101 */
102 public static function get_order_id_from_booking( $booking_id ) {
103 return get_post_meta( $booking_id, '_tt_wc_order_id', true );
104 }
105
106 /**
107 * Store the WooCommerce order ID in booking meta
108 *
109 * @param int $booking_id The booking ID
110 * @param int $order_id The WooCommerce order ID
111 *
112 * @return bool Whether the meta was updated
113 */
114 public static function set_order_id_for_booking( $booking_id, $order_id ) {
115 return update_post_meta( $booking_id, '_tt_wc_order_id', $order_id );
116 }
117
118 /**
119 * Get the booking ID from WooCommerce order meta
120 *
121 * Reads through the order object so it works with HPOS ( orders stored in their
122 * own tables ), falling back to post meta for orders written before that.
123 *
124 * @param int $order_id The WooCommerce order ID
125 * @return int|false The booking ID or false if not found
126 */
127 public static function get_booking_id_from_order( $order_id ) {
128 $order = function_exists( 'wc_get_order' ) ? wc_get_order( $order_id ) : false;
129
130 if ( ! $order ) {
131 return get_post_meta( $order_id, '_tt_booking_id', true );
132 }
133
134 $booking_id = $order->get_meta( '_tt_booking_id' );
135
136 return $booking_id ? $booking_id : get_post_meta( $order_id, '_tt_booking_id', true );
137 }
138
139 /**
140 * Store the booking ID in WooCommerce order meta
141 *
142 * Writes through the order object. With HPOS enabled and post syncing off,
143 * update_post_meta() would leave the value in a wp_postmeta row that WooCommerce
144 * itself cannot see.
145 *
146 * @param int $order_id The WooCommerce order ID
147 * @param int $booking_id The booking ID
148 * @return bool Whether the meta was updated
149 */
150 public static function set_booking_id_for_order( $order_id, $booking_id ) {
151 $order = function_exists( 'wc_get_order' ) ? wc_get_order( $order_id ) : false;
152
153 if ( ! $order ) {
154 return update_post_meta( $order_id, '_tt_booking_id', $booking_id );
155 }
156
157 $order->update_meta_data( '_tt_booking_id', $booking_id );
158 $order->save();
159
160 return true;
161 }
162
163 /**
164 * Check if a booking-order pair is currently being synced
165 *
166 * @param int $booking_id The booking ID
167 * @param int $order_id The WooCommerce order ID
168 *
169 * @return bool Whether this pair is currently syncing
170 */
171 public static function is_syncing_pair( $booking_id, $order_id ) {
172 $pair_key = "{$booking_id}_{$order_id}";
173 return isset( self::$syncing_pairs[ $pair_key ] );
174 }
175
176 /**
177 * Mark a booking-order pair as currently syncing
178 *
179 * @param int $booking_id The booking ID
180 * @param int $order_id The WooCommerce order ID
181 * @return void
182 */
183 public static function begin_sync_pair( $booking_id, $order_id ) {
184 $pair_key = "{$booking_id}_{$order_id}";
185 self::$syncing_pairs[ $pair_key ] = true;
186 }
187
188 /**
189 * Mark a booking-order pair as finished syncing
190 *
191 * @param int $booking_id The booking ID
192 * @param int $order_id The WooCommerce order ID
193 * @return void
194 */
195 public static function end_sync_pair( $booking_id, $order_id ) {
196 $pair_key = "{$booking_id}_{$order_id}";
197 unset( self::$syncing_pairs[ $pair_key ] );
198 }
199 }
200