This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate resolver for address shipping methods
- Loading branch information
Showing
5 changed files
with
117 additions
and
15 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
67 changes: 67 additions & 0 deletions
67
app/code/Magento/QuoteGraphQl/Model/Cart/Address/ShippingMethodsDataProvider.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,67 @@ | ||
<?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\Quote\Api\Data\ShippingMethodInterface; | ||
use Magento\Framework\Api\ExtensibleDataObjectConverter; | ||
use Magento\Quote\Model\Cart\ShippingMethodConverter; | ||
use Magento\Quote\Model\Quote\Address as QuoteAddress; | ||
|
||
class ShippingMethodsDataProvider | ||
{ | ||
/** | ||
* @var ExtensibleDataObjectConverter | ||
*/ | ||
private $dataObjectConverter; | ||
|
||
/** | ||
* @var ShippingMethodConverter | ||
*/ | ||
private $shippingMethodConverter; | ||
|
||
/** | ||
* AddressDataProvider constructor. | ||
* | ||
* @param ExtensibleDataObjectConverter $dataObjectConverter | ||
* @param ShippingMethodConverter $shippingMethodConverter | ||
*/ | ||
public function __construct( | ||
ExtensibleDataObjectConverter $dataObjectConverter, | ||
ShippingMethodConverter $shippingMethodConverter | ||
) { | ||
$this->dataObjectConverter = $dataObjectConverter; | ||
$this->shippingMethodConverter = $shippingMethodConverter; | ||
} | ||
|
||
public function getAvailableShippingMethods(QuoteAddress $address): array | ||
{ | ||
$methods = []; | ||
|
||
// Allow shipping rates by setting country id for new addresses | ||
if (!$address->getCountryId() && $address->getCountryCode()) { | ||
$address->setCountryId($address->getCountryCode()); | ||
} | ||
|
||
$address->setCollectShippingRates(true); | ||
$address->collectShippingRates(); | ||
$cart = $address->getQuote(); | ||
|
||
$shippingRates = $address->getGroupedAllShippingRates(); | ||
foreach ($shippingRates as $carrierRates) { | ||
foreach ($carrierRates as $rate) { | ||
$methods[] = $this->dataObjectConverter->toFlatArray( | ||
$this->shippingMethodConverter->modelToDataObject($rate, $cart), | ||
[], | ||
ShippingMethodInterface::class | ||
); | ||
} | ||
} | ||
|
||
return ['available_shipping_methods' => $methods]; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddressShippingMethods.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,46 @@ | ||
<?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\ShippingMethodsDataProvider; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class CartAddressShippingMethods implements ResolverInterface | ||
{ | ||
/** | ||
* @var ShippingMethodsDataProvider | ||
*/ | ||
private $shippingMethodsDataProvider; | ||
|
||
/** | ||
* @param ShippingMethodsDataProvider $shippingMethodsDataProvider | ||
*/ | ||
public function __construct( | ||
ShippingMethodsDataProvider $shippingMethodsDataProvider | ||
) { | ||
$this->shippingMethodsDataProvider = $shippingMethodsDataProvider; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
if (!isset($value['model'])) { | ||
throw new LocalizedException(__('"model" values should be specified')); | ||
} | ||
|
||
return $this->shippingMethodsDataProvider->getAvailableShippingMethods($value['model']); | ||
} | ||
} |
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