PluginProbe
Packeta / 1.5.0
Packeta v1.5.0
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 / WidgetOptionsBuilder.php

WidgetOptionsBuilder.php in Packeta 1.5.0, at src/Packetery/Module/WidgetOptionsBuilder.php

280 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class WidgetOptionsBuilder
4 *
5 * @package Packetery
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module;
11
12 use Packetery\Core\Entity;
13 use Packetery\Core\Entity\Order;
14 use Packetery\Module\Carrier\PacketaPickupPointsConfig;
15 use Packetery\Module\Options\FeatureFlagManager;
16 use Packetery\Module\Order\Repository;
17 use WC_Order;
18
19 /**
20 * Class WidgetOptionsBuilder
21 *
22 * @package Packetery
23 */
24 class WidgetOptionsBuilder {
25
26 /**
27 * Internal pickup points config.
28 *
29 * @var PacketaPickupPointsConfig
30 */
31 private $pickupPointsConfig;
32
33 /**
34 * RateCalculator.
35 *
36 * @var RateCalculator
37 */
38 private $rateCalculator;
39
40 /**
41 * Order repository.
42 *
43 * @var Repository
44 */
45 private $orderRepository;
46
47 /**
48 * Feature flag.
49 *
50 * @var FeatureFlagManager
51 */
52 private $featureFlag;
53
54 /**
55 * WidgetOptionsBuilder constructor.
56 *
57 * @param PacketaPickupPointsConfig $pickupPointsConfig Internal pickup points config.
58 * @param RateCalculator $rateCalculator RateCalculator.
59 * @param Repository $orderRepository Order repository.
60 * @param FeatureFlagManager $featureFlag Feature flag.
61 */
62 public function __construct(
63 PacketaPickupPointsConfig $pickupPointsConfig,
64 RateCalculator $rateCalculator,
65 Repository $orderRepository,
66 FeatureFlagManager $featureFlag
67 ) {
68 $this->pickupPointsConfig = $pickupPointsConfig;
69 $this->rateCalculator = $rateCalculator;
70 $this->orderRepository = $orderRepository;
71 $this->featureFlag = $featureFlag;
72 }
73
74 /**
75 * Gets widget vendors param.
76 *
77 * @param string $carrierId Carrier id.
78 * @param string $country Country.
79 * @param array|null $vendorGroups Vendor groups.
80 *
81 * @return array|null
82 */
83 private function getWidgetVendorsParam( string $carrierId, string $country, ?array $vendorGroups ): ?array {
84 if ( is_numeric( $carrierId ) ) {
85 return [
86 [
87 'carrierId' => $carrierId,
88 'selected' => true,
89 ],
90 ];
91 }
92
93 $vendorCarriers = $this->pickupPointsConfig->getVendorCarriers();
94 if ( ! empty( $vendorCarriers[ $carrierId ] ) ) {
95 $vendorGroups = [ $vendorCarriers[ $carrierId ]->getGroup() ];
96 }
97
98 if ( empty( $vendorGroups ) ) {
99 return null;
100 }
101
102 $vendorsParam = [];
103 foreach ( $vendorGroups as $code ) {
104 $groupSettings = [
105 'selected' => true,
106 'country' => $country,
107 ];
108 if ( Entity\Carrier::VENDOR_GROUP_ZPOINT !== $code ) {
109 $groupSettings['group'] = $code;
110 }
111 $vendorsParam[] = $groupSettings;
112 }
113
114 return $vendorsParam;
115 }
116
117 /**
118 * Gets widget carriers param.
119 *
120 * @param bool $isPickupPoints Is context pickup point related.
121 * @param string $carrierId Carrier id.
122 *
123 * @return string|null
124 */
125 private function getCarriersParam( bool $isPickupPoints, string $carrierId ): ?string {
126 if ( $isPickupPoints ) {
127 return ( is_numeric( $carrierId ) ? $carrierId : Carrier\Repository::INTERNAL_PICKUP_POINTS_ID );
128 }
129
130 return null;
131 }
132
133 /**
134 * Gets carrier configuration for widgets in frontend.
135 *
136 * @param Entity\Carrier $carrier Carrier configuration.
137 * @param float|null $defaultPrice Default price.
138 * @param string $optionId Option id.
139 *
140 * @return array
141 */
142 public function getCarrierForCheckout( Entity\Carrier $carrier, ?float $defaultPrice, string $optionId ): array {
143 $carrierConfigForWidget = [
144 'id' => $carrier->getId(),
145 'is_pickup_points' => (int) $carrier->hasPickupPoints(),
146 'defaultPrice' => $defaultPrice,
147 'defaultCurrency' => $carrier->getCurrency(),
148 ];
149
150 $carrierOption = get_option( $optionId );
151 if ( $carrier->hasPickupPoints() ) {
152 if ( $this->featureFlag->isSplitActive() ) {
153 $carrierConfigForWidget['vendors'] = $this->getWidgetVendorsParam(
154 (string) $carrier->getId(),
155 $carrier->getCountry(),
156 ( ( $carrierOption && isset( $carrierOption['vendor_groups'] ) ) ? $carrierOption['vendor_groups'] : null )
157 );
158 } else {
159 $carrierConfigForWidget['carriers'] = $this->getCarriersParam( true, $carrier->getId() );
160 }
161 }
162
163 if ( ! $carrier->hasPickupPoints() ) {
164 $addressValidation = 'none';
165 if ( $carrierOption && in_array( $carrier->getCountry(), Entity\Carrier::ADDRESS_VALIDATION_COUNTRIES, true ) ) {
166 $addressValidation = ( $carrierOption['address_validation'] ?? $addressValidation );
167 }
168
169 $carrierConfigForWidget['address_validation'] = $addressValidation;
170 }
171
172 return $carrierConfigForWidget;
173 }
174
175 /**
176 * Creates pickup point widget options in backend.
177 *
178 * @param Order $order Order.
179 *
180 * @return array|null
181 */
182 public function createPickupPointForAdmin( Order $order ): array {
183 if ( $order->isExternalCarrier() ) {
184 $carrierId = $order->getCarrierId();
185 } else {
186 $carrierId = $this->pickupPointsConfig->getCompoundCarrierIdByCountry( $order->getShippingCountry() );
187 }
188
189 $defaultPrice = null;
190 if ( null !== $carrierId ) {
191 /**
192 * Cannot be null due to the conditions in the caller method.
193 *
194 * @var WC_Order $wcOrder
195 */
196 $wcOrder = $this->orderRepository->getWcOrderById( (int) $order->getNumber() );
197 $defaultPrice = $this->rateCalculator->getShippingRateCost(
198 Carrier\Options::createByCarrierId( $carrierId ),
199 $wcOrder->get_total() - $wcOrder->get_shipping_total(),
200 $order->getWeight(),
201 $this->rateCalculator->isFreeShippingCouponApplied( $wcOrder )
202 );
203 }
204
205 $widgetOptions = [
206 'country' => $order->getShippingCountry(),
207 'language' => substr( get_user_locale(), 0, 2 ),
208 'appIdentity' => Plugin::getAppIdentity(),
209 'weight' => $order->getFinalWeight(),
210 'defaultPrice' => $defaultPrice,
211 ];
212
213 // TODO: update later when carrier will not be nullable.
214 $orderCarrier = $order->getCarrier();
215 if ( $orderCarrier ) {
216 $widgetOptions['defaultCurrency'] = $orderCarrier->getCurrency();
217 }
218
219 if ( $this->featureFlag->isSplitActive() ) {
220 // In backend, we want all pickup points in that country for packeta carrier.
221 if ( $order->getCarrierId() !== Entity\Carrier::INTERNAL_PICKUP_POINTS_ID ) {
222 $widgetOptions['vendors'] = $this->getWidgetVendorsParam(
223 $order->getCarrierId(),
224 $order->getShippingCountry(),
225 null
226 );
227 }
228 } else {
229 $widgetOptions['carriers'] = $this->getCarriersParam( $order->isPickupPointDelivery(), $order->getCarrierId() );
230 }
231
232 if ( $order->containsAdultContent() ) {
233 $widgetOptions += [ 'livePickupPoint' => true ];
234 }
235
236 return $widgetOptions;
237 }
238
239 /**
240 * Creates address validation widget options in backend.
241 *
242 * @param Order $order Order.
243 *
244 * @return array
245 */
246 public function createAddressForAdmin( Order $order ): array {
247 /**
248 * Delivery address is always present in this case.
249 *
250 * @var Entity\Address $deliveryAddress
251 */
252 $deliveryAddress = $order->getDeliveryAddress();
253 $widgetOptions = [
254 'country' => $order->getShippingCountry(),
255 'language' => substr( get_user_locale(), 0, 2 ),
256 'layout' => 'hd',
257 'appIdentity' => Plugin::getAppIdentity(),
258 'street' => $deliveryAddress->getStreet(),
259 'city' => $deliveryAddress->getCity(),
260 'postcode' => $deliveryAddress->getZip(),
261 ];
262
263 if ( $deliveryAddress->getHouseNumber() ) {
264 $widgetOptions += [ 'houseNumber' => $deliveryAddress->getHouseNumber() ];
265 }
266
267 if ( $deliveryAddress->getCounty() ) {
268 $widgetOptions += [ 'county' => $deliveryAddress->getCounty() ];
269 }
270
271 // TODO: Redo in carrier refactor.
272 if ( $order->getCarrier() && is_numeric( $order->getCarrier()->getId() ) ) {
273 $widgetOptions += [ 'carrierId' => $order->getCarrier()->getId() ];
274 }
275
276 return $widgetOptions;
277 }
278
279 }
280