| 1 |
<?php |
| 2 |
/** |
| 3 |
* Customer collection writer. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS\API\V2\Writers |
| 6 |
*/ |
| 7 |
|
| 8 |
// phpcs:disable Squiz.Commenting, Generic.Commenting -- Lifecycle docblocks are intentionally concise. |
| 9 |
|
| 10 |
namespace WCPOS\WooCommercePOS\API\V2\Writers; |
| 11 |
|
| 12 |
use WCPOS\WooCommercePOS\Services\Tax_Id_Writer; |
| 13 |
use WCPOS\WooCommercePOS\Sync\Order_Write_Payload; |
| 14 |
|
| 15 |
/** Adapts customer tax IDs and walk-in billing emails around wc/v3. */ |
| 16 |
class Customer_Writer extends Null_Writer { |
| 17 |
/** Prepare a customer create. */ |
| 18 |
public function prepare_create( array $meta, array $payload, callable $validate_tax_ids ) { |
| 19 |
$error = $validate_tax_ids( $payload ); |
| 20 |
return is_wp_error( $error ) ? $error : parent::prepare_create( $meta, $this->prepare_payload( $payload ), $validate_tax_ids ); |
| 21 |
} |
| 22 |
|
| 23 |
/** Prepare a customer update. */ |
| 24 |
public function prepare_update( array $meta, int $id, array $payload, callable $validate_tax_ids ) { |
| 25 |
$error = $validate_tax_ids( $payload ); |
| 26 |
return is_wp_error( $error ) ? $error : parent::prepare_update( $meta, $id, $this->prepare_payload( $payload ), $validate_tax_ids ); |
| 27 |
} |
| 28 |
|
| 29 |
/** Persist customer tax IDs after successful writes and poison recovery. */ |
| 30 |
public function persist( string $phase, int $id, array $payload, array $current = array(), array $response_data = array(), array $context = array() ): void { |
| 31 |
if ( ! in_array( $phase, array( 'create_before_identity', 'create_recovery', 'update' ), true ) || $id <= 0 || ! is_array( $payload['tax_ids'] ?? null ) ) { |
| 32 |
return; |
| 33 |
} |
| 34 |
( new Tax_Id_Writer() )->write_for_user( $id, $payload['tax_ids'] ); |
| 35 |
} |
| 36 |
|
| 37 |
/** Remove customer-only fields from the wc/v3 forward. */ |
| 38 |
private function prepare_payload( array $payload ): array { |
| 39 |
unset( $payload['tax_ids'] ); |
| 40 |
return ( new Order_Write_Payload() )->without_empty_billing_email( $payload ); |
| 41 |
} |
| 42 |
} |
| 43 |
|