Skip to content

Commit c98c3d5

Browse files
committed
Logic for setting shipping method is moved to a separate class
1 parent 5c0bcfd commit c98c3d5

File tree

3 files changed

+119
-66
lines changed

3 files changed

+119
-66
lines changed
Lines changed: 19 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,26 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\QuoteGraphQl\Model\Resolver\ShippingMethod;
8+
namespace Magento\QuoteGraphQl\Model\Cart;
99

10-
use Magento\Checkout\Api\ShippingInformationManagementInterface;
11-
use Magento\Checkout\Model\ShippingInformation;
1210
use Magento\Framework\Exception\InputException;
1311
use Magento\Framework\Exception\NoSuchEntityException;
1412
use Magento\Framework\Exception\StateException;
1513
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1614
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;
2116
use Magento\Quote\Model\Quote\AddressFactory as QuoteAddressFactory;
2217
use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource;
2318
use Magento\Checkout\Model\ShippingInformationFactory;
24-
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
19+
use Magento\Checkout\Api\ShippingInformationManagementInterface;
20+
use Magento\Checkout\Model\ShippingInformation;
2521

2622
/**
2723
* Class SetShippingMethodsOnCart
2824
*
29-
* Mutation resolver for setting shipping methods for shopping cart
25+
* Set shipping method for a specified shopping cart address
3026
*/
31-
class SetShippingMethodsOnCart implements ResolverInterface
27+
class SetShippingMethodOnCart
3228
{
3329
/**
3430
* @var ShippingInformationFactory
@@ -45,87 +41,52 @@ class SetShippingMethodsOnCart implements ResolverInterface
4541
*/
4642
private $quoteAddressResource;
4743

48-
/**
49-
* @var ArrayManager
50-
*/
51-
private $arrayManager;
52-
53-
/**
54-
* @var GetCartForUser
55-
*/
56-
private $getCartForUser;
57-
5844
/**
5945
* @var ShippingInformationManagementInterface
6046
*/
6147
private $shippingInformationManagement;
6248

6349
/**
64-
* SetShippingMethodsOnCart constructor.
65-
* @param ArrayManager $arrayManager
66-
* @param GetCartForUser $getCartForUser
6750
* @param ShippingInformationManagementInterface $shippingInformationManagement
6851
* @param QuoteAddressFactory $quoteAddressFactory
6952
* @param QuoteAddressResource $quoteAddressResource
7053
* @param ShippingInformationFactory $shippingInformationFactory
7154
*/
7255
public function __construct(
73-
ArrayManager $arrayManager,
74-
GetCartForUser $getCartForUser,
7556
ShippingInformationManagementInterface $shippingInformationManagement,
7657
QuoteAddressFactory $quoteAddressFactory,
7758
QuoteAddressResource $quoteAddressResource,
7859
ShippingInformationFactory $shippingInformationFactory
7960
) {
80-
$this->arrayManager = $arrayManager;
81-
$this->getCartForUser = $getCartForUser;
8261
$this->shippingInformationManagement = $shippingInformationManagement;
8362
$this->quoteAddressResource = $quoteAddressResource;
8463
$this->quoteAddressFactory = $quoteAddressFactory;
8564
$this->shippingInformationFactory = $shippingInformationFactory;
8665
}
8766

8867
/**
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
9076
*/
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
9278
{
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-
11879
$quoteAddress = $this->quoteAddressFactory->create();
119-
$this->quoteAddressResource->load($quoteAddress, $shippingMethod['cart_address_id']);
80+
$this->quoteAddressResource->load($quoteAddress, $cartAddressId);
12081

12182
/** @var ShippingInformation $shippingInformation */
12283
$shippingInformation = $this->shippingInformationFactory->create();
12384

12485
/* If the address is not a shipping address (but billing) the system will find the proper shipping address for
12586
the selected cart and set the information there (actual for single shipping address) */
12687
$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);
12990

13091
try {
13192
$this->shippingInformationManagement->saveAddressInformation($cart->getId(), $shippingInformation);
@@ -136,12 +97,5 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
13697
} catch (InputException $exception) {
13798
throw new GraphQlInputException(__($exception->getMessage()));
13899
}
139-
140-
return [
141-
'cart' => [
142-
'cart_id' => $maskedCartId,
143-
'model' => $cart
144-
]
145-
];
146100
}
147-
}
101+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
11+
use Magento\Framework\GraphQl\Query\ResolverInterface;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\Framework\Stdlib\ArrayManager;
15+
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
16+
use Magento\QuoteGraphQl\Model\Cart\SetShippingMethodOnCart;
17+
18+
/**
19+
* Class SetShippingMethodsOnCart
20+
*
21+
* Mutation resolver for setting shipping methods for shopping cart
22+
*/
23+
class SetShippingMethodsOnCart implements ResolverInterface
24+
{
25+
/**
26+
* @var SetShippingMethodOnCart
27+
*/
28+
private $setShippingMethodOnCart;
29+
30+
/**
31+
* @var ArrayManager
32+
*/
33+
private $arrayManager;
34+
35+
/**
36+
* @var GetCartForUser
37+
*/
38+
private $getCartForUser;
39+
40+
/**
41+
* @param ArrayManager $arrayManager
42+
* @param GetCartForUser $getCartForUser
43+
* @param SetShippingMethodOnCart $setShippingMethodOnCart
44+
*/
45+
public function __construct(
46+
ArrayManager $arrayManager,
47+
GetCartForUser $getCartForUser,
48+
SetShippingMethodOnCart $setShippingMethodOnCart
49+
) {
50+
$this->arrayManager = $arrayManager;
51+
$this->getCartForUser = $getCartForUser;
52+
$this->setShippingMethodOnCart = $setShippingMethodOnCart;
53+
}
54+
55+
/**
56+
* @inheritdoc
57+
*/
58+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
59+
{
60+
$shippingMethods = $this->arrayManager->get('input/shipping_methods', $args);
61+
$maskedCartId = $this->arrayManager->get('input/cart_id', $args);
62+
63+
if (!$maskedCartId) {
64+
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
65+
}
66+
if (!$shippingMethods) {
67+
throw new GraphQlInputException(__('Required parameter "shipping_methods" is missing'));
68+
}
69+
70+
$shippingMethod = reset($shippingMethods); // This point can be extended for multishipping
71+
72+
if (!$shippingMethod['cart_address_id']) {
73+
throw new GraphQlInputException(__('Required parameter "cart_address_id" is missing'));
74+
}
75+
if (!$shippingMethod['shipping_carrier_code']) {
76+
throw new GraphQlInputException(__('Required parameter "shipping_carrier_code" is missing'));
77+
}
78+
if (!$shippingMethod['shipping_method_code']) {
79+
throw new GraphQlInputException(__('Required parameter "shipping_method_code" is missing'));
80+
}
81+
82+
$userId = $context->getUserId();
83+
$cart = $this->getCartForUser->execute((string) $maskedCartId, $userId);
84+
85+
$this->setShippingMethodOnCart->execute(
86+
$cart,
87+
$shippingMethod['cart_address_id'],
88+
$shippingMethod['shipping_carrier_code'],
89+
$shippingMethod['shipping_method_code']
90+
);
91+
92+
return [
93+
'cart' => [
94+
'cart_id' => $maskedCartId,
95+
'model' => $cart
96+
]
97+
];
98+
}
99+
}

app/code/Magento/QuoteGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type Mutation {
1111
removeCouponFromCart(input: RemoveCouponFromCartInput): RemoveCouponFromCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\RemoveCouponFromCart")
1212
setShippingAddressesOnCart(input: SetShippingAddressesOnCartInput): SetShippingAddressesOnCartOutput
1313
setBillingAddressOnCart(input: SetBillingAddressOnCartInput): SetBillingAddressOnCartOutput
14-
setShippingMethodsOnCart(input: SetShippingMethodsOnCartInput): SetShippingMethodsOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingMethod\\SetShippingMethodsOnCart")
14+
setShippingMethodsOnCart(input: SetShippingMethodsOnCartInput): SetShippingMethodsOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetShippingMethodsOnCart")
1515
addSimpleProductsToCart(input: AddSimpleProductsToCartInput): AddSimpleProductsToCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AddSimpleProductsToCart")
1616
}
1717

0 commit comments

Comments
 (0)