@@ -18,6 +18,7 @@ class PreselectShipping
18
18
private AddressSetMockdata $ addressSetMockData ;
19
19
private AddressResetConditions $ addressReset ;
20
20
private ShippingAddressAssignment $ addressAssignment ;
21
+ private ?Quote $ quote ;
21
22
22
23
public function __construct (
23
24
ShippingMethodManagement $ methodManagement ,
@@ -36,44 +37,50 @@ public function __construct(
36
37
*/
37
38
public function afterGetQuote (Session $ subject , Quote $ result ): Quote
38
39
{
39
- if (!$ this ->isPreselectionAllowed ($ result )) {
40
- return $ result ;
40
+ $ this ->quote = $ result ;
41
+ if (!$ this ->isPreselectionAllowed ()) {
42
+ return $ this ->quote ;
41
43
}
42
- $ address = $ result ->getShippingAddress ();
43
- if ($ this ->shouldMockAddress ($ address )) {
44
- $ this ->addressSetMockData ->setMockDataOnAddress ($ address );
45
- $ this ->addressAssignment ->setAddress ($ result , $ address );
46
- }
47
- $ this ->preselectShippingMethod ($ result );
48
- return $ result ;
44
+ $ this ->preselectShippingAddress ();
45
+ $ this ->preselectShippingMethod ();
46
+ return $ this ->quote ;
49
47
}
50
48
51
49
public function shouldMockAddress (Address $ address ): bool
52
50
{
53
51
return (true !== $ address ->validate ());
54
52
}
53
+
54
+ public function preselectShippingAddress (): void
55
+ {
56
+ $ address = $ this ->quote ->getShippingAddress ();
57
+ if ($ this ->shouldMockAddress ($ address )) {
58
+ $ this ->addressSetMockData ->setMockDataOnAddress ($ address );
59
+ $ this ->addressAssignment ->setAddress ($ this ->quote , $ address );
60
+ }
61
+ }
55
62
56
- public function preselectShippingMethod (Quote $ quote ): void
63
+ public function preselectShippingMethod (): void
57
64
{
58
- $ quote ->getShippingAddress ()->requestShippingRates (); // load new rates
59
- if (!$ rate = $ this ->getCheapestShippingRate ($ quote )) {
65
+ $ this -> quote ->getShippingAddress ()->requestShippingRates (); // load new rates
66
+ if (!$ rate = $ this ->getCheapestShippingRate ()) {
60
67
return ;
61
68
}
62
69
try {
63
70
$ this ->methodManagement ->set (
64
- $ quote ->getId (),
71
+ $ this -> quote ->getId (),
65
72
$ rate ->getCarrier (),
66
73
$ rate ->getMethod ()
67
74
);
68
75
} catch (\Exception $ e ) {
69
- $ quote ->addErrorInfo ('error ' , null , $ e ->getCode (), $ e ->getMessage ());
76
+ $ this -> quote ->addErrorInfo ('error ' , null , $ e ->getCode (), $ e ->getMessage ());
70
77
}
71
78
}
72
79
73
- public function getCheapestShippingRate (Quote $ quote ): ?Rate
80
+ public function getCheapestShippingRate (): ?Rate
74
81
{
75
82
$ selectedRate = null ;
76
- foreach ($ this ->getShippingRates ($ quote ) as $ rate ) {
83
+ foreach ($ this ->getShippingRates () as $ rate ) {
77
84
/** @var Rate $rate */
78
85
if ($ selectedRate === null || $ rate ->getPrice () < $ selectedRate ->getPrice ()) {
79
86
$ selectedRate = $ rate ;
@@ -82,16 +89,16 @@ public function getCheapestShippingRate(Quote $quote): ?Rate
82
89
return $ selectedRate ;
83
90
}
84
91
85
- public function getShippingRates (Quote $ quote )
92
+ public function getShippingRates ()
86
93
{
87
- return $ quote ->getShippingAddress ()->getShippingRatesCollection ();
94
+ return $ this -> quote ->getShippingAddress ()->getShippingRatesCollection ();
88
95
}
89
96
90
- public function isPreselectionAllowed (Quote $ quote ): bool
97
+ public function isPreselectionAllowed (): bool
91
98
{
92
99
return $ this ->validateShippingResetConditions () &&
93
- $ this ->validateQuoteConditions ($ quote ) &&
94
- $ this ->validateShippingConditions ($ quote );
100
+ $ this ->validateQuoteConditions () &&
101
+ $ this ->validateShippingConditions ();
95
102
}
96
103
97
104
public function validateShippingResetConditions (): bool
@@ -100,14 +107,14 @@ public function validateShippingResetConditions(): bool
100
107
!$ this ->addressReset ->isAddressIgnoreRequest ();
101
108
}
102
109
103
- public function validateQuoteConditions (Quote $ quote ): bool
110
+ public function validateQuoteConditions (): bool
104
111
{
105
- return !$ quote ->getIsVirtual () && $ quote ->getItemsCount ();
112
+ return !$ this -> quote ->getIsVirtual () && $ this -> quote ->getItemsCount ();
106
113
}
107
114
108
- public function validateShippingConditions (Quote $ quote ): bool
115
+ public function validateShippingConditions (): bool
109
116
{
110
- $ address = $ quote ->getShippingAddress ();
117
+ $ address = $ this -> quote ->getShippingAddress ();
111
118
$ shippingIsFine = $ address ->validate () && !empty ($ address ->getShippingMethod ());
112
119
return !$ shippingIsFine ;
113
120
}
0 commit comments