7
7
8
8
namespace Magento \GraphQl \Quote \Guest ;
9
9
10
+ use Magento \Quote \Model \Quote ;
10
11
use Magento \Quote \Model \QuoteFactory ;
11
12
use Magento \Quote \Model \QuoteIdToMaskedQuoteIdInterface ;
12
13
use Magento \Quote \Model \ResourceModel \Quote as QuoteResource ;
@@ -109,6 +110,34 @@ public function testSetMultipleShippingMethods()
109
110
$ this ->markTestIncomplete ('https://github.com/magento/graphql-ce/issues/422 ' );
110
111
}
111
112
113
+ /**
114
+ * @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php
115
+ */
116
+ public function testReSetShippingMethod ()
117
+ {
118
+ $ maskedQuoteId = $ this ->unAssignCustomerFromQuoteAndShippingAddress ('test_order_1 ' );
119
+ $ methodCode = 'flatrate ' ;
120
+ $ carrierCode = 'flatrate ' ;
121
+ $ quote = $ this ->getQuoteByReversedQuoteId ('test_order_1 ' );
122
+ $ shippingAddress = $ quote ->getShippingAddress ();
123
+ $ shippingAddressId = $ shippingAddress ->getId ();
124
+ $ query = $ this ->prepareMutationQuery ($ maskedQuoteId , $ methodCode , $ carrierCode , $ shippingAddressId );
125
+
126
+ $ response = $ this ->graphQlQuery ($ query );
127
+
128
+ self ::assertArrayHasKey ('setShippingMethodsOnCart ' , $ response );
129
+ self ::assertArrayHasKey ('cart ' , $ response ['setShippingMethodsOnCart ' ]);
130
+ self ::assertArrayHasKey ('shipping_addresses ' , $ response ['setShippingMethodsOnCart ' ]['cart ' ]);
131
+ foreach ($ response ['setShippingMethodsOnCart ' ]['cart ' ]['shipping_addresses ' ] as $ address ) {
132
+ self ::assertArrayHasKey ('address_id ' , $ address );
133
+ if ($ address ['address_id ' ] == $ shippingAddressId ) {
134
+ self ::assertArrayHasKey ('selected_shipping_method ' , $ address );
135
+ self ::assertEquals ($ methodCode , $ address ['selected_shipping_method ' ]['method_code ' ]);
136
+ self ::assertEquals ($ carrierCode , $ address ['selected_shipping_method ' ]['carrier_code ' ]);
137
+ }
138
+ }
139
+ }
140
+
112
141
/**
113
142
* @param string $maskedQuoteId
114
143
* @param string $shippingMethodCode
@@ -128,16 +157,15 @@ private function prepareMutationQuery(
128
157
setShippingMethodsOnCart(input:
129
158
{
130
159
cart_id: " $ maskedQuoteId",
131
- shipping_addresses : [{
160
+ shipping_methods : [{
132
161
cart_address_id: $ shippingAddressId
133
- shipping_method: {
134
- method_code: " $ shippingMethodCode"
135
- carrier_code: " $ shippingCarrierCode"
136
- }
162
+ method_code: " $ shippingMethodCode"
163
+ carrier_code: " $ shippingCarrierCode"
137
164
}]
138
165
}) {
139
166
cart {
140
167
shipping_addresses {
168
+ address_id
141
169
selected_shipping_method {
142
170
carrier_code
143
171
method_code
@@ -152,6 +180,40 @@ private function prepareMutationQuery(
152
180
QUERY ;
153
181
}
154
182
183
+ /**
184
+ * @param string $reversedQuoteId
185
+ * @param int $customerId
186
+ * @return string
187
+ */
188
+ private function unAssignCustomerFromQuoteAndShippingAddress (
189
+ string $ reversedQuoteId
190
+ ): string {
191
+ /** @var Quote $quote */
192
+ $ quote = $ this ->quoteFactory ->create ();
193
+ $ this ->quoteResource ->load ($ quote , $ reversedQuoteId , 'reserved_order_id ' );
194
+ $ quote ->setCustomerId (0 );
195
+
196
+ $ shippingAddress = $ quote ->getShippingAddress ();
197
+ $ shippingAddress ->setCustomerId (0 );
198
+ $ shippingAddress ->setCustomerAddressId (0 );
199
+
200
+ $ this ->quoteResource ->save ($ quote );
201
+
202
+ return $ this ->quoteIdToMaskedId ->execute ((int )$ quote ->getId ());
203
+ }
204
+
205
+ /**
206
+ * @param string $reversedQuoteId
207
+ * @return Quote
208
+ */
209
+ private function getQuoteByReversedQuoteId (string $ reversedQuoteId ): Quote
210
+ {
211
+ $ quote = $ this ->quoteFactory ->create ();
212
+ $ this ->quoteResource ->load ($ quote , $ reversedQuoteId , 'reserved_order_id ' );
213
+
214
+ return $ quote ;
215
+ }
216
+
155
217
/**
156
218
* @param string $reversedQuoteId
157
219
* @return string
0 commit comments