-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-4430: Base shipping methods support #342
- Loading branch information
Showing
28 changed files
with
845 additions
and
210 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\AddressInterface; | ||
use Magento\Quote\Api\Data\CartInterface; | ||
|
||
/** | ||
* 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 AddressInterface $quoteAddress | ||
* @param string $carrierCode | ||
* @param string $methodCode | ||
* @throws GraphQlInputException | ||
* @throws GraphQlNoSuchEntityException | ||
*/ | ||
public function execute( | ||
CartInterface $cart, | ||
AddressInterface $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
82 changes: 82 additions & 0 deletions
82
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,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\Framework\GraphQl\Exception\GraphQlAuthorizationException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; | ||
use Magento\Quote\Api\Data\AddressInterface; | ||
use Magento\Quote\Api\Data\AddressInterfaceFactory; | ||
use Magento\Quote\Model\ResourceModel\Quote\Address as AddressResource; | ||
|
||
/** | ||
* Get quote address | ||
*/ | ||
class GetQuoteAddress | ||
{ | ||
/** | ||
* @var AddressInterfaceFactory | ||
*/ | ||
private $quoteAddressFactory; | ||
|
||
/** | ||
* @var AddressResource | ||
*/ | ||
private $quoteAddressResource; | ||
|
||
/** | ||
* @param AddressInterfaceFactory $quoteAddressFactory | ||
* @param AddressResource $quoteAddressResource | ||
*/ | ||
public function __construct( | ||
AddressInterfaceFactory $quoteAddressFactory, | ||
AddressResource $quoteAddressResource | ||
) { | ||
$this->quoteAddressFactory = $quoteAddressFactory; | ||
$this->quoteAddressResource = $quoteAddressResource; | ||
} | ||
|
||
/** | ||
* 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): AddressInterface | ||
{ | ||
$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.