5
5
*/
6
6
declare (strict_types=1 );
7
7
8
- namespace Magento \QuoteGraphQl \Model \Resolver \ ShippingMethod ;
8
+ namespace Magento \QuoteGraphQl \Model \Cart ;
9
9
10
- use Magento \Checkout \Api \ShippingInformationManagementInterface ;
11
- use Magento \Checkout \Model \ShippingInformation ;
12
10
use Magento \Framework \Exception \InputException ;
13
11
use Magento \Framework \Exception \NoSuchEntityException ;
14
12
use Magento \Framework \Exception \StateException ;
15
13
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
16
14
use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
17
- use Magento \Framework \GraphQl \Query \ResolverInterface ;
18
- use Magento \Framework \GraphQl \Config \Element \Field ;
19
- use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
20
- use Magento \Framework \Stdlib \ArrayManager ;
15
+ use Magento \Quote \Model \Quote ;
21
16
use Magento \Quote \Model \Quote \AddressFactory as QuoteAddressFactory ;
22
17
use Magento \Quote \Model \ResourceModel \Quote \Address as QuoteAddressResource ;
23
18
use Magento \Checkout \Model \ShippingInformationFactory ;
24
- use Magento \QuoteGraphQl \Model \Cart \GetCartForUser ;
19
+ use Magento \Checkout \Api \ShippingInformationManagementInterface ;
20
+ use Magento \Checkout \Model \ShippingInformation ;
25
21
26
22
/**
27
23
* Class SetShippingMethodsOnCart
28
24
*
29
- * Mutation resolver for setting shipping methods for shopping cart
25
+ * Set shipping method for a specified shopping cart address
30
26
*/
31
- class SetShippingMethodsOnCart implements ResolverInterface
27
+ class SetShippingMethodOnCart
32
28
{
33
29
/**
34
30
* @var ShippingInformationFactory
@@ -45,87 +41,52 @@ class SetShippingMethodsOnCart implements ResolverInterface
45
41
*/
46
42
private $ quoteAddressResource ;
47
43
48
- /**
49
- * @var ArrayManager
50
- */
51
- private $ arrayManager ;
52
-
53
- /**
54
- * @var GetCartForUser
55
- */
56
- private $ getCartForUser ;
57
-
58
44
/**
59
45
* @var ShippingInformationManagementInterface
60
46
*/
61
47
private $ shippingInformationManagement ;
62
48
63
49
/**
64
- * SetShippingMethodsOnCart constructor.
65
- * @param ArrayManager $arrayManager
66
- * @param GetCartForUser $getCartForUser
67
50
* @param ShippingInformationManagementInterface $shippingInformationManagement
68
51
* @param QuoteAddressFactory $quoteAddressFactory
69
52
* @param QuoteAddressResource $quoteAddressResource
70
53
* @param ShippingInformationFactory $shippingInformationFactory
71
54
*/
72
55
public function __construct (
73
- ArrayManager $ arrayManager ,
74
- GetCartForUser $ getCartForUser ,
75
56
ShippingInformationManagementInterface $ shippingInformationManagement ,
76
57
QuoteAddressFactory $ quoteAddressFactory ,
77
58
QuoteAddressResource $ quoteAddressResource ,
78
59
ShippingInformationFactory $ shippingInformationFactory
79
60
) {
80
- $ this ->arrayManager = $ arrayManager ;
81
- $ this ->getCartForUser = $ getCartForUser ;
82
61
$ this ->shippingInformationManagement = $ shippingInformationManagement ;
83
62
$ this ->quoteAddressResource = $ quoteAddressResource ;
84
63
$ this ->quoteAddressFactory = $ quoteAddressFactory ;
85
64
$ this ->shippingInformationFactory = $ shippingInformationFactory ;
86
65
}
87
66
88
67
/**
89
- * @inheritdoc
68
+ * Sets shipping method for a specified shopping cart address
69
+ *
70
+ * @param Quote $cart
71
+ * @param int $cartAddressId
72
+ * @param string $carrierCode
73
+ * @param string $methodCode
74
+ * @throws GraphQlInputException
75
+ * @throws GraphQlNoSuchEntityException
90
76
*/
91
- public function resolve ( Field $ field , $ context , ResolveInfo $ info , array $ value = null , array $ args = null )
77
+ public function execute ( Quote $ cart , int $ cartAddressId , string $ carrierCode , string $ methodCode ): void
92
78
{
93
- $ shippingMethods = $ this ->arrayManager ->get ('input/shipping_methods ' , $ args );
94
- $ maskedCartId = $ this ->arrayManager ->get ('input/cart_id ' , $ args );
95
-
96
- if (!$ maskedCartId ) {
97
- throw new GraphQlInputException (__ ('Required parameter "cart_id" is missing ' ));
98
- }
99
- if (!$ shippingMethods ) {
100
- throw new GraphQlInputException (__ ('Required parameter "shipping_methods" is missing ' ));
101
- }
102
-
103
- $ shippingMethod = reset ($ shippingMethods );
104
-
105
- if (!$ shippingMethod ['cart_address_id ' ]) {
106
- throw new GraphQlInputException (__ ('Required parameter "cart_address_id" is missing ' ));
107
- }
108
- if (!$ shippingMethod ['shipping_carrier_code ' ]) {
109
- throw new GraphQlInputException (__ ('Required parameter "shipping_carrier_code" is missing ' ));
110
- }
111
- if (!$ shippingMethod ['shipping_method_code ' ]) {
112
- throw new GraphQlInputException (__ ('Required parameter "shipping_method_code" is missing ' ));
113
- }
114
-
115
- $ userId = $ context ->getUserId ();
116
- $ cart = $ this ->getCartForUser ->execute ((string ) $ maskedCartId , $ userId );
117
-
118
79
$ quoteAddress = $ this ->quoteAddressFactory ->create ();
119
- $ this ->quoteAddressResource ->load ($ quoteAddress , $ shippingMethod [ ' cart_address_id ' ] );
80
+ $ this ->quoteAddressResource ->load ($ quoteAddress , $ cartAddressId );
120
81
121
82
/** @var ShippingInformation $shippingInformation */
122
83
$ shippingInformation = $ this ->shippingInformationFactory ->create ();
123
84
124
85
/* If the address is not a shipping address (but billing) the system will find the proper shipping address for
125
86
the selected cart and set the information there (actual for single shipping address) */
126
87
$ shippingInformation ->setShippingAddress ($ quoteAddress );
127
- $ shippingInformation ->setShippingCarrierCode ($ shippingMethod [ ' shipping_carrier_code ' ] );
128
- $ shippingInformation ->setShippingMethodCode ($ shippingMethod [ ' shipping_method_code ' ] );
88
+ $ shippingInformation ->setShippingCarrierCode ($ carrierCode );
89
+ $ shippingInformation ->setShippingMethodCode ($ methodCode );
129
90
130
91
try {
131
92
$ this ->shippingInformationManagement ->saveAddressInformation ($ cart ->getId (), $ shippingInformation );
@@ -136,12 +97,5 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
136
97
} catch (InputException $ exception ) {
137
98
throw new GraphQlInputException (__ ($ exception ->getMessage ()));
138
99
}
139
-
140
- return [
141
- 'cart ' => [
142
- 'cart_id ' => $ maskedCartId ,
143
- 'model ' => $ cart
144
- ]
145
- ];
146
100
}
147
- }
101
+ }
0 commit comments