PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.56
Timetics – Appointment Booking Calendar & Scheduling v1.0.56
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 1.0.23 1.0.24 All 62 releases
timetics / core / integrations / woocommerce / status-mapper.php

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

174 lines 5.2 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 /**
5 * Class Status_Mapper
6 *
7 * Maps booking statuses to WooCommerce order statuses and vice versa
8 *
9 */
10 class Status_Mapper {
11
12 /**
13 * Track currently syncing booking-order pairs in this request
14 *
15 * @var array
16 */
17 private static $syncing_pairs = [];
18
19 /**
20 * Map Timetics booking status to WooCommerce order status
21 *
22 * Mapping: ( Timetics -> Woocommerce )
23 *
24 * - pending -> pending
25 * - approved -> processing
26 * - completed -> completed
27 * - cancel -> cancelled
28 * - failed -> failed
29 *
30 * @param string $booking_status The Timetics booking status
31 * @return string The WooCommerce order status (without 'wc-' prefix)
32 */
33 public static function booking_to_order_status( $booking_status ) {
34 $mapping = [
35 'pending' => 'pending',
36 'approved' => 'processing',
37 'completed' => 'completed',
38 'cancel' => 'cancelled',
39 'failed' => 'failed',
40 ];
41
42 return isset( $mapping[ $booking_status ] ) ? $mapping[ $booking_status ] : $booking_status;
43 }
44
45 /**
46 * Map WooCommerce order status to Timetics booking status
47 *
48 * Mapping:
49 * - pending -> pending
50 * - processing -> approved
51 * - on-hold -> pending
52 * - completed -> completed
53 * - cancelled -> cancel
54 * - refunded -> cancel
55 * - failed -> failed
56 *
57 * @param string $order_status The WooCommerce order status (may or may not include 'wc-' prefix)
58 * @return string The Timetics booking status
59 */
60 public static function order_to_booking_status( $order_status ) {
61 // Remove 'wc-' prefix if present
62 $order_status = str_replace( 'wc-', '', $order_status );
63
64 $mapping = [
65 'pending' => 'pending',
66 'processing' => 'approved',
67 'on-hold' => 'pending',
68 'completed' => 'completed',
69 'cancelled' => 'cancel',
70 'refunded' => 'cancel',
71 'failed' => 'failed',
72 ];
73
74 return isset( $mapping[ $order_status ] ) ? $mapping[ $order_status ] : $order_status;
75 }
76
77 /**
78 * Check if a status transition should trigger sync
79 *
80 * @param string $from_status The previous status
81 * @param string $to_status The new status
82 *
83 * @return bool Whether the transition should be synced
84 */
85 public static function should_sync_status( $from_status, $to_status ) {
86 if ( $from_status === $to_status ) {
87 return false;
88 }
89
90 return true;
91 }
92
93 /**
94 * Get the WooCommerce order ID from booking meta
95 *
96 * @param int $booking_id The booking ID
97 *
98 * @return int|false The WooCommerce order ID or false if not found
99 */
100 public static function get_order_id_from_booking( $booking_id ) {
101 return get_post_meta( $booking_id, '_tt_wc_order_id', true );
102 }
103
104 /**
105 * Store the WooCommerce order ID in booking meta
106 *
107 * @param int $booking_id The booking ID
108 * @param int $order_id The WooCommerce order ID
109 *
110 * @return bool Whether the meta was updated
111 */
112 public static function set_order_id_for_booking( $booking_id, $order_id ) {
113 return update_post_meta( $booking_id, '_tt_wc_order_id', $order_id );
114 }
115
116 /**
117 * Get the booking ID from WooCommerce order meta
118 *
119 * @param int $order_id The WooCommerce order ID
120 * @return int|false The booking ID or false if not found
121 */
122 public static function get_booking_id_from_order( $order_id ) {
123 return get_post_meta( $order_id, '_tt_booking_id', true );
124 }
125
126 /**
127 * Store the booking ID in WooCommerce order meta
128 *
129 * @param int $order_id The WooCommerce order ID
130 * @param int $booking_id The booking ID
131 * @return bool Whether the meta was updated
132 */
133 public static function set_booking_id_for_order( $order_id, $booking_id ) {
134 return update_post_meta( $order_id, '_tt_booking_id', $booking_id );
135 }
136
137 /**
138 * Check if a booking-order pair is currently being synced
139 *
140 * @param int $booking_id The booking ID
141 * @param int $order_id The WooCommerce order ID
142 *
143 * @return bool Whether this pair is currently syncing
144 */
145 public static function is_syncing_pair( $booking_id, $order_id ) {
146 $pair_key = "{$booking_id}_{$order_id}";
147 return isset( self::$syncing_pairs[ $pair_key ] );
148 }
149
150 /**
151 * Mark a booking-order pair as currently syncing
152 *
153 * @param int $booking_id The booking ID
154 * @param int $order_id The WooCommerce order ID
155 * @return void
156 */
157 public static function begin_sync_pair( $booking_id, $order_id ) {
158 $pair_key = "{$booking_id}_{$order_id}";
159 self::$syncing_pairs[ $pair_key ] = true;
160 }
161
162 /**
163 * Mark a booking-order pair as finished syncing
164 *
165 * @param int $booking_id The booking ID
166 * @param int $order_id The WooCommerce order ID
167 * @return void
168 */
169 public static function end_sync_pair( $booking_id, $order_id ) {
170 $pair_key = "{$booking_id}_{$order_id}";
171 unset( self::$syncing_pairs[ $pair_key ] );
172 }
173 }
174