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

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

276 lines 6.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 Packetery\Module\Carrier;
8 use Packetery\Module\Carrier\CarDeliveryConfig;
9 use Packetery\Module\Carrier\PacketaPickupPointsConfig;
10 use Packetery\Module\Framework\WcAdapter;
11 use Packetery\Module\Options\OptionsProvider;
12 use Packetery\Module\Shipping\BaseShippingMethod;
13 use Packetery\Module\ShippingMethod;
14 use Packetery\Nette\Http\Request;
15 use WC_Shipping_Rate;
16
17 class CheckoutService {
18
19 /**
20 * @var WcAdapter
21 */
22 private $wcAdapter;
23
24 /**
25 * @var Request
26 */
27 private $httpRequest;
28
29 /**
30 * @var CarDeliveryConfig
31 */
32 private $carDeliveryConfig;
33
34 /**
35 * @var Carrier\Repository
36 */
37 private $carrierRepository;
38
39 /**
40 * @var Carrier\EntityRepository
41 */
42 private $carrierEntityRepository;
43
44 /**
45 * @var PacketaPickupPointsConfig
46 */
47 private $pickupPointsConfig;
48
49 /**
50 * @var OptionsProvider
51 */
52 private $optionsProvider;
53
54 public function __construct(
55 WcAdapter $wcAdapter,
56 Request $httpRequest,
57 CarDeliveryConfig $carDeliveryConfig,
58 Carrier\Repository $carrierRepository,
59 Carrier\EntityRepository $carrierEntityRepository,
60 PacketaPickupPointsConfig $pickupPointsConfig,
61 OptionsProvider $optionsProvider
62 ) {
63 $this->wcAdapter = $wcAdapter;
64 $this->httpRequest = $httpRequest;
65 $this->carDeliveryConfig = $carDeliveryConfig;
66 $this->carrierRepository = $carrierRepository;
67 $this->carrierEntityRepository = $carrierEntityRepository;
68 $this->pickupPointsConfig = $pickupPointsConfig;
69 $this->optionsProvider = $optionsProvider;
70 }
71
72 /**
73 * Calculates shipping without using POST data and returns id of chosen shipping rate.
74 *
75 * @return string|null
76 */
77 public function calculateShippingAndGetOptionId(): ?string {
78 $chosenShippingRates = $this->wcAdapter->cartCalculateShipping();
79 $chosenShippingRate = array_shift( $chosenShippingRates );
80
81 if ( $chosenShippingRate instanceof WC_Shipping_Rate ) {
82 return $this->getShippingMethodOptionId( $chosenShippingRate->get_id() );
83 }
84
85 return null;
86 }
87
88 /**
89 * Get chosen shipping rate id.
90 *
91 * @return string|null
92 */
93 public function resolveChosenMethod(): ?string {
94 $postedShippingMethodArray = $this->httpRequest->getPost( 'shipping_method' );
95
96 if ( $postedShippingMethodArray !== null ) {
97 return $this->getShippingMethodOptionId( current( $postedShippingMethodArray ) );
98 }
99
100 return $this->calculateShippingAndGetOptionId();
101 }
102
103 /**
104 * Gets ShippingRate's ID of extended id.
105 *
106 * @param string $chosenMethod Chosen shipping method.
107 *
108 * @return string
109 */
110 public function getShippingMethodOptionId( string $chosenMethod ): string {
111 if ( strpos( $chosenMethod, BaseShippingMethod::PACKETA_METHOD_PREFIX ) === 0 ) {
112 [ $methodId, $instanceId ] = explode( ':', $chosenMethod );
113
114 return Carrier\OptionPrefixer::getOptionId( str_replace( BaseShippingMethod::PACKETA_METHOD_PREFIX, '', $methodId ) );
115 }
116
117 return str_replace( ShippingMethod::PACKETERY_METHOD_ID . ':', '', $chosenMethod );
118 }
119
120 /**
121 * Checks if chosen shipping method is one of packetery.
122 *
123 * @param string $chosenMethod Chosen shipping method.
124 *
125 * @return bool
126 */
127 public function isPacketeryShippingMethod( string $chosenMethod ): bool {
128 if ( strpos( $chosenMethod, ':' ) !== false ) {
129 $optionId = $this->getShippingMethodOptionId( $chosenMethod );
130 } else {
131 $optionId = $chosenMethod;
132 }
133
134 return Carrier\OptionPrefixer::isOptionId( $optionId );
135 }
136
137 /**
138 * Gets feed ID or artificially created ID for internal purposes.
139 *
140 * @param string $chosenMethod Chosen shipping method.
141 *
142 * @return string|null
143 */
144 public function getCarrierIdFromShippingMethod( string $chosenMethod ): ?string {
145 if ( ! $this->isPacketeryShippingMethod( $chosenMethod ) ) {
146 return null;
147 }
148
149 $optionId = $this->getShippingMethodOptionId( $chosenMethod );
150
151 return Carrier\OptionPrefixer::removePrefix( $optionId );
152 }
153
154 public function getCarrierIdFromPacketeryShippingMethod( string $chosenMethod ): string {
155 $optionId = $this->getShippingMethodOptionId( $chosenMethod );
156
157 return Carrier\OptionPrefixer::removePrefix( $optionId );
158 }
159
160 /**
161 * Check if chosen shipping rate is bound with Packeta pickup points
162 *
163 * @return bool
164 */
165 public function isPickupPointOrder(): bool {
166 $chosenMethod = $this->resolveChosenMethod();
167 if ( $chosenMethod === null ) {
168 return false;
169 }
170 $carrierId = $this->getCarrierIdFromShippingMethod( $chosenMethod );
171
172 return $carrierId !== null && $this->isPickupPointCarrier( $carrierId );
173 }
174
175 /**
176 * Checks if chosen carrier has pickup points and sets carrier id in provided array.
177 *
178 * @param string $carrierId Carrier id.
179 *
180 * @return bool
181 */
182 private function isPickupPointCarrier( string $carrierId ): bool {
183 if ( $this->pickupPointsConfig->isInternalPickupPointCarrier( $carrierId ) ) {
184 return true;
185 }
186
187 return $this->carrierRepository->hasPickupPoints( (int) $carrierId );
188 }
189
190 /**
191 * Check if chosen shipping rate is bound with Packeta home delivery
192 *
193 * @return bool
194 */
195 public function isHomeDeliveryOrder(): bool {
196 $chosenMethod = $this->resolveChosenMethod();
197 if ( $chosenMethod === null ) {
198 return false;
199 }
200 $carrierId = $this->getCarrierIdFromShippingMethod( $chosenMethod );
201
202 return $carrierId !== null && $this->carrierEntityRepository->isHomeDeliveryCarrier( $carrierId );
203 }
204
205 /**
206 * Check if chosen shipping rate is bound with Packeta car delivery
207 *
208 * @return bool
209 */
210 public function isCarDeliveryOrder(): bool {
211 $chosenMethod = $this->resolveChosenMethod();
212 if ( $chosenMethod === null ) {
213 return false;
214 }
215 $carrierId = $this->getCarrierIdFromShippingMethod( $chosenMethod );
216
217 return $carrierId !== null && $this->carDeliveryConfig->isCarDeliveryCarrier( $carrierId );
218 }
219
220 /**
221 * Gets customer country from WC cart.
222 *
223 * @return string|null
224 */
225 public function getCustomerCountry(): ?string {
226 $shippingCountry = $this->wcAdapter->customerGetShippingCountry();
227 if ( $shippingCountry !== null ) {
228 return strtolower( $shippingCountry );
229 }
230
231 $billingCountry = $this->wcAdapter->customerGetBillingCountry();
232 if ( $billingCountry !== null ) {
233 return strtolower( $billingCountry );
234 }
235
236 return null;
237 }
238
239 /**
240 * Checks if Blocks are used in checkout.
241 *
242 * @return bool
243 */
244 public function areBlocksUsedInCheckout(): bool {
245 $checkoutDetection = $this->optionsProvider->getCheckoutDetection();
246
247 if ( $checkoutDetection === OptionsProvider::BLOCK_CHECKOUT_DETECTION ) {
248 return true;
249 }
250
251 if ( $checkoutDetection === OptionsProvider::CLASSIC_CHECKOUT_DETECTION ) {
252 return false;
253 }
254
255 if (
256 $this->wcAdapter->hasBlockInPage(
257 $this->wcAdapter->getPageId( 'checkout' ),
258 'woocommerce/checkout'
259 ) ) {
260 return true;
261 }
262
263 return false;
264 }
265
266 /**
267 * @return string|null|int
268 */
269 public function getOrderPayParameter() {
270 global $wp;
271
272 // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
273 return $wp->query_vars['order-pay'] ?? null;
274 }
275 }
276