| 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 create. */ |
| 30 |
public function after_create( int $id, array $payload ): void { |
| 31 |
$this->write_tax_ids( $id, $payload ); |
| 32 |
} |
| 33 |
|
| 34 |
/** Persist customer tax IDs after recovery. */ |
| 35 |
public function after_recovery( int $id, array $payload ): void { |
| 36 |
$this->write_tax_ids( $id, $payload ); |
| 37 |
} |
| 38 |
|
| 39 |
/** Persist customer tax IDs after update. */ |
| 40 |
public function after_update( int $id, array $payload, array $current, array $response_data, array $context ): void { |
| 41 |
$this->write_tax_ids( $id, $payload ); |
| 42 |
} |
| 43 |
|
| 44 |
/** The one tax-ID write every lifecycle phase shares; identity itself never carries tax ids. */ |
| 45 |
private function write_tax_ids( int $id, array $payload ): void { |
| 46 |
if ( $id <= 0 || ! is_array( $payload['tax_ids'] ?? null ) ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
( new Tax_Id_Writer() )->write_for_user( $id, $payload['tax_ids'] ); |
| 50 |
} |
| 51 |
|
| 52 |
/** Remove customer-only fields from the wc/v3 forward. */ |
| 53 |
private function prepare_payload( array $payload ): array { |
| 54 |
unset( $payload['tax_ids'] ); |
| 55 |
return ( new Order_Write_Payload() )->without_empty_billing_email( $payload ); |
| 56 |
} |
| 57 |
} |
| 58 |
|