Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Commit

Permalink
Separate resolver for address shipping methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pmclain committed Feb 6, 2019
1 parent 4b84afb commit 2f7237a
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ public function __construct(
* Collect and return information about shipping and billing addresses
*
* @param CartInterface $cart
* @param bool $includeShippingMethods
* @return array
*/
public function getCartAddresses(CartInterface $cart, $includeShippingMethods = false): array
public function getCartAddresses(CartInterface $cart): array
{
$addressData = [];
$shippingAddress = $cart->getShippingAddress();
Expand All @@ -61,12 +60,6 @@ public function getCartAddresses(CartInterface $cart, $includeShippingMethods =
if ($shippingAddress) {
$shippingData = $this->dataObjectConverter->toFlatArray($shippingAddress, [], AddressInterface::class);
$shippingData['address_type'] = 'SHIPPING';
if ($includeShippingMethods) {
$shippingData['available_shipping_methods'] = $this->extractAvailableShippingRateData(
$cart,
$shippingAddress
);
}
$addressData[] = array_merge($shippingData, $this->extractAddressData($shippingAddress));
}

Expand All @@ -88,6 +81,7 @@ public function getCartAddresses(CartInterface $cart, $includeShippingMethods =
private function extractAddressData(QuoteAddress $address): array
{
$addressData = [
'model' => $address,
'country' => [
'code' => $address->getCountryId(),
'label' => $address->getCountry()
Expand Down
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];
}
}
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']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value

$cart = $value['model'];

return $this->addressDataProvider->getCartAddresses($cart, $this->includeShippingMethods($info));
}

private function includeShippingMethods(ResolveInfo $info): bool
{
return $info->getFieldSelection()['available_shipping_methods'] ?? false;
return $this->addressDataProvider->getCartAddresses($cart);
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ type CartAddress {
telephone: String
address_type: AdressTypeEnum
selected_shipping_method: CheckoutShippingMethod
available_shipping_methods: [CheckoutAvailableShippingMethod]
available_shipping_methods: [CheckoutAvailableShippingMethod] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartAddressShippingMethods")
items_weight: Float
customer_notes: String
cart_items: [CartItemQuantity]
Expand Down

0 comments on commit 2f7237a

Please sign in to comment.