PluginProbe
Packeta / 2.1
Packeta v2.1
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.1, at src/Packetery/Module/Checkout/CheckoutSettings.php

261 lines 8.5 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 'showLogo' => $this->optionsProvider->isCheckoutLogoShown(),
168 'country' => $this->checkoutService->getCustomerCountry() ?? '',
169 'weight' => $widgetWeight,
170 'carrierConfig' => $carriersConfigForWidget,
171 'isCarDeliverySampleEnabled' => $this->carDeliveryConfig->isSampleEnabled(),
172 'isAgeVerificationRequired' => $this->cartService->isAgeVerificationRequired(),
173 'biggestProductSize' => $this->cartService->getBiggestProductSize(),
174 'pickupPointAttrs' => Order\Attribute::$pickupPointAttributes,
175 'homeDeliveryAttrs' => Order\Attribute::$homeDeliveryAttributes,
176 'carDeliveryAttrs' => Order\Attribute::$carDeliveryAttributes,
177 'carDeliveryCarriers' => Entity\Carrier::CAR_DELIVERY_CARRIERS,
178 'expeditionDay' => $this->getCarDeliveryExpeditionDay(),
179 'appIdentity' => Plugin::getAppIdentity(),
180 'packeteryApiKey' => $this->optionsProvider->get_api_key(),
181 'widgetAutoOpen' => $this->optionsProvider->shouldWidgetOpenAutomatically(),
182 'saveSelectedPickupPointUrl' => $this->apiRouter->getSaveSelectedPickupPointUrl(),
183 'saveValidatedAddressUrl' => $this->apiRouter->getSaveValidatedAddressUrl(),
184 'saveCarDeliveryDetailsUrl' => $this->apiRouter->getSaveCarDeliveryDetailsUrl(),
185 'removeSavedDataUrl' => $this->apiRouter->getRemoveSavedDataUrl(),
186 'adminAjaxUrl' => $this->wpAdapter->adminUrl( 'admin-ajax.php' ),
187 'nonce' => (string) $this->wpAdapter->createNonce( 'wp_rest' ),
188 'savedData' => $this->storage->getFromTransient(),
189 'widgetUrl' => $this->widgetUrlResolver->getUrl(),
190 'translations' => [
191 'packeta' => $this->wpAdapter->__( 'Packeta', 'packeta' ),
192 'choosePickupPoint' => $this->wpAdapter->__( 'Choose pickup point', 'packeta' ),
193 'pickupPointNotChosen' => $this->wpAdapter->__( 'Pickup point is not chosen.', 'packeta' ),
194 'placeholderText' => $this->wpAdapter->__( 'Loading Packeta widget button...', 'packeta' ),
195 'chooseAddress' => $this->wpAdapter->__( 'Choose delivery address', 'packeta' ),
196 'addressValidationIsOutOfOrder' => $this->wpAdapter->__( 'Address validation is out of order', 'packeta' ),
197 'invalidAddressCountrySelected' => $this->wpAdapter->__( 'The selected country does not correspond to the destination country.', 'packeta' ),
198 'deliveryAddressNotification' => $this->wpAdapter->__( 'The order will be delivered to the address:', 'packeta' ),
199 'addressIsNotValidatedAndRequiredByCarrier' => $this->wpAdapter->__( 'Delivery address has not been chosen. Choosing a delivery address using the widget is required by this carrier.', 'packeta' ),
200 ],
201 ];
202 }
203
204 /**
205 * Used to provide additional settings for blocks checkout.
206 *
207 * @return void
208 */
209 public function actionCreateSettingsAjax(): void {
210 $settings = [];
211 if ( $this->wcAdapter->cart() instanceof WC_Cart ) {
212 $settings['biggestProductSize'] = $this->cartService->getBiggestProductSize();
213 $settings['isAgeVerificationRequired'] = $this->cartService->isAgeVerificationRequired();
214 }
215
216 $this->wpAdapter->sendJson( $settings );
217 }
218
219 /**
220 * Calculates and returns Expedition Day
221 *
222 * @return string|null
223 */
224 private function getCarDeliveryExpeditionDay(): ?string {
225 $chosenShippingMethod = $this->sessionService->getChosenMethodFromSession();
226 $carrierId = OptionPrefixer::removePrefix( $chosenShippingMethod );
227 if ( $this->carDeliveryConfig->isCarDeliveryCarrier( $carrierId ) === false ) {
228 return null;
229 }
230
231 $carrierOptions = $this->carrierOptionsFactory->createByOptionId( $chosenShippingMethod )->toArray();
232 $today = new DateTime();
233 $processingDays = $carrierOptions['days_until_shipping'];
234 $cutoffTime = $carrierOptions['shipping_time_cut_off'];
235
236 // Check if a cut-off time is provided and if the current time is after the cut-off time.
237 if ( $cutoffTime !== null ) {
238 $currentTime = $today->format( 'H:i' );
239 if ( $currentTime > $cutoffTime ) {
240 // If after cut-off time, move to the next day.
241 $today->modify( '+1 day' );
242 }
243 }
244
245 // Loop through each day to add processing days, skipping weekends.
246 for ( $i = 0; $i < $processingDays; $i++ ) {
247 // Add a day to the current date.
248 $today->modify( '+1 day' );
249
250 // Check if the current day is a weekend (Saturday or Sunday).
251 if ( $today->format( 'N' ) >= 6 ) {
252 // If it's a weekend, move to the next Monday.
253 $today->modify( 'next Monday' );
254 }
255 }
256
257 // Get the final expedition day.
258 return $today->format( 'Y-m-d' );
259 }
260 }
261