PluginProbe
WooCommerce Square / 5.0.0
WooCommerce Square v5.0.0
5.5.0 5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 All 132 releases
woocommerce-square / includes / Sync / Order_Mapper.php
Order_Mapper.php
85 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Order Data Mapper.
4 *
5 * Maps data between Square and WooCommerce order formats.
6 *
7 * @package WooCommerce\Square\Sync
8 * @since 5.0.0
9 */
10
11 namespace WooCommerce\Square\Sync;
12
13 /**
14 * Class Order_Mapper
15 *
16 * Provides static methods to map order data between Square and WooCommerce.
17 *
18 * @since 5.0.0
19 */
20 class Order_Mapper {
21 /**
22 * Map Square order state to WooCommerce order status.
23 *
24 * @since 5.0.0
25 * @param string $square_state Square order state.
26 * @return string WooCommerce order status.
27 */
28 public static function map_square_state_to_wc_status( $square_state ) {
29 $status_map = array(
30 'OPEN' => 'processing',
31 'COMPLETED' => 'completed',
32 'CANCELED' => 'cancelled',
33 'DRAFT' => 'pending',
34 );
35
36 return isset( $status_map[ $square_state ] ) ? $status_map[ $square_state ] : 'processing';
37 }
38
39 /**
40 * Check if order status change is allowed.
41 *
42 * @since 5.0.0
43 * @param string $from_status Current WooCommerce status.
44 * @param string $to_status New status from Square.
45 * @return bool True if status change is allowed.
46 */
47 public static function is_status_change_allowed( $from_status, $to_status ) {
48 // Don't allow status changes from terminal states unless it's a specific case.
49 $terminal_statuses = array( 'completed', 'cancelled', 'refunded', 'failed' );
50
51 /**
52 * Filters the allowed status changes.
53 *
54 * @since 5.0.0
55 *
56 * @param bool $allowed_status_changes True if status change is allowed.
57 * @param string $from_status Current WooCommerce status.
58 * @param string $to_status New status from Square.
59 *
60 * @return bool True if status change is allowed.
61 */
62 $allowed_status_changes = apply_filters( 'wc_square_allowed_status_changes', false, $from_status, $to_status );
63
64 // Return early true if status change is allowed by filter.
65 if ( $allowed_status_changes ) {
66 return true;
67 }
68
69 if ( in_array( $from_status, $terminal_statuses, true ) ) {
70 // Only allow specific transitions from terminal states.
71 $allowed_terminal_transitions = array(
72 'completed' => array( 'cancelled' ), // Allow cancellation of completed orders.
73 'cancelled' => array(), // No transitions from cancelled.
74 'refunded' => array(), // No transitions from refunded.
75 'failed' => array( 'pending', 'processing' ), // Allow retry of failed orders.
76 );
77
78 return in_array( $to_status, $allowed_terminal_transitions[ $from_status ] ?? array(), true );
79 }
80
81 // Allow all other status changes.
82 return true;
83 }
84 }
85