Skip to content

Commit d88a153

Browse files
ENGCOM-4430: Base shipping methods support #342
- Merge Pull Request magento/graphql-ce#342 from magento/graphql-ce:base-shipping-methods-checkout - Merged commits: 1. 2cfeec7 2. 15c9377 3. c88e975 4. b1a0bb6 5. b4c032b 6. d10e506 7. 65ff06b 8. a8c5504 9. f3c9247 10. 0ef7711 11. e02dc94 12. f2f05df 13. b424d79 14. 8b2106a 15. a13639c 16. bfacd22 17. 031dc63
2 parents 3a4ba66 + 031dc63 commit d88a153

28 files changed

+845
-210
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
use Magento\Framework\Exception\NoSuchEntityException;
1212
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1313
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
14+
use Magento\Quote\Api\Data\AddressInterface;
1415
use Magento\Quote\Api\Data\CartInterface;
1516
use Magento\Quote\Api\BillingAddressManagementInterface;
16-
use Magento\Quote\Model\Quote\Address as QuoteAddress;
1717

1818
/**
1919
* Set billing address for a specified shopping cart
@@ -38,22 +38,22 @@ public function __construct(
3838
* Assign billing address to cart
3939
*
4040
* @param CartInterface $cart
41-
* @param QuoteAddress $billingAddress
41+
* @param AddressInterface $billingAddress
4242
* @param bool $useForShipping
4343
* @throws GraphQlInputException
4444
* @throws GraphQlNoSuchEntityException
4545
*/
4646
public function execute(
4747
CartInterface $cart,
48-
QuoteAddress $billingAddress,
48+
AddressInterface $billingAddress,
4949
bool $useForShipping
5050
): void {
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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
use Magento\Framework\Exception\NoSuchEntityException;
1212
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1313
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
14+
use Magento\Quote\Api\Data\AddressInterface;
1415
use Magento\Quote\Api\Data\CartInterface;
15-
use Magento\Quote\Model\Quote\Address as QuoteAddress;
1616
use Magento\Quote\Model\ShippingAddressManagementInterface;
1717

1818
/**
@@ -38,20 +38,20 @@ public function __construct(
3838
* Assign shipping address to cart
3939
*
4040
* @param CartInterface $cart
41-
* @param QuoteAddress $shippingAddress
41+
* @param AddressInterface $shippingAddress
4242
* @throws GraphQlInputException
4343
* @throws GraphQlNoSuchEntityException
4444
*/
4545
public function execute(
4646
CartInterface $cart,
47-
QuoteAddress $shippingAddress
47+
AddressInterface $shippingAddress
4848
): void {
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\AddressInterface;
18+
use Magento\Quote\Api\Data\CartInterface;
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 AddressInterface $quoteAddress
52+
* @param string $carrierCode
53+
* @param string $methodCode
54+
* @throws GraphQlInputException
55+
* @throws GraphQlNoSuchEntityException
56+
*/
57+
public function execute(
58+
CartInterface $cart,
59+
AddressInterface $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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ public function execute(QuoteAddress $address): array
5151
'label' => $address->getRegion()
5252
],
5353
'street' => $address->getStreet(),
54-
'selected_shipping_method' => [
55-
'code' => $address->getShippingMethod(),
56-
'label' => $address->getShippingDescription(),
57-
'free_shipping' => $address->getFreeShipping(),
58-
'amount' => $address->getShippingAmount()
59-
],
6054
'items_weight' => $address->getWeight(),
6155
'customer_notes' => $address->getCustomerNotes()
6256
]);

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

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: 6 additions & 6 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(

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)