Skip to content

Commit 0ef7711

Browse files
committed
GraphQL-418: [Shipping methods] Set Shipping Methods on Cart
1 parent f3c9247 commit 0ef7711

21 files changed

+392
-208
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/AssignBillingAddressToCart.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ public function execute(
5151
try {
5252
$this->billingAddressManagement->assign($cart->getId(), $billingAddress, $useForShipping);
5353
} catch (NoSuchEntityException $e) {
54-
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
54+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
5555
} catch (LocalizedException $e) {
56-
throw new GraphQlInputException(__($e->getMessage()));
56+
throw new GraphQlInputException(__($e->getMessage()), $e);
5757
}
5858
}
5959
}

app/code/Magento/QuoteGraphQl/Model/Cart/AssignShippingAddressToCart.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ public function execute(
4949
try {
5050
$this->shippingAddressManagement->assign($cart->getId(), $shippingAddress);
5151
} catch (NoSuchEntityException $e) {
52-
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
52+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
5353
} catch (LocalizedException $e) {
54-
throw new GraphQlInputException(__($e->getMessage()));
54+
throw new GraphQlInputException(__($e->getMessage()), $e);
5555
}
5656
}
5757
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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\Checkout\Api\Data\ShippingInformationInterface;
11+
use Magento\Checkout\Api\Data\ShippingInformationInterfaceFactory;
12+
use Magento\Checkout\Api\ShippingInformationManagementInterface;
13+
use Magento\Framework\Exception\LocalizedException;
14+
use Magento\Framework\Exception\NoSuchEntityException;
15+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
16+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
17+
use Magento\Quote\Api\Data\CartInterface;
18+
use Magento\Quote\Model\Quote\Address as QuoteAddress;
19+
20+
/**
21+
* Assign shipping method to cart
22+
*/
23+
class AssignShippingMethodToCart
24+
{
25+
/**
26+
* @var ShippingInformationInterfaceFactory
27+
*/
28+
private $shippingInformationFactory;
29+
30+
/**
31+
* @var ShippingInformationManagementInterface
32+
*/
33+
private $shippingInformationManagement;
34+
35+
/**
36+
* @param ShippingInformationInterfaceFactory $shippingInformationFactory
37+
* @param ShippingInformationManagementInterface $shippingInformationManagement
38+
*/
39+
public function __construct(
40+
ShippingInformationInterfaceFactory $shippingInformationFactory,
41+
ShippingInformationManagementInterface $shippingInformationManagement
42+
) {
43+
$this->shippingInformationFactory = $shippingInformationFactory;
44+
$this->shippingInformationManagement = $shippingInformationManagement;
45+
}
46+
47+
/**
48+
* Assign shipping method to cart
49+
*
50+
* @param CartInterface $cart
51+
* @param QuoteAddress $quoteAddress
52+
* @param string $carrierCode
53+
* @param string $methodCode
54+
* @throws GraphQlInputException
55+
* @throws GraphQlNoSuchEntityException
56+
*/
57+
public function execute(
58+
CartInterface $cart,
59+
QuoteAddress $quoteAddress,
60+
string $carrierCode,
61+
string $methodCode
62+
): void {
63+
/** @var ShippingInformationInterface $shippingInformation */
64+
$shippingInformation = $this->shippingInformationFactory->create([
65+
'data' => [
66+
/* If the address is not a shipping address (but billing) the system will find the proper shipping
67+
address for the selected cart and set the information there (actual for single shipping address) */
68+
ShippingInformationInterface::SHIPPING_ADDRESS => $quoteAddress,
69+
ShippingInformationInterface::SHIPPING_CARRIER_CODE => $carrierCode,
70+
ShippingInformationInterface::SHIPPING_METHOD_CODE => $methodCode,
71+
],
72+
]);
73+
74+
try {
75+
$this->shippingInformationManagement->saveAddressInformation($cart->getId(), $shippingInformation);
76+
} catch (NoSuchEntityException $e) {
77+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
78+
} catch (LocalizedException $e) {
79+
throw new GraphQlInputException(__($e->getMessage()), $e);
80+
}
81+
}
82+
}

app/code/Magento/QuoteGraphQl/Model/Cart/ExtractDataFromAddress.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ public function execute(QuoteAddress $address): array
4040
$addressData = $this->dataObjectConverter->toFlatArray($address, [], AddressInterface::class);
4141
$addressData['model'] = $address;
4242

43-
if ($address->getShippingMethod()) {
44-
list($carrierCode, $methodCode) = explode('_', $address->getShippingMethod(), 2);
45-
$shippingAmount = $address->getShippingAmount();
46-
}
47-
4843
$addressData = array_merge($addressData, [
4944
'address_id' => $address->getId(),
5045
'country' => [
@@ -56,12 +51,6 @@ public function execute(QuoteAddress $address): array
5651
'label' => $address->getRegion()
5752
],
5853
'street' => $address->getStreet(),
59-
'selected_shipping_method' => [
60-
'carrier_code' => $carrierCode ?? null,
61-
'method_code' => $methodCode ?? null,
62-
'label' => $address->getShippingDescription(),
63-
'amount' => $shippingAmount ?? null
64-
],
6554
'items_weight' => $address->getWeight(),
6655
'customer_notes' => $address->getCustomerNotes()
6756
]);

app/code/Magento/QuoteGraphQl/Model/Cart/GetCustomerAddress.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1717

1818
/**
19-
* Get customer address. Throws exception if customer is not owner of address
19+
* Get customer address
2020
*/
2121
class GetCustomerAddress
2222
{
@@ -52,7 +52,7 @@ public function execute(int $addressId, int $customerId): AddressInterface
5252
__('Could not find a address with ID "%address_id"', ['address_id' => $addressId])
5353
);
5454
} catch (LocalizedException $e) {
55-
throw new GraphQlInputException(__($e->getMessage()));
55+
throw new GraphQlInputException(__($e->getMessage()), $e);
5656
}
5757

5858
if ((int)$customerAddress->getCustomerId() !== $customerId) {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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\Customer\Api\AddressRepositoryInterface;
11+
use Magento\Customer\Api\Data\AddressInterface;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
15+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
16+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
17+
use Magento\Quote\Model\Quote\Address as QuoteAddress;
18+
use Magento\Quote\Model\Quote\AddressFactory as QuoteAddressFactory;
19+
use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource;
20+
21+
22+
/**
23+
* Get quote address
24+
*/
25+
class GetQuoteAddress
26+
{
27+
/**
28+
* @var QuoteAddressFactory
29+
*/
30+
private $quoteAddressFactory;
31+
32+
/**
33+
* @var QuoteAddressResource
34+
*/
35+
private $quoteAddressResource;
36+
37+
/**
38+
* @var AddressRepositoryInterface
39+
*/
40+
private $addressRepository;
41+
42+
/**
43+
* @param AddressRepositoryInterface $addressRepository
44+
*/
45+
public function __construct(AddressRepositoryInterface $addressRepository)
46+
{
47+
$this->addressRepository = $addressRepository;
48+
}
49+
50+
/**
51+
* Get quote address
52+
*
53+
* @param int $quoteAddressId
54+
* @param int|null $customerId
55+
* @return AddressInterface
56+
* @throws GraphQlInputException
57+
* @throws GraphQlNoSuchEntityException
58+
* @throws GraphQlAuthorizationException
59+
*/
60+
public function execute(int $quoteAddressId, ?int $customerId): QuoteAddress
61+
{
62+
$quoteAddress = $this->quoteAddressFactory->create();
63+
64+
$this->quoteAddressResource->load($quoteAddress, $quoteAddressId);
65+
if (null === $quoteAddress->getId()) {
66+
throw new GraphQlNoSuchEntityException(
67+
__('Could not find a cart address with ID "%cart_address_id"', ['cart_address_id' => $quoteAddressId])
68+
);
69+
}
70+
71+
$quoteAddressCustomerId = (int)$quoteAddress->getCustomerId();
72+
73+
/* Guest cart, allow operations */
74+
if (!$quoteAddressCustomerId && null === $customerId) {
75+
return $quoteAddress;
76+
}
77+
78+
if ($quoteAddressCustomerId !== $customerId) {
79+
throw new GraphQlAuthorizationException(
80+
__(
81+
'The current user cannot use cart address with ID "%cart_address_id"',
82+
['cart_address_id' => $quoteAddressId]
83+
)
84+
);
85+
}
86+
return $quoteAddress;
87+
}
88+
}

app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function createBasedOnCustomerAddress(CustomerAddress $customerAddress):
6060
try {
6161
$quoteAddress->importCustomerAddressData($customerAddress);
6262
} catch (LocalizedException $e) {
63-
throw new GraphQlInputException(__($e->getMessage()));
63+
throw new GraphQlInputException(__($e->getMessage()), $e);
6464
}
6565
return $quoteAddress;
6666
}

app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,19 @@ public function __construct(
6363
*
6464
* @param ContextInterface $context
6565
* @param CartInterface $cart
66-
* @param array $billingAddress
66+
* @param array $billingAddressInput
6767
* @return void
6868
* @throws GraphQlInputException
6969
* @throws GraphQlAuthenticationException
7070
* @throws GraphQlAuthorizationException
7171
* @throws GraphQlNoSuchEntityException
7272
*/
73-
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddress): void
73+
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddressInput): void
7474
{
75-
$customerAddressId = $billingAddress['customer_address_id'] ?? null;
76-
$addressInput = $billingAddress['address'] ?? null;
77-
$useForShipping = isset($billingAddress['use_for_shipping'])
78-
? (bool)$billingAddress['use_for_shipping'] : false;
75+
$customerAddressId = $billingAddressInput['customer_address_id'] ?? null;
76+
$addressInput = $billingAddressInput['address'] ?? null;
77+
$useForShipping = isset($billingAddressInput['use_for_shipping'])
78+
? (bool)$billingAddressInput['use_for_shipping'] : false;
7979

8080
if (null === $customerAddressId && null === $addressInput) {
8181
throw new GraphQlInputException(
@@ -97,13 +97,13 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b
9797
}
9898

9999
if (null === $customerAddressId) {
100-
$billingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
100+
$billingAddressInput = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
101101
} else {
102102
$this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType());
103103
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$context->getUserId());
104-
$billingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress);
104+
$billingAddressInput = $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress);
105105
}
106106

107-
$this->assignBillingAddressToCart->execute($cart, $billingAddress, $useForShipping);
107+
$this->assignBillingAddressToCart->execute($cart, $billingAddressInput, $useForShipping);
108108
}
109109
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Set single shipping address for a specified shopping cart
1717
*/
18-
class SetShippingAddressOnCart implements SetShippingAddressesOnCartInterface
18+
class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface
1919
{
2020
/**
2121
* @var QuoteAddressFactory
@@ -58,16 +58,16 @@ public function __construct(
5858
/**
5959
* @inheritdoc
6060
*/
61-
public function execute(ContextInterface $context, CartInterface $cart, array $shippingAddresses): void
61+
public function execute(ContextInterface $context, CartInterface $cart, array $shippingAddressesInput): void
6262
{
63-
if (count($shippingAddresses) > 1) {
63+
if (count($shippingAddressesInput) > 1) {
6464
throw new GraphQlInputException(
6565
__('You cannot specify multiple shipping addresses.')
6666
);
6767
}
68-
$shippingAddress = current($shippingAddresses);
69-
$customerAddressId = $shippingAddress['customer_address_id'] ?? null;
70-
$addressInput = $shippingAddress['address'] ?? null;
68+
$shippingAddressInput = current($shippingAddressesInput);
69+
$customerAddressId = $shippingAddressInput['customer_address_id'] ?? null;
70+
$addressInput = $shippingAddressInput['address'] ?? null;
7171

7272
if (null === $customerAddressId && null === $addressInput) {
7373
throw new GraphQlInputException(

app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCartInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ interface SetShippingAddressesOnCartInterface
2727
*
2828
* @param ContextInterface $context
2929
* @param CartInterface $cart
30-
* @param array $shippingAddresses
30+
* @param array $shippingAddressesInput
3131
* @return void
3232
* @throws GraphQlInputException
3333
* @throws GraphQlAuthorizationException
3434
* @throws GraphQlAuthenticationException
3535
* @throws GraphQlNoSuchEntityException
3636
*/
37-
public function execute(ContextInterface $context, CartInterface $cart, array $shippingAddresses): void;
37+
public function execute(ContextInterface $context, CartInterface $cart, array $shippingAddressesInput): void;
3838
}

0 commit comments

Comments
 (0)