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

CheckoutSettings.php in Packeta 2.0.9, at src/Packetery/Module/Checkout/CheckoutSettings.php

260 lines 8.4 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 DateTime;
8 use Packetery\Core\Entity;
9 use Packetery\Module\Api;
10 use Packetery\Module\Carrier;
11 use Packetery\Module\Carrier\CarDeliveryConfig;
12 use Packetery\Module\Carrier\OptionPrefixer;
13 use Packetery\Module\Framework\WcAdapter;
14 use Packetery\Module\Framework\WpAdapter;
15 use Packetery\Module\Options\OptionsProvider;
16 use Packetery\Module\Order;
17 use Packetery\Module\Plugin;
18 use Packetery\Module\Views\UrlBuilder;
19 use Packetery\Module\WidgetOptionsBuilder;
20 use Packetery\Module\WidgetUrlResolver;
21 use WC_Cart;
22
23 class CheckoutSettings {
24
25 /**
26 * @var Carrier\EntityRepository
27 */
28 private $carrierEntityRepository;
29
30 /**
31 * @var WidgetOptionsBuilder
32 */
33 private $widgetOptionsBuilder;
34
35 /**
36 * @var UrlBuilder
37 */
38 private $urlBuilder;
39
40 /**
41 * @var CarDeliveryConfig
42 */
43 private $carDeliveryConfig;
44
45 /**
46 * @var OptionsProvider
47 */
48 private $optionsProvider;
49
50 /**
51 * @var Api\Internal\CheckoutRouter
52 */
53 private $apiRouter;
54
55 /**
56 * @var Carrier\CarrierOptionsFactory
57 */
58 private $carrierOptionsFactory;
59
60 /**
61 * @var CheckoutStorage
62 */
63 private $storage;
64
65 /**
66 * @var CheckoutService
67 */
68 private $checkoutService;
69
70 /**
71 * @var CartService
72 */
73 private $cartService;
74
75 /**
76 * @var SessionService
77 */
78 private $sessionService;
79
80 /**
81 * @var WpAdapter
82 */
83 private $wpAdapter;
84
85 /**
86 * @var WcAdapter
87 */
88 private $wcAdapter;
89
90 /**
91 * @var WidgetUrlResolver
92 */
93 private $widgetUrlResolver;
94
95 public function __construct(
96 Carrier\EntityRepository $carrierEntityRepository,
97 WidgetOptionsBuilder $widgetOptionsBuilder,
98 UrlBuilder $urlBuilder,
99 CarDeliveryConfig $carDeliveryConfig,
100 OptionsProvider $optionsProvider,
101 Api\Internal\CheckoutRouter $apiRouter,
102 Carrier\CarrierOptionsFactory $carrierOptionsFactory,
103 CheckoutStorage $storage,
104 CheckoutService $checkoutService,
105 CartService $cartService,
106 SessionService $sessionService,
107 WpAdapter $wpAdapter,
108 WcAdapter $wcAdapter,
109 WidgetUrlResolver $widgetUrlResolver
110 ) {
111
112 $this->carrierEntityRepository = $carrierEntityRepository;
113 $this->widgetOptionsBuilder = $widgetOptionsBuilder;
114 $this->urlBuilder = $urlBuilder;
115 $this->carDeliveryConfig = $carDeliveryConfig;
116 $this->optionsProvider = $optionsProvider;
117 $this->apiRouter = $apiRouter;
118 $this->carrierOptionsFactory = $carrierOptionsFactory;
119 $this->storage = $storage;
120 $this->checkoutService = $checkoutService;
121 $this->cartService = $cartService;
122 $this->sessionService = $sessionService;
123 $this->wpAdapter = $wpAdapter;
124 $this->wcAdapter = $wcAdapter;
125 $this->widgetUrlResolver = $widgetUrlResolver;
126 }
127
128 /**
129 * Creates settings for checkout script.
130 *
131 * @return array
132 */
133 public function createSettings(): array {
134 if ( ! $this->wcAdapter->cart() instanceof WC_Cart ) {
135 return [];
136 }
137
138 $carriersConfigForWidget = [];
139 $carriers = $this->carrierEntityRepository->getAllCarriersIncludingNonFeed();
140
141 foreach ( $carriers as $carrier ) {
142 $optionId = Carrier\OptionPrefixer::getOptionId( $carrier->getId() );
143
144 $carriersConfigForWidget[ $optionId ] = $this->widgetOptionsBuilder->getCarrierForCheckout(
145 $carrier,
146 $optionId
147 );
148 }
149
150 /**
151 * Filter widget weight in checkout.
152 *
153 * @since 1.6.3
154 */
155 $widgetWeight = (float) $this->wpAdapter->applyFilters( 'packeta_widget_weight', $this->cartService->getCartWeightKg() );
156
157 /**
158 * Filter widget language in checkout.
159 *
160 * @since 1.4.2
161 */
162 $language = (string) $this->wpAdapter->applyFilters( 'packeta_widget_language', substr( get_locale(), 0, 2 ) );
163
164 return [
165 'language' => $language,
166 'logo' => $this->urlBuilder->buildAssetUrl( 'public/images/packeta-symbol.png' ),
167 'country' => $this->checkoutService->getCustomerCountry() ?? '',
168 'weight' => $widgetWeight,
169 'carrierConfig' => $carriersConfigForWidget,
170 'isCarDeliverySampleEnabled' => $this->carDeliveryConfig->isSampleEnabled(),
171 'isAgeVerificationRequired' => $this->cartService->isAgeVerificationRequired(),
172 'biggestProductSize' => $this->cartService->getBiggestProductSize(),
173 'pickupPointAttrs' => Order\Attribute::$pickupPointAttributes,
174 'homeDeliveryAttrs' => Order\Attribute::$homeDeliveryAttributes,
175 'carDeliveryAttrs' => Order\Attribute::$carDeliveryAttributes,
176 'carDeliveryCarriers' => Entity\Carrier::CAR_DELIVERY_CARRIERS,
177 'expeditionDay' => $this->getCarDeliveryExpeditionDay(),
178 'appIdentity' => Plugin::getAppIdentity(),
179 'packeteryApiKey' => $this->optionsProvider->get_api_key(),
180 'widgetAutoOpen' => $this->optionsProvider->shouldWidgetOpenAutomatically(),
181 'saveSelectedPickupPointUrl' => $this->apiRouter->getSaveSelectedPickupPointUrl(),
182 'saveValidatedAddressUrl' => $this->apiRouter->getSaveValidatedAddressUrl(),
183 'saveCarDeliveryDetailsUrl' => $this->apiRouter->getSaveCarDeliveryDetailsUrl(),
184 'removeSavedDataUrl' => $this->apiRouter->getRemoveSavedDataUrl(),
185 'adminAjaxUrl' => $this->wpAdapter->adminUrl( 'admin-ajax.php' ),
186 'nonce' => (string) $this->wpAdapter->createNonce( 'wp_rest' ),
187 'savedData' => $this->storage->getFromTransient(),
188 'widgetUrl' => $this->widgetUrlResolver->getUrl(),
189 'translations' => [
190 'packeta' => $this->wpAdapter->__( 'Packeta', 'packeta' ),
191 'choosePickupPoint' => $this->wpAdapter->__( 'Choose pickup point', 'packeta' ),
192 'pickupPointNotChosen' => $this->wpAdapter->__( 'Pickup point is not chosen.', 'packeta' ),
193 'placeholderText' => $this->wpAdapter->__( 'Loading Packeta widget button...', 'packeta' ),
194 'chooseAddress' => $this->wpAdapter->__( 'Choose delivery address', 'packeta' ),
195 'addressValidationIsOutOfOrder' => $this->wpAdapter->__( 'Address validation is out of order', 'packeta' ),
196 'invalidAddressCountrySelected' => $this->wpAdapter->__( 'The selected country does not correspond to the destination country.', 'packeta' ),
197 'deliveryAddressNotification' => $this->wpAdapter->__( 'The order will be delivered to the address:', 'packeta' ),
198 'addressIsNotValidatedAndRequiredByCarrier' => $this->wpAdapter->__( 'Delivery address has not been chosen. Choosing a delivery address using the widget is required by this carrier.', 'packeta' ),
199 ],
200 ];
201 }
202
203 /**
204 * Used to provide additional settings for blocks checkout.
205 *
206 * @return void
207 */
208 public function actionCreateSettingsAjax(): void {
209 $settings = [];
210 if ( $this->wcAdapter->cart() instanceof WC_Cart ) {
211 $settings['biggestProductSize'] = $this->cartService->getBiggestProductSize();
212 $settings['isAgeVerificationRequired'] = $this->cartService->isAgeVerificationRequired();
213 }
214
215 $this->wpAdapter->sendJson( $settings );
216 }
217
218 /**
219 * Calculates and returns Expedition Day
220 *
221 * @return string|null
222 */
223 private function getCarDeliveryExpeditionDay(): ?string {
224 $chosenShippingMethod = $this->sessionService->getChosenMethodFromSession();
225 $carrierId = OptionPrefixer::removePrefix( $chosenShippingMethod );
226 if ( $this->carDeliveryConfig->isCarDeliveryCarrier( $carrierId ) === false ) {
227 return null;
228 }
229
230 $carrierOptions = $this->carrierOptionsFactory->createByOptionId( $chosenShippingMethod )->toArray();
231 $today = new DateTime();
232 $processingDays = $carrierOptions['days_until_shipping'];
233 $cutoffTime = $carrierOptions['shipping_time_cut_off'];
234
235 // Check if a cut-off time is provided and if the current time is after the cut-off time.
236 if ( $cutoffTime !== null ) {
237 $currentTime = $today->format( 'H:i' );
238 if ( $currentTime > $cutoffTime ) {
239 // If after cut-off time, move to the next day.
240 $today->modify( '+1 day' );
241 }
242 }
243
244 // Loop through each day to add processing days, skipping weekends.
245 for ( $i = 0; $i < $processingDays; $i++ ) {
246 // Add a day to the current date.
247 $today->modify( '+1 day' );
248
249 // Check if the current day is a weekend (Saturday or Sunday).
250 if ( $today->format( 'N' ) >= 6 ) {
251 // If it's a weekend, move to the next Monday.
252 $today->modify( 'next Monday' );
253 }
254 }
255
256 // Get the final expedition day.
257 return $today->format( 'Y-m-d' );
258 }
259 }
260