Skip to content

Commit e327bb6

Browse files
author
vitaliyboyko
committed
graphQl: shipping address concept
Signed-off-by: vitaliyboyko <v.boyko@atwix.com>
1 parent 93f6e5e commit e327bb6

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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\ShippingAddress;
9+
10+
use Magento\Customer\Api\AddressRepositoryInterface;
11+
use Magento\Framework\Api\DataObjectHelper;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
17+
use Magento\Quote\Model\Quote\Address;
18+
use Magento\Quote\Model\ShippingAddressManagementInterface;
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
class SetShippingAddressesOnCart implements ResolverInterface
24+
{
25+
/**
26+
* @var ShippingAddressManagementInterface
27+
*/
28+
private $shippingAddressManagement;
29+
30+
/**
31+
* @var AddressRepositoryInterface
32+
*/
33+
private $addressRepository;
34+
35+
/**
36+
* @var Address
37+
*/
38+
private $addressModel;
39+
/**
40+
* @var DataObjectHelper
41+
*/
42+
private $dataObjectHelper;
43+
44+
/**
45+
* @var MaskedQuoteIdToQuoteIdInterface
46+
*/
47+
private $maskedQuoteIdToQuoteId;
48+
49+
/**
50+
* @param ShippingAddressManagementInterface $shippingAddressManagement
51+
* @param AddressRepositoryInterface $addressRepository
52+
* @param Address $addressModel
53+
* @param DataObjectHelper $dataObjectHelper
54+
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
55+
*/
56+
public function __construct(
57+
ShippingAddressManagementInterface $shippingAddressManagement,
58+
AddressRepositoryInterface $addressRepository,
59+
Address $addressModel,
60+
DataObjectHelper $dataObjectHelper,
61+
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
62+
) {
63+
$this->shippingAddressManagement = $shippingAddressManagement;
64+
$this->addressRepository = $addressRepository;
65+
$this->addressModel = $addressModel;
66+
$this->dataObjectHelper = $dataObjectHelper;
67+
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
68+
}
69+
70+
/**
71+
* @inheritdoc
72+
*/
73+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
74+
{
75+
if (!isset($args['input']['cart_id'])) {
76+
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
77+
}
78+
$maskedCartId = $args['input']['cart_id'];
79+
$cartId = $this->maskedQuoteIdToQuoteId->execute($maskedCartId);
80+
81+
$customerAddressId = $args['input']['customer_address_id'] ?? 0;
82+
$address = $args['input']['address'] ?? null;
83+
$cartItems = $args['input']['cart_items'] ?? [];
84+
85+
if (!$customerAddressId && !$address) {
86+
throw new GraphQlInputException(__('Query should contain either address id or address input.'));
87+
}
88+
89+
if (!$cartItems) {
90+
if($customerAddressId) {
91+
$customerAddress = $this->addressRepository->getById($customerAddressId);
92+
$shippingAddress = $this->addressModel->importCustomerAddressData($customerAddress);
93+
$this->shippingAddressManagement->assign($cartId, $shippingAddress);
94+
} else {
95+
$shippingAddress = $this->addressModel->addData($address);
96+
$this->shippingAddressManagement->assign($cartId, $shippingAddress);
97+
}
98+
} else {
99+
//TODO: implement multi shipping address assign flow
100+
}
101+
102+
return [];
103+
}
104+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type Mutation {
99
createEmptyCart: String @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\Cart\\CreateEmptyCart") @doc(description:"Creates empty shopping cart for guest or logged in user")
1010
applyCouponToCart(input: ApplyCouponToCartInput): ApplyCouponToCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\Coupon\\ApplyCouponToCart")
1111
removeCouponFromCart(input: RemoveCouponFromCartInput): RemoveCouponFromCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\Coupon\\RemoveCouponFromCart")
12-
setShippingAddressesOnCart(input: SetShippingAddressesOnCartInput): SetShippingAddressesOnCartOutput
12+
setShippingAddressesOnCart(input: SetShippingAddressesOnCartInput): SetShippingAddressesOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddress\\SetShippingAddressesOnCart")
1313
setBillingAddressOnCart(input: SetBillingAddressOnCartInput): SetBillingAddressOnCartOutput
1414
setShippingMethodsOnCart(input: SetShippingMethodsOnCartInput): SetShippingMethodsOnCartOutput
1515
}

0 commit comments

Comments
 (0)