@@ -18,6 +18,7 @@ class PreselectShipping
1818 private AddressSetMockdata $ addressSetMockData ;
1919 private AddressResetConditions $ addressReset ;
2020 private ShippingAddressAssignment $ addressAssignment ;
21+ private ?Quote $ quote ;
2122
2223 public function __construct (
2324 ShippingMethodManagement $ methodManagement ,
@@ -36,44 +37,50 @@ public function __construct(
3637 */
3738 public function afterGetQuote (Session $ subject , Quote $ result ): Quote
3839 {
39- if (!$ this ->isPreselectionAllowed ($ result )) {
40- return $ result ;
40+ $ this ->quote = $ result ;
41+ if (!$ this ->isPreselectionAllowed ()) {
42+ return $ this ->quote ;
4143 }
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 ;
4947 }
5048
5149 public function shouldMockAddress (Address $ address ): bool
5250 {
5351 return (true !== $ address ->validate ());
5452 }
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+ }
5562
56- public function preselectShippingMethod (Quote $ quote ): void
63+ public function preselectShippingMethod (): void
5764 {
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 ()) {
6067 return ;
6168 }
6269 try {
6370 $ this ->methodManagement ->set (
64- $ quote ->getId (),
71+ $ this -> quote ->getId (),
6572 $ rate ->getCarrier (),
6673 $ rate ->getMethod ()
6774 );
6875 } catch (\Exception $ e ) {
69- $ quote ->addErrorInfo ('error ' , null , $ e ->getCode (), $ e ->getMessage ());
76+ $ this -> quote ->addErrorInfo ('error ' , null , $ e ->getCode (), $ e ->getMessage ());
7077 }
7178 }
7279
73- public function getCheapestShippingRate (Quote $ quote ): ?Rate
80+ public function getCheapestShippingRate (): ?Rate
7481 {
7582 $ selectedRate = null ;
76- foreach ($ this ->getShippingRates ($ quote ) as $ rate ) {
83+ foreach ($ this ->getShippingRates () as $ rate ) {
7784 /** @var Rate $rate */
7885 if ($ selectedRate === null || $ rate ->getPrice () < $ selectedRate ->getPrice ()) {
7986 $ selectedRate = $ rate ;
@@ -82,16 +89,16 @@ public function getCheapestShippingRate(Quote $quote): ?Rate
8289 return $ selectedRate ;
8390 }
8491
85- public function getShippingRates (Quote $ quote )
92+ public function getShippingRates ()
8693 {
87- return $ quote ->getShippingAddress ()->getShippingRatesCollection ();
94+ return $ this -> quote ->getShippingAddress ()->getShippingRatesCollection ();
8895 }
8996
90- public function isPreselectionAllowed (Quote $ quote ): bool
97+ public function isPreselectionAllowed (): bool
9198 {
9299 return $ this ->validateShippingResetConditions () &&
93- $ this ->validateQuoteConditions ($ quote ) &&
94- $ this ->validateShippingConditions ($ quote );
100+ $ this ->validateQuoteConditions () &&
101+ $ this ->validateShippingConditions ();
95102 }
96103
97104 public function validateShippingResetConditions (): bool
@@ -100,14 +107,14 @@ public function validateShippingResetConditions(): bool
100107 !$ this ->addressReset ->isAddressIgnoreRequest ();
101108 }
102109
103- public function validateQuoteConditions (Quote $ quote ): bool
110+ public function validateQuoteConditions (): bool
104111 {
105- return !$ quote ->getIsVirtual () && $ quote ->getItemsCount ();
112+ return !$ this -> quote ->getIsVirtual () && $ this -> quote ->getItemsCount ();
106113 }
107114
108- public function validateShippingConditions (Quote $ quote ): bool
115+ public function validateShippingConditions (): bool
109116 {
110- $ address = $ quote ->getShippingAddress ();
117+ $ address = $ this -> quote ->getShippingAddress ();
111118 $ shippingIsFine = $ address ->validate () && !empty ($ address ->getShippingMethod ());
112119 return !$ shippingIsFine ;
113120 }
0 commit comments