Skip to content

Commit 163a788

Browse files
committed
Merge branch 'WP-192' into 'main'
fix: check CDEK shipping on restore fields See merge request ecommerce_modules/cms/wordpress/wordpress!81
2 parents 1316682 + 4b062b2 commit 163a788

File tree

4 files changed

+73
-19
lines changed

4 files changed

+73
-19
lines changed

src/Helpers/CheckoutHelper.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,15 @@ public static function getCurrentValue(string $valueName, string $defaultValue =
6868

6969
public static function restoreFields(array $fields): array
7070
{
71-
if (!empty(WC()->cart) && !WC()->cart->needs_shipping()) {
71+
$shippingDetector = ShippingDetector::new();
72+
73+
if (
74+
!$shippingDetector->needShipping() ||
75+
(
76+
!$shippingDetector->isShippingEmpty() &&
77+
$shippingDetector->getShipping() === null
78+
)
79+
) {
7280
return $fields;
7381
}
7482

src/Helpers/ShippingDetector.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace {
6+
defined('ABSPATH') or exit;
7+
}
8+
9+
namespace Cdek\Helpers {
10+
11+
use Cdek\Config;
12+
use Cdek\Traits\CanBeCreated;
13+
14+
class ShippingDetector
15+
{
16+
use CanBeCreated;
17+
private ?array $shippingMethods = null;
18+
19+
public function __construct()
20+
{
21+
if(WC()->cart === false || WC()->cart->needs_shipping() === false) {
22+
return;
23+
}
24+
25+
$this->shippingMethods = WC()->session->get('chosen_shipping_methods', []);
26+
}
27+
28+
public function needShipping(): bool
29+
{
30+
return $this->shippingMethods !== null;
31+
}
32+
33+
public function getShipping(): ?string
34+
{
35+
if($this->isShippingEmpty()){
36+
return null;
37+
}
38+
39+
$shippingMethodIdSelected = $this->shippingMethods[0];
40+
41+
if ( strpos($shippingMethodIdSelected, Config::DELIVERY_NAME) === false ) {
42+
return null;
43+
}
44+
45+
return $shippingMethodIdSelected;
46+
}
47+
48+
public function isShippingEmpty(): bool
49+
{
50+
return empty($this->shippingMethods[0]);
51+
}
52+
}
53+
}

src/UI/CheckoutMap.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Cdek\CdekApi;
1313
use Cdek\Config;
1414
use Cdek\Helpers\CheckoutHelper;
15+
use Cdek\Helpers\ShippingDetector;
1516
use Cdek\Model\Tariff;
1617
use Throwable;
1718

@@ -66,14 +67,16 @@ private function isTariffDestinationCdekOffice($shippingMethodCurrent): bool
6667
return false;
6768
}
6869

69-
$shippingMethodIdSelected = WC()->session->get('chosen_shipping_methods', []);
70+
$shippingMethodIdSelected = ShippingDetector::new()->getShipping();
7071

71-
if (empty($shippingMethodIdSelected[0]) ||
72-
$shippingMethodCurrent->get_id() !== $shippingMethodIdSelected[0]) {
72+
if (
73+
empty($shippingMethodIdSelected) ||
74+
$shippingMethodCurrent->get_id() !== $shippingMethodIdSelected
75+
) {
7376
return false;
7477
}
7578

76-
$tariffCode = explode(':', $shippingMethodIdSelected[0])[1];
79+
$tariffCode = explode(':', $shippingMethodIdSelected)[1];
7780

7881
return Tariff::isToOffice((int)$tariffCode);
7982
}

src/Validator/CheckoutValidator.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
namespace Cdek\Validator {
1111

1212
use Cdek\CdekApi;
13-
use Cdek\Config;
1413
use Cdek\Exceptions\CacheException;
1514
use Cdek\Exceptions\External\ApiException;
1615
use Cdek\Exceptions\External\CoreAuthException;
1716
use Cdek\Helpers\CheckoutHelper;
17+
use Cdek\Helpers\ShippingDetector;
1818
use Cdek\Model\Tariff;
1919
use Throwable;
2020

@@ -23,23 +23,13 @@ class CheckoutValidator
2323

2424
public function __invoke(): void
2525
{
26-
if ( !WC()->cart->needs_shipping() ) {
27-
return;
28-
}
29-
30-
$shippingMethods = WC()->session->get('chosen_shipping_methods');
31-
32-
if ( empty($shippingMethods[0]) ) {
33-
return;
34-
}
35-
36-
$shippingMethodIdSelected = $shippingMethods[0];
26+
$shippingDetector = ShippingDetector::new();
3727

38-
if ( strpos($shippingMethodIdSelected, Config::DELIVERY_NAME) === false ) {
28+
if( $shippingDetector->getShipping() === null ) {
3929
return;
4030
}
4131

42-
$tariffCode = explode(':', $shippingMethodIdSelected)[1];
32+
$tariffCode = explode(':', $shippingDetector->getShipping())[1];
4333

4434
if ( Tariff::isToOffice((int)$tariffCode) ) {
4535
if ( empty(CheckoutHelper::getCurrentValue('office_code')) ) {

0 commit comments

Comments
 (0)