Skip to content

Commit

Permalink
Concept for setting shipping method only for single address checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
rogyar committed Oct 3, 2018
1 parent 0b86e17 commit 2467233
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,63 @@

namespace Magento\QuoteGraphQl\Model\Resolver\ShippingMethod;

use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
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\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
use Magento\Quote\Model\ShippingMethodManagementInterface;
use Magento\QuoteGraphQl\Model\Authorization\IsCartMutationAllowedForCurrentUser;

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

/**
* @var MaskedQuoteIdToQuoteIdInterface
*/
private $maskedQuoteIdToQuoteId;

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

/**
* @var ShippingMethodManagementInterface
*/
private $shippingMethodManagement;

/**
* @var IsCartMutationAllowedForCurrentUser
*/
private $isCartMutationAllowedForCurrentUser;

/**
* SetShippingMethodsOnCart constructor.
* @param ArrayManager $arrayManager
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
* @param ShippingMethodManagementInterface $shippingMethodManagement
*/
public function __construct(
ArrayManager $arrayManager,
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
CartRepositoryInterface $cartRepository
ShippingMethodManagementInterface $shippingMethodManagement,
IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser
) {
$this->arrayManager = $arrayManager;
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
$this->cartRepository = $cartRepository;
$this->shippingMethodManagement = $shippingMethodManagement;
$this->isCartMutationAllowedForCurrentUser = $isCartMutationAllowedForCurrentUser;
}

/**
Expand All @@ -56,50 +75,56 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
$maskedCartId = $this->arrayManager->get('input/cart_id', $args);

if (!$maskedCartId) {
// TODO: throw an exception
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
}

if (!$shippingMethods) {
// TODO: throw an exception?
throw new GraphQlInputException(__('Required parameter "shipping_methods" is missing'));
}

foreach ($shippingMethods as $shippingMethod) {
if (empty($shippingMethod['cart_address_id'])) {
// TODO: throw input exception
}

if (empty($shippingMethod['shipping_method_code'])) {
// TODO: throw input exception
}
$shippingMethod = reset($shippingMethods); // TODO: provide implementation for multishipping

// TODO: move to a separate class
// TODO: check current customer can apply operations on specified cart
if (!$shippingMethod['shipping_carrier_code']) { // FIXME: check the E_WARNING here
throw new GraphQlInputException(__('Required parameter "shipping_carrier_code" is missing'));
}

$quoteId = $this->maskedQuoteIdToQuoteId->execute($maskedCartId);
$quote = $this->cartRepository->get($quoteId); // TODO: catch no such entity exception
$this->setShippingMethods($shippingMethods, $quote);
if (!$shippingMethod['shipping_method_code']) { // FIXME: check the E_WARNING here
throw new GraphQlInputException(__('Required parameter "shipping_method_code" is missing'));
}

$quote->collectTotals();
$quote->save();
//$this->cartRepository->save($quote);
try {
$cartId = $this->maskedQuoteIdToQuoteId->execute((string) $maskedCartId);
} catch (NoSuchEntityException $exception) {
throw new GraphQlNoSuchEntityException(
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $maskedCartId])
);
}

return 'Success!';
}
if (false === $this->isCartMutationAllowedForCurrentUser->execute($cartId)) {
throw new GraphQlAuthorizationException(
__(
'The current user cannot perform operations on cart "%masked_cart_id"',
['masked_cart_id' => $maskedCartId]
)
);
}

private function setShippingMethods($shippingMethods, CartInterface $quote)
{
$addresses = $quote->getAllShippingAddresses();
/** @var \Magento\Quote\Model\Quote\Address $address */
foreach ($addresses as $address) {
$addressId = $address->getId();
$shippingMethodForAddress = array_search($addressId, array_column($shippingMethods, 'cart_address_id'));
if ($shippingMethodForAddress !== false) {
$address->setShippingMethod($shippingMethods[$shippingMethodForAddress]['shipping_method_code']);
// $address->setCollectShippingRates(1);
$address->save();
}
try {
$this->shippingMethodManagement->set(
$cartId,
$shippingMethods['shipping_carrier_code'],
$shippingMethods['shipping_method_code']
);
} catch (InputException $exception) {
throw new GraphQlInputException(__($exception->getMessage()));
} catch (CouldNotSaveException $exception) {
throw new GraphQlInputException(__($exception->getMessage()));
} catch (StateException $exception) {
throw new GraphQlInputException(__($exception->getMessage()));
} catch (NoSuchEntityException $exception) {
throw new GraphQlNoSuchEntityException(__($exception->getMessage()));
}
// TODO: make sure that shipping method is assigned for all addresses

return 'Success!'; // TODO we should return cart here
}
}
5 changes: 3 additions & 2 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ input SetShippingMethodsOnCartInput {
}

input ShippingMethodForAddressInput {
cart_address_id: String! # todo: int?
cart_address_id: int
shipping_carrier_code: String!
shipping_method_code: String!
}

Expand All @@ -66,7 +67,7 @@ type SetShippingAddressesOnCartOutput {
}

type SetShippingMethodsOnCartOutput {
cart: String
cart: String #TODO: temp placeholder, should be Cart!
}

# If no address is provided, the system get address assigned to a quote
Expand Down

0 comments on commit 2467233

Please sign in to comment.