PluginProbe
Packeta / 1.6.3
Packeta v1.6.3
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Order / MetaboxesWrapper.php

MetaboxesWrapper.php in Packeta 1.6.3, at src/Packetery/Module/Order/MetaboxesWrapper.php

92 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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