PluginProbe
Packeta / trunk
Packeta vtrunk
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 trunk, at src/Packetery/Module/Checkout/OrderUpdater.php

300 lines 8.6 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\DiagnosticsLogger\DiagnosticsLogger;
10 use Packetery\Module\EntityFactory\SizeFactory;
11 use Packetery\Module\Framework\WcAdapter;
12 use Packetery\Module\Framework\WpAdapter;
13 use Packetery\Module\Options\OptionsProvider;
14 use Packetery\Module\Order;
15 use Packetery\Module\Order\PickupPointValidator;
16 use WC_Data_Exception;
17 use WC_Order;
18
19 class OrderUpdater {
20
21 /** @var WpAdapter */
22 private $wpAdapter;
23
24 /** @var WcAdapter */
25 private $wcAdapter;
26
27 /**
28 * @var Order\Repository
29 */
30 private $orderRepository;
31
32 /**
33 * @var CheckoutService
34 */
35 private $checkoutService;
36
37 /**
38 * @var CheckoutStorage
39 */
40 private $storage;
41
42 /**
43 * @var OptionsProvider
44 */
45 private $optionsProvider;
46
47 /**
48 * @var Order\AttributeMapper
49 */
50 private $mapper;
51
52 /**
53 * @var Carrier\EntityRepository
54 */
55 private $carrierEntityRepository;
56
57 /**
58 * @var CartService
59 */
60 private $cartService;
61
62 /**
63 * @var Order\PacketAutoSubmitter
64 */
65 private $packetAutoSubmitter;
66
67 /**
68 * @var SizeFactory
69 */
70 private $sizeFactory;
71
72 /**
73 * @var DiagnosticsLogger
74 */
75 private $diagnosticsLogger;
76
77 public function __construct(
78 WpAdapter $wpAdapter,
79 WcAdapter $wcAdapter,
80 Order\Repository $orderRepository,
81 CheckoutService $checkoutService,
82 CheckoutStorage $checkoutStorage,
83 OptionsProvider $optionsProvider,
84 Order\AttributeMapper $attributeMapper,
85 Carrier\EntityRepository $carrierEntityRepository,
86 CartService $cartService,
87 Order\PacketAutoSubmitter $packetAutoSubmitter,
88 SizeFactory $sizeFactory,
89 DiagnosticsLogger $diagnosticsLogger
90 ) {
91 $this->wpAdapter = $wpAdapter;
92 $this->wcAdapter = $wcAdapter;
93 $this->orderRepository = $orderRepository;
94 $this->checkoutService = $checkoutService;
95 $this->storage = $checkoutStorage;
96 $this->optionsProvider = $optionsProvider;
97 $this->mapper = $attributeMapper;
98 $this->carrierEntityRepository = $carrierEntityRepository;
99 $this->cartService = $cartService;
100 $this->packetAutoSubmitter = $packetAutoSubmitter;
101 $this->sizeFactory = $sizeFactory;
102 $this->diagnosticsLogger = $diagnosticsLogger;
103 }
104
105 /**
106 * Saves pickup point and other Packeta information to order.
107 *
108 * @throws WC_Data_Exception When invalid data are passed during shipping address update.
109 */
110 public function actionUpdateOrderById( int $orderId ): void {
111 $wcOrder = $this->orderRepository->getWcOrderById( $orderId );
112 if ( $wcOrder === null ) {
113 $this->diagnosticsLogger->log( 'Action update order by id - Order not found', [ 'orderId' => $orderId ] );
114
115 return;
116 }
117
118 $this->actionUpdateOrder( $wcOrder );
119 }
120
121 /**
122 * Saves pickup point and other Packeta information to order.
123 *
124 * @throws WC_Data_Exception When invalid data are passed during shipping address update.
125 */
126 public function actionUpdateOrder( WC_Order $wcOrder ): void {
127 $chosenMethod = $this->checkoutService->resolveChosenMethod();
128 if (
129 $chosenMethod === null ||
130 $this->checkoutService->isPacketeryShippingMethod( $chosenMethod ) === false
131 ) {
132 $this->diagnosticsLogger->log( 'Action update order - No packetery shipping method chosen', [ 'chosenMethod' => $chosenMethod ] );
133
134 return;
135 }
136
137 $checkoutData = $this->storage->getPostDataIncludingStoredData( $chosenMethod, $wcOrder->get_id() );
138 $propsToSave = [];
139 $carrierId = $this->checkoutService->getCarrierIdFromPacketeryShippingMethod( $chosenMethod );
140 $orderHasUnsavedChanges = false;
141
142 $propsToSave[ Order\Attribute::CARRIER_ID ] = $carrierId;
143
144 $this->diagnosticsLogger->log(
145 'Action update order - Chosen method',
146 [
147 'chosenMethod' => $chosenMethod,
148 'checkoutData' => $checkoutData,
149 'propsToSave' => $propsToSave,
150 ]
151 );
152
153 if ( $this->checkoutService->isPickupPointOrder() ) {
154 $this->addPickupPointValidationError( $wcOrder );
155
156 if ( count( $checkoutData ) === 0 ) {
157 $this->diagnosticsLogger->log( 'Action update order - No pickup point data', [] );
158
159 return;
160 }
161 $propsToSave = $this->getPropsFromCheckoutData( $checkoutData, $propsToSave, $wcOrder );
162 $this->diagnosticsLogger->log(
163 'Action update order - Pickup point data',
164 [
165 'propsToSave' => $propsToSave,
166 ]
167 );
168
169 $orderHasUnsavedChanges = true;
170 }
171
172 $carrier = $this->carrierEntityRepository->getAnyById( $carrierId );
173 if ( $carrier === null ) {
174 $this->diagnosticsLogger->log( 'Action update order - Carrier not found', [] );
175
176 return;
177 }
178 $order = new Entity\Order( (string) $wcOrder->get_id(), $carrier );
179
180 $orderHasUnsavedChanges = $this->updateHomeDelivery(
181 $checkoutData,
182 $order,
183 $wcOrder,
184 $orderHasUnsavedChanges
185 );
186 if ( $orderHasUnsavedChanges ) {
187 $wcOrder->save();
188 }
189
190 $this->updateCarDelivery( $checkoutData, $order );
191
192 if ( $this->cartService->getCartWeightKg() === 0.0 && $this->optionsProvider->isDefaultWeightEnabled() === true ) {
193 $order->setWeight( $this->optionsProvider->getDefaultWeight() + $this->optionsProvider->getPackagingWeight() );
194 }
195
196 if (
197 $carrier->requiresSize() === true &&
198 $this->optionsProvider->isDefaultDimensionsEnabled() === true
199 ) {
200 $order->setSize( $this->sizeFactory->createDefaultSizeForNewOrder() );
201 }
202
203 $pickupPoint = $this->mapper->toOrderEntityPickupPoint( $order, $propsToSave );
204 $this->diagnosticsLogger->log(
205 'Action update order',
206 [
207 'pickupPoint' => $pickupPoint,
208 'order' => $order,
209 ]
210 );
211 $order->setPickupPoint( $pickupPoint );
212
213 $this->storage->deleteTransient();
214 $this->orderRepository->save( $order );
215 $this->packetAutoSubmitter->handleEventAsync( Order\PacketAutoSubmitter::EVENT_ON_ORDER_CREATION_FE, $wcOrder->get_id() );
216 }
217
218 private function addPickupPointValidationError( WC_Order $wcOrder ): void {
219 if ( $this->optionsProvider->isPickupPointValidationEnabled() ) {
220 $pickupPointValidationError = $this->wcAdapter->sessionGetString( PickupPointValidator::VALIDATION_HTTP_ERROR_SESSION_KEY );
221 if ( $pickupPointValidationError !== null ) {
222 $wcOrder->add_order_note(
223 sprintf(
224 // translators: %s: Message from downloader.
225 $this->wpAdapter->__( 'The selected Packeta pickup point could not be validated, reason: %s.', 'packeta' ),
226 $pickupPointValidationError
227 )
228 );
229 $this->wcAdapter->sessionSet( PickupPointValidator::VALIDATION_HTTP_ERROR_SESSION_KEY, null );
230 }
231 }
232 }
233
234 /**
235 * @throws WC_Data_Exception When invalid data are passed during shipping address update.
236 */
237 private function getPropsFromCheckoutData( array $checkoutData, array $propsToSave, WC_Order $wcOrder ): array {
238 foreach ( Order\Attribute::$pickupPointAttributes as $attr ) {
239 $attrName = $attr['name'];
240 if ( ! isset( $checkoutData[ $attrName ] ) ) {
241 continue;
242 }
243 $attrValue = $checkoutData[ $attrName ];
244
245 $saveMeta = true;
246 if (
247 $attrName === Order\Attribute::CARRIER_ID ||
248 ( $attrName === Order\Attribute::POINT_URL && ! filter_var( $attrValue, FILTER_VALIDATE_URL ) )
249 ) {
250 $saveMeta = false;
251 }
252 if ( $saveMeta ) {
253 $propsToSave[ $attrName ] = $attrValue;
254 }
255
256 if ( $this->optionsProvider->replaceShippingAddressWithPickupPointAddress() ) {
257 $this->mapper->toWcOrderShippingAddress( $wcOrder, $attrName, (string) $attrValue );
258 }
259 }
260
261 return $propsToSave;
262 }
263
264 private function updateHomeDelivery( array $checkoutData, Entity\Order $orderEntity, WC_Order $wcOrder, bool $orderHasUnsavedChanges ): bool {
265 if (
266 ! isset( $checkoutData[ Order\Attribute::ADDRESS_IS_VALIDATED ] ) ||
267 $checkoutData[ Order\Attribute::ADDRESS_IS_VALIDATED ] !== '1' ||
268 ! $this->checkoutService->isHomeDeliveryOrder()
269 ) {
270 return $orderHasUnsavedChanges;
271 }
272
273 $validatedAddress = $this->mapper->toValidatedAddress( $checkoutData );
274 $orderEntity->setDeliveryAddress( $validatedAddress );
275 $orderEntity->setAddressValidated( true );
276 if ( $this->checkoutService->areBlocksUsedInCheckout() ) {
277 $this->mapper->validatedAddressToWcOrderShippingAddress( $wcOrder, $checkoutData );
278 $orderHasUnsavedChanges = true;
279 }
280
281 return $orderHasUnsavedChanges;
282 }
283
284 /**
285 * @param array $checkoutData
286 * @param Entity\Order $orderEntity
287 *
288 * @return void
289 */
290 private function updateCarDelivery( array $checkoutData, Entity\Order $orderEntity ): void {
291 if ( count( $checkoutData ) <= 0 || ! $this->checkoutService->isCarDeliveryOrder() ) {
292 return;
293 }
294 $address = $this->mapper->toCarDeliveryAddress( $checkoutData );
295 $orderEntity->setDeliveryAddress( $address );
296 $orderEntity->setAddressValidated( true );
297 $orderEntity->setCarDeliveryId( $checkoutData[ Order\Attribute::CAR_DELIVERY_ID ] );
298 }
299 }
300