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

Commit

Permalink
ENGCOM-3456: Setting shipping method for shopping cart (single addres…
Browse files Browse the repository at this point in the history
…s shipping) #211
  • Loading branch information
Valeriy Naida authored Nov 13, 2018
2 parents 5f976fa + b97a008 commit deb8bfa
Show file tree
Hide file tree
Showing 6 changed files with 462 additions and 1 deletion.
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 app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingMethodOnCart.php
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 app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddresses.php
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?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\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\QuoteGraphQl\Model\Cart\SetShippingMethodOnCart;

/**
* Class SetShippingMethodsOnCart
*
* Mutation resolver for setting shipping methods for shopping cart
*/
class SetShippingMethodsOnCart implements ResolverInterface
{
/**
* @var SetShippingMethodOnCart
*/
private $setShippingMethodOnCart;

/**
* @var ArrayManager
*/
private $arrayManager;

/**
* @var GetCartForUser
*/
private $getCartForUser;

/**
* @param ArrayManager $arrayManager
* @param GetCartForUser $getCartForUser
* @param SetShippingMethodOnCart $setShippingMethodOnCart
*/
public function __construct(
ArrayManager $arrayManager,
GetCartForUser $getCartForUser,
SetShippingMethodOnCart $setShippingMethodOnCart
) {
$this->arrayManager = $arrayManager;
$this->getCartForUser = $getCartForUser;
$this->setShippingMethodOnCart = $setShippingMethodOnCart;
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$shippingMethods = $this->arrayManager->get('input/shipping_methods', $args);
$maskedCartId = $this->arrayManager->get('input/cart_id', $args);

if (!$maskedCartId) {
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
}
if (!$shippingMethods) {
throw new GraphQlInputException(__('Required parameter "shipping_methods" is missing'));
}

$shippingMethod = reset($shippingMethods); // This point can be extended for multishipping

if (!$shippingMethod['cart_address_id']) {
throw new GraphQlInputException(__('Required parameter "cart_address_id" is missing'));
}
if (!$shippingMethod['shipping_carrier_code']) {
throw new GraphQlInputException(__('Required parameter "shipping_carrier_code" is missing'));
}
if (!$shippingMethod['shipping_method_code']) {
throw new GraphQlInputException(__('Required parameter "shipping_method_code" is missing'));
}

$userId = $context->getUserId();
$cart = $this->getCartForUser->execute((string) $maskedCartId, $userId);

$this->setShippingMethodOnCart->execute(
$cart,
$shippingMethod['cart_address_id'],
$shippingMethod['shipping_carrier_code'],
$shippingMethod['shipping_method_code']
);

return [
'cart' => [
'cart_id' => $maskedCartId,
'model' => $cart
]
];
}
}
1 change: 1 addition & 0 deletions app/code/Magento/QuoteGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"php": "~7.1.3||~7.2.0",
"magento/framework": "*",
"magento/module-quote": "*",
"magento/module-checkout": "*",
"magento/module-catalog": "*",
"magento/module-store": "*"
},
Expand Down
Loading

0 comments on commit deb8bfa

Please sign in to comment.