| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class MetaboxesWrapper. |
| 4 |
* |
| 5 |
* @package Packetery |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
|
| 11 |
namespace Packetery\Module\Order; |
| 12 |
|
| 13 |
use Packetery\Module\Exception\InvalidCarrierException; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class MetaboxesWrapper. |
| 17 |
*/ |
| 18 |
class MetaboxesWrapper { |
| 19 |
|
| 20 |
/** |
| 21 |
* General order metabox. |
| 22 |
* |
| 23 |
* @var Metabox |
| 24 |
*/ |
| 25 |
private $generalMetabox; |
| 26 |
|
| 27 |
/** |
| 28 |
* Customs declaration metabox. |
| 29 |
* |
| 30 |
* @var CustomsDeclarationMetabox |
| 31 |
*/ |
| 32 |
private $customDeclarationMetabox; |
| 33 |
|
| 34 |
/** |
| 35 |
* Order repository. |
| 36 |
* |
| 37 |
* @var Repository |
| 38 |
*/ |
| 39 |
private $orderRepository; |
| 40 |
|
| 41 |
/** |
| 42 |
* Constructor. |
| 43 |
* |
| 44 |
* @param Metabox $generalMetabox General metabox. |
| 45 |
* @param CustomsDeclarationMetabox $customDeclarationMetabox Customs declaration metabox. |
| 46 |
* @param Repository $orderRepository Order repository. |
| 47 |
*/ |
| 48 |
public function __construct( |
| 49 |
Metabox $generalMetabox, |
| 50 |
CustomsDeclarationMetabox $customDeclarationMetabox, |
| 51 |
Repository $orderRepository |
| 52 |
) { |
| 53 |
$this->generalMetabox = $generalMetabox; |
| 54 |
$this->customDeclarationMetabox = $customDeclarationMetabox; |
| 55 |
$this->orderRepository = $orderRepository; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Registers metaboxes. |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
public function register(): void { |
| 64 |
$this->generalMetabox->register(); |
| 65 |
$this->customDeclarationMetabox->register(); |
| 66 |
add_action( 'save_post_shop_order', [ $this, 'saveFields' ] ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Saves metabox fields. |
| 71 |
* |
| 72 |
* @param int|mixed $wcOrderId Order ID. |
| 73 |
* |
| 74 |
* @return void |
| 75 |
* @throws \WC_Data_Exception When invalid data are passed during shipping address update. |
| 76 |
*/ |
| 77 |
public function saveFields( $wcOrderId ): void { |
| 78 |
try { |
| 79 |
$order = $this->orderRepository->getById( (int) $wcOrderId ); |
| 80 |
} catch ( InvalidCarrierException $invalidCarrierException ) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
if ( null === $order ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
$this->generalMetabox->saveFields( $order ); |
| 89 |
$this->customDeclarationMetabox->saveFields( $order ); |
| 90 |
} |
| 91 |
} |
| 92 |
|