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

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

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