-
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.
Merge pull request #3442 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
- Loading branch information
Showing
35 changed files
with
879 additions
and
37 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
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
94 changes: 94 additions & 0 deletions
94
app/code/Magento/QuoteGraphQl/Model/Cart/Address/AddressDataProvider.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,94 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\QuoteGraphQl\Model\Cart\Address; | ||
|
||
use Magento\Framework\Api\ExtensibleDataObjectConverter; | ||
use Magento\Quote\Api\Data\AddressInterface; | ||
use Magento\Quote\Api\Data\CartInterface; | ||
use Magento\Quote\Model\Quote\Address as QuoteAddress; | ||
|
||
/** | ||
* Class AddressDataProvider | ||
* | ||
* Collect and return information about cart shipping and billing addresses | ||
*/ | ||
class AddressDataProvider | ||
{ | ||
/** | ||
* @var ExtensibleDataObjectConverter | ||
*/ | ||
private $dataObjectConverter; | ||
|
||
/** | ||
* AddressDataProvider constructor. | ||
* | ||
* @param ExtensibleDataObjectConverter $dataObjectConverter | ||
*/ | ||
public function __construct( | ||
ExtensibleDataObjectConverter $dataObjectConverter | ||
) { | ||
$this->dataObjectConverter = $dataObjectConverter; | ||
} | ||
|
||
/** | ||
* Collect and return information about shipping and billing addresses | ||
* | ||
* @param CartInterface $cart | ||
* @return array | ||
*/ | ||
public function getCartAddresses(CartInterface $cart): array | ||
{ | ||
$addressData = []; | ||
$shippingAddress = $cart->getShippingAddress(); | ||
$billingAddress = $cart->getBillingAddress(); | ||
|
||
if ($shippingAddress) { | ||
$shippingData = $this->dataObjectConverter->toFlatArray($shippingAddress, [], AddressInterface::class); | ||
$shippingData['address_type'] = 'SHIPPING'; | ||
$addressData[] = array_merge($shippingData, $this->extractAddressData($shippingAddress)); | ||
} | ||
|
||
if ($billingAddress) { | ||
$billingData = $this->dataObjectConverter->toFlatArray($billingAddress, [], AddressInterface::class); | ||
$billingData['address_type'] = 'BILLING'; | ||
$addressData[] = array_merge($billingData, $this->extractAddressData($billingAddress)); | ||
} | ||
|
||
return $addressData; | ||
} | ||
|
||
/** | ||
* Extract the necessary address fields from address model | ||
* | ||
* @param QuoteAddress $address | ||
* @return array | ||
*/ | ||
private function extractAddressData(QuoteAddress $address): array | ||
{ | ||
$addressData = [ | ||
'country' => [ | ||
'code' => $address->getCountryId(), | ||
'label' => $address->getCountry() | ||
], | ||
'region' => [ | ||
'code' => $address->getRegionCode(), | ||
'label' => $address->getRegion() | ||
], | ||
'street' => $address->getStreet(), | ||
'selected_shipping_method' => [ | ||
'code' => $address->getShippingMethod(), | ||
'label' => $address->getShippingDescription(), | ||
'free_shipping' => $address->getFreeShipping(), | ||
], | ||
'items_weight' => $address->getWeight(), | ||
'customer_notes' => $address->getCustomerNotes() | ||
]; | ||
|
||
return $addressData; | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingMethodOnCart.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,101 @@ | ||
<?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\Exception\InputException; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Framework\Exception\StateException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; | ||
use Magento\Quote\Model\Quote; | ||
use Magento\Quote\Model\Quote\AddressFactory as QuoteAddressFactory; | ||
use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource; | ||
use Magento\Checkout\Model\ShippingInformationFactory; | ||
use Magento\Checkout\Api\ShippingInformationManagementInterface; | ||
use Magento\Checkout\Model\ShippingInformation; | ||
|
||
/** | ||
* Class SetShippingMethodsOnCart | ||
* | ||
* Set shipping method for a specified shopping cart address | ||
*/ | ||
class SetShippingMethodOnCart | ||
{ | ||
/** | ||
* @var ShippingInformationFactory | ||
*/ | ||
private $shippingInformationFactory; | ||
|
||
/** | ||
* @var QuoteAddressFactory | ||
*/ | ||
private $quoteAddressFactory; | ||
|
||
/** | ||
* @var QuoteAddressResource | ||
*/ | ||
private $quoteAddressResource; | ||
|
||
/** | ||
* @var ShippingInformationManagementInterface | ||
*/ | ||
private $shippingInformationManagement; | ||
|
||
/** | ||
* @param ShippingInformationManagementInterface $shippingInformationManagement | ||
* @param QuoteAddressFactory $quoteAddressFactory | ||
* @param QuoteAddressResource $quoteAddressResource | ||
* @param ShippingInformationFactory $shippingInformationFactory | ||
*/ | ||
public function __construct( | ||
ShippingInformationManagementInterface $shippingInformationManagement, | ||
QuoteAddressFactory $quoteAddressFactory, | ||
QuoteAddressResource $quoteAddressResource, | ||
ShippingInformationFactory $shippingInformationFactory | ||
) { | ||
$this->shippingInformationManagement = $shippingInformationManagement; | ||
$this->quoteAddressResource = $quoteAddressResource; | ||
$this->quoteAddressFactory = $quoteAddressFactory; | ||
$this->shippingInformationFactory = $shippingInformationFactory; | ||
} | ||
|
||
/** | ||
* Sets shipping method for a specified shopping cart address | ||
* | ||
* @param Quote $cart | ||
* @param int $cartAddressId | ||
* @param string $carrierCode | ||
* @param string $methodCode | ||
* @throws GraphQlInputException | ||
* @throws GraphQlNoSuchEntityException | ||
*/ | ||
public function execute(Quote $cart, int $cartAddressId, string $carrierCode, string $methodCode): void | ||
{ | ||
$quoteAddress = $this->quoteAddressFactory->create(); | ||
$this->quoteAddressResource->load($quoteAddress, $cartAddressId); | ||
|
||
/** @var ShippingInformation $shippingInformation */ | ||
$shippingInformation = $this->shippingInformationFactory->create(); | ||
|
||
/* 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) */ | ||
$shippingInformation->setShippingAddress($quoteAddress); | ||
$shippingInformation->setShippingCarrierCode($carrierCode); | ||
$shippingInformation->setShippingMethodCode($methodCode); | ||
|
||
try { | ||
$this->shippingInformationManagement->saveAddressInformation($cart->getId(), $shippingInformation); | ||
} catch (NoSuchEntityException $exception) { | ||
throw new GraphQlNoSuchEntityException(__($exception->getMessage())); | ||
} catch (StateException $exception) { | ||
throw new GraphQlInputException(__($exception->getMessage())); | ||
} catch (InputException $exception) { | ||
throw new GraphQlInputException(__($exception->getMessage())); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddresses.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,48 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\QuoteGraphQl\Model\Resolver; | ||
|
||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\QuoteGraphQl\Model\Cart\Address\AddressDataProvider; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class CartAddresses implements ResolverInterface | ||
{ | ||
/** | ||
* @var AddressDataProvider | ||
*/ | ||
private $addressDataProvider; | ||
|
||
/** | ||
* @param AddressDataProvider $addressDataProvider | ||
*/ | ||
public function __construct( | ||
AddressDataProvider $addressDataProvider | ||
) { | ||
$this->addressDataProvider = $addressDataProvider; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
if (!isset($value['model'])) { | ||
throw new LocalizedException(__('"model" value should be specified')); | ||
} | ||
|
||
$cart = $value['model']; | ||
|
||
return $this->addressDataProvider->getCartAddresses($cart); | ||
} | ||
} |
Oops, something went wrong.