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 / WidgetOptionsBuilder.php

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

180 lines 4.9 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;
6
7 use Packetery\Core\Entity;
8 use Packetery\Core\Entity\Order;
9 use Packetery\Module\Carrier\PacketaPickupPointsConfig;
10 use Packetery\Module\Exception\EmptyVendorsException;
11 use Packetery\Module\Framework\WpAdapter;
12
13 class WidgetOptionsBuilder {
14
15 /**
16 * Internal pickup points config.
17 *
18 * @var PacketaPickupPointsConfig
19 */
20 private $pickupPointsConfig;
21
22 /**
23 * @var WpAdapter
24 */
25 private $wpAdapter;
26
27 public function __construct(
28 PacketaPickupPointsConfig $pickupPointsConfig,
29 WpAdapter $wpAdapter
30 ) {
31 $this->pickupPointsConfig = $pickupPointsConfig;
32 $this->wpAdapter = $wpAdapter;
33 }
34
35 /**
36 * Gets widget vendors param.
37 *
38 * @param string $carrierId Carrier id.
39 * @param string $country Country.
40 * @param array|null $vendorGroups Vendor groups when checked in compound carrier settings.
41 *
42 * @return array
43 * @throws EmptyVendorsException
44 */
45 private function getWidgetVendorsParam( string $carrierId, string $country, ?array $vendorGroups ): array {
46 if ( is_numeric( $carrierId ) ) {
47 return [
48 [
49 'carrierId' => $carrierId,
50 'selected' => true,
51 ],
52 ];
53 }
54
55 $vendorGroups = $this->pickupPointsConfig->getFinalVendorGroups( $vendorGroups, $carrierId );
56 if ( $vendorGroups === null ) {
57 throw new EmptyVendorsException( 'Empty vendor groups for internal carrier.' );
58 }
59
60 $vendorsParam = [];
61 foreach ( $vendorGroups as $code ) {
62 $groupSettings = [
63 'selected' => true,
64 'country' => $country,
65 ];
66 if ( $code !== Entity\Carrier::VENDOR_GROUP_ZPOINT ) {
67 $groupSettings['group'] = $code;
68 }
69 $vendorsParam[] = $groupSettings;
70 }
71
72 return $vendorsParam;
73 }
74
75 /**
76 * Gets carrier configuration for widgets in frontend.
77 *
78 * @param Entity\Carrier $carrier Carrier configuration.
79 * @param string $optionId Option id.
80 *
81 * @return array
82 */
83 public function getCarrierForCheckout( Entity\Carrier $carrier, string $optionId ): array {
84 $carrierConfigForWidget = [
85 'id' => $carrier->getId(),
86 'is_pickup_points' => (int) $carrier->hasPickupPoints(),
87 ];
88
89 $carrierOption = $this->wpAdapter->getOption( $optionId );
90 if ( $carrier->hasPickupPoints() ) {
91 $carrierConfigForWidget['vendors'] = $this->getWidgetVendorsParam(
92 $carrier->getId(),
93 $carrier->getCountry(),
94 ( ( ( $carrierOption !== null && $carrierOption !== false ) && isset( $carrierOption['vendor_groups'] ) ) ? $carrierOption['vendor_groups'] : null )
95 );
96 }
97
98 if ( ! $carrier->hasPickupPoints() ) {
99 $addressValidation = 'none';
100 if ( ( $carrierOption !== null && $carrierOption !== false ) && in_array( $carrier->getCountry(), Entity\Carrier::ADDRESS_VALIDATION_COUNTRIES, true ) ) {
101 $addressValidation = ( $carrierOption['address_validation'] ?? $addressValidation );
102 }
103
104 $carrierConfigForWidget['address_validation'] = $addressValidation;
105 }
106
107 return $carrierConfigForWidget;
108 }
109
110 /**
111 * Creates pickup point widget options in backend.
112 *
113 * @param Order $order Order.
114 *
115 * @return array
116 */
117 public function createPickupPointForAdmin( Order $order ): array {
118 $widgetOptions = [
119 'country' => $order->getShippingCountry(),
120 'language' => substr( $this->wpAdapter->getUserLocale(), 0, 2 ),
121 'appIdentity' => Plugin::getAppIdentity(),
122 'weight' => $order->getFinalWeight(),
123 ];
124
125 // In backend, we want all pickup points in that country for Packeta carrier.
126 if ( $order->getCarrier()->getId() !== Entity\Carrier::INTERNAL_PICKUP_POINTS_ID ) {
127 $widgetOptions['vendors'] = $this->getWidgetVendorsParam(
128 $order->getCarrier()->getId(),
129 $order->getShippingCountry(),
130 null
131 );
132 }
133
134 if ( $order->containsAdultContent() === true ) {
135 $widgetOptions += [ 'livePickupPoint' => true ];
136 }
137
138 return $widgetOptions;
139 }
140
141 /**
142 * Creates address validation widget options in backend.
143 *
144 * @param Order $order Order.
145 *
146 * @return array
147 */
148 public function createAddressForAdmin( Order $order ): array {
149 /**
150 * Delivery address is always present in this case.
151 *
152 * @var Entity\Address $deliveryAddress
153 */
154 $deliveryAddress = $order->getDeliveryAddress();
155 $widgetOptions = [
156 'country' => $order->getShippingCountry(),
157 'language' => substr( $this->wpAdapter->getUserLocale(), 0, 2 ),
158 'layout' => 'hd',
159 'appIdentity' => Plugin::getAppIdentity(),
160 'street' => $deliveryAddress->getStreet(),
161 'city' => $deliveryAddress->getCity(),
162 'postcode' => $deliveryAddress->getZip(),
163 ];
164
165 if ( $deliveryAddress->getHouseNumber() !== null ) {
166 $widgetOptions += [ 'houseNumber' => $deliveryAddress->getHouseNumber() ];
167 }
168
169 if ( $deliveryAddress->getCounty() !== null ) {
170 $widgetOptions += [ 'county' => $deliveryAddress->getCounty() ];
171 }
172
173 if ( is_numeric( $order->getCarrier()->getId() ) ) {
174 $widgetOptions += [ 'carrierId' => $order->getCarrier()->getId() ];
175 }
176
177 return $widgetOptions;
178 }
179 }
180