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 |