PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 5.4.3
WooCommerce Square v5.4.3
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 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Sync / Order_Mapper.php
woocommerce-square / includes / Sync Last commit date
Records 3 months ago Catalog_Item.php 9 months ago Helper.php 1 year ago Interval_Polling.php 6 months ago Job.php 2 years ago Manual_Synchronization.php 1 month ago Order_Importer.php 11 months ago Order_Mapper.php 11 months ago Order_Polling.php 11 months ago Product_Import.php 2 months ago Records.php 2 years ago Stepped_Job.php 2 years ago
Order_Mapper.php
85 lines
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