PluginProbe
Packeta / 1.5.4
Packeta v1.5.4
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.4, at src/Packetery/Module/WidgetOptionsBuilder.php

229 lines 6.0 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
17 /**
18 * Class WidgetOptionsBuilder
19 *
20 * @package Packetery
21 */
22 class WidgetOptionsBuilder {
23
24 /**
25 * Internal pickup points config.
26 *
27 * @var PacketaPickupPointsConfig
28 */
29 private $pickupPointsConfig;
30
31 /**
32 * Feature flag.
33 *
34 * @var FeatureFlagManager
35 */
36 private $featureFlag;
37
38 /**
39 * WidgetOptionsBuilder constructor.
40 *
41 * @param PacketaPickupPointsConfig $pickupPointsConfig Internal pickup points config.
42 * @param FeatureFlagManager $featureFlag Feature flag.
43 */
44 public function __construct(
45 PacketaPickupPointsConfig $pickupPointsConfig,
46 FeatureFlagManager $featureFlag
47 ) {
48 $this->pickupPointsConfig = $pickupPointsConfig;
49 $this->featureFlag = $featureFlag;
50 }
51
52 /**
53 * Gets widget vendors param.
54 *
55 * @param string $carrierId Carrier id.
56 * @param string $country Country.
57 * @param array|null $vendorGroups Vendor groups.
58 *
59 * @return array|null
60 */
61 private function getWidgetVendorsParam( string $carrierId, string $country, ?array $vendorGroups ): ?array {
62 if ( is_numeric( $carrierId ) ) {
63 return [
64 [
65 'carrierId' => $carrierId,
66 'selected' => true,
67 ],
68 ];
69 }
70
71 $vendorCarriers = $this->pickupPointsConfig->getVendorCarriers();
72 if ( ! empty( $vendorCarriers[ $carrierId ] ) ) {
73 $vendorGroups = [ $vendorCarriers[ $carrierId ]->getGroup() ];
74 }
75
76 if ( empty( $vendorGroups ) ) {
77 return null;
78 }
79
80 $vendorsParam = [];
81 foreach ( $vendorGroups as $code ) {
82 $groupSettings = [
83 'selected' => true,
84 'country' => $country,
85 ];
86 if ( Entity\Carrier::VENDOR_GROUP_ZPOINT !== $code ) {
87 $groupSettings['group'] = $code;
88 }
89 $vendorsParam[] = $groupSettings;
90 }
91
92 return $vendorsParam;
93 }
94
95 /**
96 * Gets widget carriers param.
97 *
98 * @param bool $isPickupPoints Is context pickup point related.
99 * @param string $carrierId Carrier id.
100 *
101 * @return string|null
102 */
103 private function getCarriersParam( bool $isPickupPoints, string $carrierId ): ?string {
104 if ( $isPickupPoints ) {
105 return ( is_numeric( $carrierId ) ? $carrierId : Carrier\Repository::INTERNAL_PICKUP_POINTS_ID );
106 }
107
108 return null;
109 }
110
111 /**
112 * Gets carrier configuration for widgets in frontend.
113 *
114 * @param Entity\Carrier $carrier Carrier configuration.
115 * @param float|null $defaultPrice Default price.
116 * @param string $optionId Option id.
117 *
118 * @return array
119 */
120 public function getCarrierForCheckout( Entity\Carrier $carrier, ?float $defaultPrice, string $optionId ): array {
121 $carrierConfigForWidget = [
122 'id' => $carrier->getId(),
123 'is_pickup_points' => (int) $carrier->hasPickupPoints(),
124 'defaultPrice' => $defaultPrice,
125 'defaultCurrency' => get_woocommerce_currency(),
126 ];
127
128 $carrierOption = get_option( $optionId );
129 if ( $carrier->hasPickupPoints() ) {
130 if ( $this->featureFlag->isSplitActive() ) {
131 $carrierConfigForWidget['vendors'] = $this->getWidgetVendorsParam(
132 $carrier->getId(),
133 $carrier->getCountry(),
134 ( ( $carrierOption && isset( $carrierOption['vendor_groups'] ) ) ? $carrierOption['vendor_groups'] : null )
135 );
136 } else {
137 $carrierConfigForWidget['carriers'] = $this->getCarriersParam( true, $carrier->getId() );
138 }
139 }
140
141 if ( ! $carrier->hasPickupPoints() ) {
142 $addressValidation = 'none';
143 if ( $carrierOption && in_array( $carrier->getCountry(), Entity\Carrier::ADDRESS_VALIDATION_COUNTRIES, true ) ) {
144 $addressValidation = ( $carrierOption['address_validation'] ?? $addressValidation );
145 }
146
147 $carrierConfigForWidget['address_validation'] = $addressValidation;
148 }
149
150 return $carrierConfigForWidget;
151 }
152
153 /**
154 * Creates pickup point widget options in backend.
155 *
156 * @param Order $order Order.
157 *
158 * @return array|null
159 */
160 public function createPickupPointForAdmin( Order $order ): array {
161 $widgetOptions = [
162 'country' => $order->getShippingCountry(),
163 'language' => substr( get_user_locale(), 0, 2 ),
164 'appIdentity' => Plugin::getAppIdentity(),
165 'weight' => $order->getFinalWeight(),
166 ];
167
168 if ( $this->featureFlag->isSplitActive() ) {
169 // In backend, we want all pickup points in that country for packeta carrier.
170 if ( $order->getCarrierId() !== Entity\Carrier::INTERNAL_PICKUP_POINTS_ID ) {
171 $widgetOptions['vendors'] = $this->getWidgetVendorsParam(
172 $order->getCarrierId(),
173 $order->getShippingCountry(),
174 null
175 );
176 }
177 } else {
178 $widgetOptions['carriers'] = $this->getCarriersParam( $order->isPickupPointDelivery(), $order->getCarrierId() );
179 }
180
181 if ( $order->containsAdultContent() ) {
182 $widgetOptions += [ 'livePickupPoint' => true ];
183 }
184
185 return $widgetOptions;
186 }
187
188 /**
189 * Creates address validation widget options in backend.
190 *
191 * @param Order $order Order.
192 *
193 * @return array
194 */
195 public function createAddressForAdmin( Order $order ): array {
196 /**
197 * Delivery address is always present in this case.
198 *
199 * @var Entity\Address $deliveryAddress
200 */
201 $deliveryAddress = $order->getDeliveryAddress();
202 $widgetOptions = [
203 'country' => $order->getShippingCountry(),
204 'language' => substr( get_user_locale(), 0, 2 ),
205 'layout' => 'hd',
206 'appIdentity' => Plugin::getAppIdentity(),
207 'street' => $deliveryAddress->getStreet(),
208 'city' => $deliveryAddress->getCity(),
209 'postcode' => $deliveryAddress->getZip(),
210 ];
211
212 if ( $deliveryAddress->getHouseNumber() ) {
213 $widgetOptions += [ 'houseNumber' => $deliveryAddress->getHouseNumber() ];
214 }
215
216 if ( $deliveryAddress->getCounty() ) {
217 $widgetOptions += [ 'county' => $deliveryAddress->getCounty() ];
218 }
219
220 // TODO: Redo in carrier refactor.
221 if ( $order->getCarrier() && is_numeric( $order->getCarrier()->getId() ) ) {
222 $widgetOptions += [ 'carrierId' => $order->getCarrier()->getId() ];
223 }
224
225 return $widgetOptions;
226 }
227
228 }
229