-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GraphQL-418: [Shipping methods] Set Shipping Methods on Cart
- Loading branch information
Showing
21 changed files
with
392 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
app/code/Magento/QuoteGraphQl/Model/Cart/AssignShippingMethodToCart.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\QuoteGraphQl\Model\Cart; | ||
|
||
use Magento\Checkout\Api\Data\ShippingInformationInterface; | ||
use Magento\Checkout\Api\Data\ShippingInformationInterfaceFactory; | ||
use Magento\Checkout\Api\ShippingInformationManagementInterface; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; | ||
use Magento\Quote\Api\Data\CartInterface; | ||
use Magento\Quote\Model\Quote\Address as QuoteAddress; | ||
|
||
/** | ||
* Assign shipping method to cart | ||
*/ | ||
class AssignShippingMethodToCart | ||
{ | ||
/** | ||
* @var ShippingInformationInterfaceFactory | ||
*/ | ||
private $shippingInformationFactory; | ||
|
||
/** | ||
* @var ShippingInformationManagementInterface | ||
*/ | ||
private $shippingInformationManagement; | ||
|
||
/** | ||
* @param ShippingInformationInterfaceFactory $shippingInformationFactory | ||
* @param ShippingInformationManagementInterface $shippingInformationManagement | ||
*/ | ||
public function __construct( | ||
ShippingInformationInterfaceFactory $shippingInformationFactory, | ||
ShippingInformationManagementInterface $shippingInformationManagement | ||
) { | ||
$this->shippingInformationFactory = $shippingInformationFactory; | ||
$this->shippingInformationManagement = $shippingInformationManagement; | ||
} | ||
|
||
/** | ||
* Assign shipping method to cart | ||
* | ||
* @param CartInterface $cart | ||
* @param QuoteAddress $quoteAddress | ||
* @param string $carrierCode | ||
* @param string $methodCode | ||
* @throws GraphQlInputException | ||
* @throws GraphQlNoSuchEntityException | ||
*/ | ||
public function execute( | ||
CartInterface $cart, | ||
QuoteAddress $quoteAddress, | ||
string $carrierCode, | ||
string $methodCode | ||
): void { | ||
/** @var ShippingInformationInterface $shippingInformation */ | ||
$shippingInformation = $this->shippingInformationFactory->create([ | ||
'data' => [ | ||
/* If the address is not a shipping address (but billing) the system will find the proper shipping | ||
address for the selected cart and set the information there (actual for single shipping address) */ | ||
ShippingInformationInterface::SHIPPING_ADDRESS => $quoteAddress, | ||
ShippingInformationInterface::SHIPPING_CARRIER_CODE => $carrierCode, | ||
ShippingInformationInterface::SHIPPING_METHOD_CODE => $methodCode, | ||
], | ||
]); | ||
|
||
try { | ||
$this->shippingInformationManagement->saveAddressInformation($cart->getId(), $shippingInformation); | ||
} catch (NoSuchEntityException $e) { | ||
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); | ||
} catch (LocalizedException $e) { | ||
throw new GraphQlInputException(__($e->getMessage()), $e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
app/code/Magento/QuoteGraphQl/Model/Cart/GetQuoteAddress.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\QuoteGraphQl\Model\Cart; | ||
|
||
use Magento\Customer\Api\AddressRepositoryInterface; | ||
use Magento\Customer\Api\Data\AddressInterface; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; | ||
use Magento\Quote\Model\Quote\Address as QuoteAddress; | ||
use Magento\Quote\Model\Quote\AddressFactory as QuoteAddressFactory; | ||
use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource; | ||
|
||
|
||
/** | ||
* Get quote address | ||
*/ | ||
class GetQuoteAddress | ||
{ | ||
/** | ||
* @var QuoteAddressFactory | ||
*/ | ||
private $quoteAddressFactory; | ||
|
||
/** | ||
* @var QuoteAddressResource | ||
*/ | ||
private $quoteAddressResource; | ||
|
||
/** | ||
* @var AddressRepositoryInterface | ||
*/ | ||
private $addressRepository; | ||
|
||
/** | ||
* @param AddressRepositoryInterface $addressRepository | ||
*/ | ||
public function __construct(AddressRepositoryInterface $addressRepository) | ||
{ | ||
$this->addressRepository = $addressRepository; | ||
} | ||
|
||
/** | ||
* Get quote address | ||
* | ||
* @param int $quoteAddressId | ||
* @param int|null $customerId | ||
* @return AddressInterface | ||
* @throws GraphQlInputException | ||
* @throws GraphQlNoSuchEntityException | ||
* @throws GraphQlAuthorizationException | ||
*/ | ||
public function execute(int $quoteAddressId, ?int $customerId): QuoteAddress | ||
{ | ||
$quoteAddress = $this->quoteAddressFactory->create(); | ||
|
||
$this->quoteAddressResource->load($quoteAddress, $quoteAddressId); | ||
if (null === $quoteAddress->getId()) { | ||
throw new GraphQlNoSuchEntityException( | ||
__('Could not find a cart address with ID "%cart_address_id"', ['cart_address_id' => $quoteAddressId]) | ||
); | ||
} | ||
|
||
$quoteAddressCustomerId = (int)$quoteAddress->getCustomerId(); | ||
|
||
/* Guest cart, allow operations */ | ||
if (!$quoteAddressCustomerId && null === $customerId) { | ||
return $quoteAddress; | ||
} | ||
|
||
if ($quoteAddressCustomerId !== $customerId) { | ||
throw new GraphQlAuthorizationException( | ||
__( | ||
'The current user cannot use cart address with ID "%cart_address_id"', | ||
['cart_address_id' => $quoteAddressId] | ||
) | ||
); | ||
} | ||
return $quoteAddress; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.