| @@ -1,7 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Timetics\Core\Integrations\Woocommerce; |
| 3 | 3 | |
| 4 | +defined( 'ABSPATH' ) || exit; | |
| 5 | + | |
| 4 | 6 | /** |
| 5 | 7 | * Class Status_Mapper |
| 6 | 8 | * |
| 7 | 9 | * Maps booking statuses to WooCommerce order statuses and vice versa |
| @@ -115,24 +117,48 @@ | ||
| 115 | 117 | |
| 116 | 118 | /** |
| 117 | 119 | * Get the booking ID from WooCommerce order meta |
| 118 | 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 | + * | |
| 119 | 124 | * @param int $order_id The WooCommerce order ID |
| 120 | 125 | * @return int|false The booking ID or false if not found |
| 121 | 126 | */ |
| 122 | 127 | public static function get_booking_id_from_order( $order_id ) { |
| 123 | - return get_post_meta( $order_id, '_tt_booking_id', true ); | |
| 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 ); | |
| 124 | 137 | } |
| 125 | 138 | |
| 126 | 139 | /** |
| 127 | 140 | * Store the booking ID in WooCommerce order meta |
| 128 | 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 | + * | |
| 129 | 146 | * @param int $order_id The WooCommerce order ID |
| 130 | 147 | * @param int $booking_id The booking ID |
| 131 | 148 | * @return bool Whether the meta was updated |
| 132 | 149 | */ |
| 133 | 150 | public static function set_booking_id_for_order( $order_id, $booking_id ) { |
| 134 | - return update_post_meta( $order_id, '_tt_booking_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; | |
| 135 | 161 | } |
| 136 | 162 | |
| 137 | 163 | /** |
| 138 | 164 | * Check if a booking-order pair is currently being synced |