PluginProbe
Packeta / 2.1
Packeta v2.1
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
← All changes | src/Packetery/Module/Order/MetaboxesWrapper.php +20 -10 1.6.12.1 View file →
@@ -6,12 +6,11 @@
6 6 */
7 7
8 8 declare( strict_types=1 );
9 9
10 -
11 10 namespace Packetery\Module\Order;
12 11
13 -use Packetery\Module\Exception\InvalidCarrierException;
12 +use WC_Order;
14 13
15 14 /**
16 15 * Class MetaboxesWrapper.
17 16 */
@@ -62,12 +61,28 @@
62 61 */
63 62 public function register(): void {
64 63 $this->generalMetabox->register();
65 64 $this->customDeclarationMetabox->register();
66 - add_action( 'save_post_shop_order', [ $this, 'saveFields' ] );
65 + add_action( 'woocommerce_before_order_object_save', [ $this, 'beforeOrderSave' ], PHP_INT_MAX );
67 66 }
68 67
69 68 /**
69 + * Updates order object before persisting its new data to DB.
70 + *
71 + * @param WC_Order $wcOrder WC Order.
72 + *
73 + * @return void
74 + */
75 + public function beforeOrderSave( WC_Order $wcOrder ): void {
76 + $order = $this->orderRepository->getByIdWithValidCarrier( $wcOrder->get_id() );
77 + if ( $order === null ) {
78 + return;
79 + }
80 +
81 + $this->generalMetabox->saveFields( $order, $wcOrder );
82 + }
83 +
84 + /**
70 85 * Saves metabox fields.
71 86 *
72 87 * @param int|mixed $wcOrderId Order ID.
73 88 *
@@ -74,18 +89,13 @@
74 89 * @return void
75 90 * @throws \WC_Data_Exception When invalid data are passed during shipping address update.
76 91 */
77 92 public function saveFields( $wcOrderId ): void {
78 - try {
79 - $order = $this->orderRepository->getById( (int) $wcOrderId );
80 - } catch ( InvalidCarrierException $invalidCarrierException ) {
81 - return;
82 - }
93 + $order = $this->orderRepository->getByIdWithValidCarrier( (int) $wcOrderId );
83 94
84 - if ( null === $order ) {
95 + if ( $order === null ) {
85 96 return;
86 97 }
87 98
88 - $this->generalMetabox->saveFields( $order );
89 99 $this->customDeclarationMetabox->saveFields( $order );
90 100 }
91 101 }