Skip to content

Commit e1f09fb

Browse files
author
vitaliyboyko
committed
graphQl: added extension point for multishipping
1 parent 5d2b1c8 commit e1f09fb

File tree

5 files changed

+106
-24
lines changed

5 files changed

+106
-24
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\Cart;
9+
10+
use InvalidArgumentException;
11+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
12+
13+
/**
14+
* Set shipping addresses for shopping cart processors chain
15+
*/
16+
class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface
17+
{
18+
/**
19+
* @var SetShippingAddressesOnCartInterface[]
20+
*/
21+
private $shippingAddressProcessors;
22+
23+
/**
24+
* @param array $shippingAddressProcessors
25+
*/
26+
public function __construct(
27+
array $shippingAddressProcessors
28+
) {
29+
$this->shippingAddressProcessors = $shippingAddressProcessors;
30+
}
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
public function execute(ContextInterface $context, int $cartId, array $shippingAddresses): void
36+
{
37+
foreach ($this->shippingAddressProcessors as $shippingAddressProcessor) {
38+
if (!$shippingAddressProcessor instanceof SetShippingAddressesOnCartInterface) {
39+
throw new InvalidArgumentException(
40+
get_class($shippingAddressProcessor) .
41+
' is not instance of \Magento\QuoteGraphQl\Model\Cart\SetShippingAddressesOnCartInterface.'
42+
);
43+
}
44+
45+
$shippingAddressProcessor->execute($context, $cartId, $shippingAddresses);
46+
}
47+
}
48+
}

app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressOnCart.php renamed to app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart/SingleShippingAddressProcessor.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\QuoteGraphQl\Model\Cart;
8+
namespace Magento\QuoteGraphQl\Model\Cart\SetShippingAddressesOnCart;
99

1010
use Magento\Authorization\Model\UserContextInterface;
1111
use Magento\Customer\Api\Data\AddressInterface;
@@ -15,13 +15,12 @@
1515
use Magento\Quote\Model\Quote\Address;
1616
use Magento\Quote\Model\ShippingAddressManagementInterface;
1717
use Magento\Customer\Api\AddressRepositoryInterface;
18+
use Magento\QuoteGraphQl\Model\Cart\SetShippingAddressesOnCartInterface;
1819

1920
/**
20-
* Class SetShippingAddressOnCart
21-
*
22-
* Set shipping address for a specified shopping cart
21+
* Set single shipping address for a specified shopping cart
2322
*/
24-
class SetShippingAddressOnCart
23+
class SingleShippingAddressProcessor implements SetShippingAddressesOnCartInterface
2524
{
2625
/**
2726
* @var ShippingAddressManagementInterface
@@ -54,17 +53,12 @@ public function __construct(
5453
}
5554

5655
/**
57-
* @param ContextInterface $context
58-
* @param int $cartId
59-
* @param array $shippingAddresses
60-
* @return void
56+
* @inheritdoc
6157
*/
62-
public function setAddresses(ContextInterface $context, int $cartId, array $shippingAddresses): void
58+
public function execute(ContextInterface $context, int $cartId, array $shippingAddresses): void
6359
{
6460
if (count($shippingAddresses) > 1) {
65-
throw new GraphQlInputException(
66-
__('Multiple address does not allowed here!')
67-
);
61+
return;
6862
}
6963
$shippingAddress = current($shippingAddresses);
7064
$customerAddressId = $shippingAddress['customer_address_id'] ?? null;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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\Cart;
9+
10+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
11+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
12+
13+
/**
14+
* Extension point for setting shipping addresses for a specified shopping cart
15+
*
16+
* All objects that are responsible for set shipping addresses on cart via GraphQl should implement this interface.
17+
*/
18+
interface SetShippingAddressesOnCartInterface
19+
{
20+
21+
/**
22+
* Set shipping addresses for a specified shopping cart
23+
*
24+
* @param ContextInterface $context
25+
* @param int $cartId
26+
* @param array $shippingAddresses
27+
* @return void
28+
* @throws GraphQlInputException
29+
*/
30+
public function execute(ContextInterface $context, int $cartId, array $shippingAddresses): void;
31+
}

app/code/Magento/QuoteGraphQl/Model/Resolver/SetShippingAddressesOnCart.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
1616
use Magento\Quote\Model\ShippingAddressManagementInterface;
1717
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
18-
use Magento\QuoteGraphQl\Model\Cart\SetShippingAddressOnCart;
18+
use Magento\QuoteGraphQl\Model\Cart\SetShippingAddressesOnCartInterface;
1919

2020
/**
2121
* Class SetShippingAddressesOnCart
@@ -29,11 +29,6 @@ class SetShippingAddressesOnCart implements ResolverInterface
2929
*/
3030
private $maskedQuoteIdToQuoteId;
3131

32-
/**
33-
* @var SetShippingAddressOnCart
34-
*/
35-
private $setShippingAddressOnCart;
36-
3732
/**
3833
* @var ShippingAddressManagementInterface
3934
*/
@@ -49,25 +44,30 @@ class SetShippingAddressesOnCart implements ResolverInterface
4944
*/
5045
private $arrayManager;
5146

47+
/**
48+
* @var SetShippingAddressesOnCartInterface
49+
*/
50+
private $setShippingAddressesOnCart;
51+
5252
/**
5353
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
54-
* @param SetShippingAddressOnCart $setShippingAddressOnCart
5554
* @param ShippingAddressManagementInterface $shippingAddressManagement
5655
* @param GetCartForUser $getCartForUser
5756
* @param ArrayManager $arrayManager
57+
* @param SetShippingAddressesOnCartInterface $setShippingAddressesOnCart
5858
*/
5959
public function __construct(
6060
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
61-
SetShippingAddressOnCart $setShippingAddressOnCart,
6261
ShippingAddressManagementInterface $shippingAddressManagement,
6362
GetCartForUser $getCartForUser,
64-
ArrayManager $arrayManager
63+
ArrayManager $arrayManager,
64+
SetShippingAddressesOnCartInterface $setShippingAddressesOnCart
6565
) {
6666
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
67-
$this->setShippingAddressOnCart = $setShippingAddressOnCart;
6867
$this->shippingAddressManagement = $shippingAddressManagement;
6968
$this->getCartForUser = $getCartForUser;
7069
$this->arrayManager = $arrayManager;
70+
$this->setShippingAddressesOnCart = $setShippingAddressesOnCart;
7171
}
7272

7373
/**
@@ -89,7 +89,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
8989
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
9090
$cartId = (int)$cart->getEntityId();
9191

92-
$this->setShippingAddressOnCart->setAddresses($context, $cartId, $shippingAddresses);
92+
$this->setShippingAddressesOnCart->execute($context, $cartId, $shippingAddresses);
9393

9494
return [
9595
'cart' => [

app/code/Magento/QuoteGraphQl/etc/graphql/di.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<preference for="Magento\QuoteGraphQl\Model\Cart\SetShippingAddressesOnCartInterface"
10+
type="Magento\QuoteGraphQl\Model\Cart\SetShippingAddressesOnCart" />
11+
<type name="Magento\QuoteGraphQl\Model\Cart\SetShippingAddressesOnCartInterface">
12+
<arguments>
13+
<argument name="shippingAddressProcessors" xsi:type="array">
14+
<item name="singleShippingAddress" xsi:type="object">Magento\QuoteGraphQl\Model\Cart\SetShippingAddressesOnCart\SingleShippingAddressProcessor</item>
15+
</argument>
16+
</arguments>
17+
</type>
918
<virtualType name="multishippingPaymentSpecification" type="Magento\Payment\Model\Method\Specification\Composite">
1019
<arguments>
1120
<argument name="specifications" xsi:type="array">

0 commit comments

Comments
 (0)