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

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