16
16
use Magento \Quote \Api \Data \AddressInterface ;
17
17
use Magento \Quote \Api \Data \EstimateAddressInterface ;
18
18
use Magento \Quote \Api \ShipmentEstimationInterface ;
19
+ use Magento \Quote \Model \ResourceModel \Quote \Address as QuoteAddressResource ;
19
20
20
21
/**
21
22
* Shipping method read service
@@ -63,6 +64,11 @@ class ShippingMethodManagement implements
63
64
*/
64
65
private $ addressFactory ;
65
66
67
+ /**
68
+ * @var QuoteAddressResource
69
+ */
70
+ private $ quoteAddressResource ;
71
+
66
72
/**
67
73
* Constructor
68
74
*
@@ -71,20 +77,24 @@ class ShippingMethodManagement implements
71
77
* @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
72
78
* @param Quote\TotalsCollector $totalsCollector
73
79
* @param AddressInterfaceFactory|null $addressFactory
80
+ * @param QuoteAddressResource|null $quoteAddressResource
74
81
*/
75
82
public function __construct (
76
83
\Magento \Quote \Api \CartRepositoryInterface $ quoteRepository ,
77
84
Cart \ShippingMethodConverter $ converter ,
78
85
\Magento \Customer \Api \AddressRepositoryInterface $ addressRepository ,
79
86
\Magento \Quote \Model \Quote \TotalsCollector $ totalsCollector ,
80
- AddressInterfaceFactory $ addressFactory = null
87
+ AddressInterfaceFactory $ addressFactory = null ,
88
+ QuoteAddressResource $ quoteAddressResource = null
81
89
) {
82
90
$ this ->quoteRepository = $ quoteRepository ;
83
91
$ this ->converter = $ converter ;
84
92
$ this ->addressRepository = $ addressRepository ;
85
93
$ this ->totalsCollector = $ totalsCollector ;
86
94
$ this ->addressFactory = $ addressFactory ?: ObjectManager::getInstance ()
87
95
->get (AddressInterfaceFactory::class);
96
+ $ this ->quoteAddressResource = $ quoteAddressResource ?: ObjectManager::getInstance ()
97
+ ->get (QuoteAddressResource::class);
88
98
}
89
99
90
100
/**
@@ -172,6 +182,8 @@ public function set($cartId, $carrierCode, $methodCode)
172
182
* @return void
173
183
* @throws InputException The shipping method is not valid for an empty cart.
174
184
* @throws NoSuchEntityException CThe Cart includes virtual product(s) only, so a shipping address is not used.
185
+ * @throws StateException The billing or shipping address is not set.
186
+ * @throws \Exception
175
187
*/
176
188
public function apply ($ cartId , $ carrierCode , $ methodCode )
177
189
{
@@ -189,7 +201,9 @@ public function apply($cartId, $carrierCode, $methodCode)
189
201
}
190
202
$ shippingAddress = $ quote ->getShippingAddress ();
191
203
if (!$ shippingAddress ->getCountryId ()) {
192
- return ;
204
+ // Remove empty quote address
205
+ $ this ->quoteAddressResource ->delete ($ shippingAddress );
206
+ throw new StateException (__ ('The shipping address is missing. Set the address and try again. ' ));
193
207
}
194
208
$ shippingAddress ->setShippingMethod ($ carrierCode . '_ ' . $ methodCode );
195
209
}
0 commit comments