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
packeta / src / Packetery / Module / Checkout / OrderUpdater.php

OrderUpdater.php in Packeta 2.1, at src/Packetery/Module/Checkout/OrderUpdater.php

231 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare( strict_types=1 );
4
5 namespace Packetery\Module\Checkout;
6
7 use Packetery\Core\Entity;
8 use Packetery\Module\Carrier;
9 use Packetery\Module\EntityFactory\SizeFactory;
10 use Packetery\Module\Options\OptionsProvider;
11 use Packetery\Module\Order;
12 use WC_Data_Exception;
13 use WC_Order;
14
15 class OrderUpdater {
16
17 /**
18 * @var Order\Repository
19 */
20 private $orderRepository;
21
22 /**
23 * @var CheckoutService
24 */
25 private $checkoutService;
26
27 /**
28 * @var CheckoutStorage
29 */
30 private $storage;
31
32 /**
33 * @var OptionsProvider
34 */
35 private $optionsProvider;
36
37 /**
38 * @var Order\AttributeMapper
39 */
40 private $mapper;
41
42 /**
43 * @var Carrier\EntityRepository
44 */
45 private $carrierEntityRepository;
46
47 /**
48 * @var CartService
49 */
50 private $cartService;
51
52 /**
53 * @var Order\PacketAutoSubmitter
54 */
55 private $packetAutoSubmitter;
56
57 /**
58 * @var SizeFactory
59 */
60 private $sizeFactory;
61
62 public function __construct(
63 Order\Repository $orderRepository,
64 CheckoutService $checkoutService,
65 CheckoutStorage $checkoutStorage,
66 OptionsProvider $optionsProvider,
67 Order\AttributeMapper $attributeMapper,
68 Carrier\EntityRepository $carrierEntityRepository,
69 CartService $cartService,
70 Order\PacketAutoSubmitter $packetAutoSubmitter,
71 SizeFactory $sizeFactory
72 ) {
73 $this->orderRepository = $orderRepository;
74 $this->checkoutService = $checkoutService;
75 $this->storage = $checkoutStorage;
76 $this->optionsProvider = $optionsProvider;
77 $this->mapper = $attributeMapper;
78 $this->carrierEntityRepository = $carrierEntityRepository;
79 $this->cartService = $cartService;
80 $this->packetAutoSubmitter = $packetAutoSubmitter;
81 $this->sizeFactory = $sizeFactory;
82 }
83
84 /**
85 * Saves pickup point and other Packeta information to order.
86 *
87 * @throws WC_Data_Exception When invalid data are passed during shipping address update.
88 */
89 public function actionUpdateOrderById( int $orderId ): void {
90 $wcOrder = $this->orderRepository->getWcOrderById( $orderId );
91 if ( $wcOrder === null ) {
92 return;
93 }
94
95 $this->actionUpdateOrder( $wcOrder );
96 }
97
98 /**
99 * Saves pickup point and other Packeta information to order.
100 *
101 * @throws WC_Data_Exception When invalid data are passed during shipping address update.
102 */
103 public function actionUpdateOrder( WC_Order $wcOrder ): void {
104 $chosenMethod = $this->checkoutService->resolveChosenMethod();
105 if (
106 $chosenMethod === null ||
107 $this->checkoutService->isPacketeryShippingMethod( $chosenMethod ) === false
108 ) {
109 return;
110 }
111
112 $checkoutData = $this->storage->getPostDataIncludingStoredData( $chosenMethod, $wcOrder->get_id() );
113 $propsToSave = [];
114 $carrierId = $this->checkoutService->getCarrierIdFromPacketeryShippingMethod( $chosenMethod );
115 $orderHasUnsavedChanges = false;
116
117 $propsToSave[ Order\Attribute::CARRIER_ID ] = $carrierId;
118
119 if ( $this->checkoutService->isPickupPointOrder() ) {
120 if ( count( $checkoutData ) === 0 ) {
121 return;
122 }
123 $propsToSave = $this->getPropsFromCheckoutData( $checkoutData, $propsToSave, $wcOrder );
124
125 $orderHasUnsavedChanges = true;
126 }
127
128 $carrier = $this->carrierEntityRepository->getAnyById( $carrierId );
129 if ( $carrier === null ) {
130 return;
131 }
132 $order = new Entity\Order( (string) $wcOrder->get_id(), $carrier );
133
134 $orderHasUnsavedChanges = $this->updateHomeDelivery(
135 $checkoutData,
136 $order,
137 $wcOrder,
138 $orderHasUnsavedChanges
139 );
140 if ( $orderHasUnsavedChanges ) {
141 $wcOrder->save();
142 }
143
144 $this->updateCarDelivery( $checkoutData, $order );
145
146 if ( $this->cartService->getCartWeightKg() === 0.0 && $this->optionsProvider->isDefaultWeightEnabled() === true ) {
147 $order->setWeight( $this->optionsProvider->getDefaultWeight() + $this->optionsProvider->getPackagingWeight() );
148 }
149
150 if (
151 $carrier->requiresSize() === true &&
152 $this->optionsProvider->isDefaultDimensionsEnabled() === true
153 ) {
154 $order->setSize( $this->sizeFactory->createDefaultSizeForNewOrder() );
155 }
156
157 $pickupPoint = $this->mapper->toOrderEntityPickupPoint( $order, $propsToSave );
158 $order->setPickupPoint( $pickupPoint );
159
160 $this->storage->deleteTransient();
161 $this->orderRepository->save( $order );
162 $this->packetAutoSubmitter->handleEventAsync( Order\PacketAutoSubmitter::EVENT_ON_ORDER_CREATION_FE, $wcOrder->get_id() );
163 }
164
165 /**
166 * @throws WC_Data_Exception When invalid data are passed during shipping address update.
167 */
168 private function getPropsFromCheckoutData( array $checkoutData, array $propsToSave, WC_Order $wcOrder ): array {
169 foreach ( Order\Attribute::$pickupPointAttributes as $attr ) {
170 $attrName = $attr['name'];
171 if ( ! isset( $checkoutData[ $attrName ] ) ) {
172 continue;
173 }
174 $attrValue = $checkoutData[ $attrName ];
175
176 $saveMeta = true;
177 if (
178 $attrName === Order\Attribute::CARRIER_ID ||
179 ( $attrName === Order\Attribute::POINT_URL && ! filter_var( $attrValue, FILTER_VALIDATE_URL ) )
180 ) {
181 $saveMeta = false;
182 }
183 if ( $saveMeta ) {
184 $propsToSave[ $attrName ] = $attrValue;
185 }
186
187 if ( $this->optionsProvider->replaceShippingAddressWithPickupPointAddress() ) {
188 $this->mapper->toWcOrderShippingAddress( $wcOrder, $attrName, (string) $attrValue );
189 }
190 }
191
192 return $propsToSave;
193 }
194
195 private function updateHomeDelivery( array $checkoutData, Entity\Order $orderEntity, WC_Order $wcOrder, bool $orderHasUnsavedChanges ): bool {
196 if (
197 ! isset( $checkoutData[ Order\Attribute::ADDRESS_IS_VALIDATED ] ) ||
198 $checkoutData[ Order\Attribute::ADDRESS_IS_VALIDATED ] !== '1' ||
199 ! $this->checkoutService->isHomeDeliveryOrder()
200 ) {
201 return $orderHasUnsavedChanges;
202 }
203
204 $validatedAddress = $this->mapper->toValidatedAddress( $checkoutData );
205 $orderEntity->setDeliveryAddress( $validatedAddress );
206 $orderEntity->setAddressValidated( true );
207 if ( $this->checkoutService->areBlocksUsedInCheckout() ) {
208 $this->mapper->validatedAddressToWcOrderShippingAddress( $wcOrder, $checkoutData );
209 $orderHasUnsavedChanges = true;
210 }
211
212 return $orderHasUnsavedChanges;
213 }
214
215 /**
216 * @param array $checkoutData
217 * @param Entity\Order $orderEntity
218 *
219 * @return void
220 */
221 private function updateCarDelivery( array $checkoutData, Entity\Order $orderEntity ): void {
222 if ( count( $checkoutData ) <= 0 || ! $this->checkoutService->isCarDeliveryOrder() ) {
223 return;
224 }
225 $address = $this->mapper->toCarDeliveryAddress( $checkoutData );
226 $orderEntity->setDeliveryAddress( $address );
227 $orderEntity->setAddressValidated( true );
228 $orderEntity->setCarDeliveryId( $checkoutData[ Order\Attribute::CAR_DELIVERY_ID ] );
229 }
230 }
231