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

Commit

Permalink
Working concept for setting shipping address for a shopping cart
Browse files Browse the repository at this point in the history
  • Loading branch information
rogyar committed Oct 6, 2018
1 parent 2467233 commit 87cfb71
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

namespace Magento\QuoteGraphQl\Model\Resolver\ShippingMethod;

use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Checkout\Api\ShippingInformationManagementInterface;
use Magento\Checkout\Model\ShippingInformation;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;
Expand All @@ -19,8 +20,10 @@
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
use Magento\Quote\Model\ShippingMethodManagementInterface;
use Magento\QuoteGraphQl\Model\Authorization\IsCartMutationAllowedForCurrentUser;
use Magento\Quote\Model\Quote\AddressFactory as QuoteAddressFactory;
use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource;
use Magento\Checkout\Model\ShippingInformationFactory;

/**
* Class SetShippingMethodsOnCart
Expand All @@ -29,41 +32,64 @@
*/
class SetShippingMethodsOnCart implements ResolverInterface
{
/**
* @var ShippingInformationFactory
*/
private $shippingInformationFactory;

/**
* @var QuoteAddressFactory
*/
private $quoteAddressFactory;

/**
* @var QuoteAddressResource
*/
private $quoteAddressResource;

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

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

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

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

/**
* SetShippingMethodsOnCart constructor.
* @param ArrayManager $arrayManager
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
* @param ShippingMethodManagementInterface $shippingMethodManagement
* @param IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser
*/
public function __construct(
ArrayManager $arrayManager,
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
ShippingMethodManagementInterface $shippingMethodManagement,
IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser
IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser,
ShippingInformationManagementInterface $shippingInformationManagement,
QuoteAddressFactory $quoteAddressFacrory,
QuoteAddressResource $quoteAddressResource,
ShippingInformationFactory $shippingInformationFactory
) {
$this->arrayManager = $arrayManager;
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
$this->shippingMethodManagement = $shippingMethodManagement;
$this->isCartMutationAllowedForCurrentUser = $isCartMutationAllowedForCurrentUser;
$this->shippingInformationManagement = $shippingInformationManagement;

$this->quoteAddressResource = $quoteAddressResource;
$this->quoteAddressFactory = $quoteAddressFacrory;
$this->shippingInformationFactory = $shippingInformationFactory;
}

/**
Expand All @@ -77,17 +103,18 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
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); // TODO: provide implementation for multishipping

if (!$shippingMethod['cart_address_id']) {
throw new GraphQlInputException(__('Required parameter "cart_address_id" is missing'));
}
if (!$shippingMethod['shipping_carrier_code']) { // FIXME: check the E_WARNING here
throw new GraphQlInputException(__('Required parameter "shipping_carrier_code" is missing'));
}

if (!$shippingMethod['shipping_method_code']) { // FIXME: check the E_WARNING here
throw new GraphQlInputException(__('Required parameter "shipping_method_code" is missing'));
}
Expand All @@ -109,22 +136,28 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
);
}

$quoteAddress = $this->quoteAddressFactory->create();
$this->quoteAddressResource->load($quoteAddress, $shippingMethod['cart_address_id']);

/** @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($shippingMethod['shipping_carrier_code']);
$shippingInformation->setShippingMethodCode($shippingMethod['shipping_method_code']);

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()));
$this->shippingInformationManagement->saveAddressInformation($cartId, $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()));
}

return 'Success!'; // TODO we should return cart here
}
}
}
8 changes: 4 additions & 4 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ type Query {
}

type Mutation {
createEmptyCart: String @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\Cart\\CreateEmptyCart") @doc(description:"Creates empty shopping cart for guest or logged in user")
applyCouponToCart(input: ApplyCouponToCartInput): ApplyCouponToCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\Coupon\\ApplyCouponToCart")
removeCouponFromCart(input: RemoveCouponFromCartInput): RemoveCouponFromCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\Coupon\\RemoveCouponFromCart")
createEmptyCart: String @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CreateEmptyCart") @doc(description:"Creates empty shopping cart for guest or logged in user")
applyCouponToCart(input: ApplyCouponToCartInput): ApplyCouponToCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ApplyCouponToCart")
removeCouponFromCart(input: RemoveCouponFromCartInput): RemoveCouponFromCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\RemoveCouponFromCart")
setShippingAddressesOnCart(input: SetShippingAddressesOnCartInput): SetShippingAddressesOnCartOutput
setBillingAddressOnCart(input: SetBillingAddressOnCartInput): SetBillingAddressOnCartOutput
setShippingMethodsOnCart(input: SetShippingMethodsOnCartInput): SetShippingMethodsOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingMethod\\SetShippingMethodsOnCart")
Expand Down Expand Up @@ -53,7 +53,7 @@ input SetShippingMethodsOnCartInput {
}

input ShippingMethodForAddressInput {
cart_address_id: int
cart_address_id: Int!
shipping_carrier_code: String!
shipping_method_code: String!
}
Expand Down

0 comments on commit 87cfb71

Please sign in to comment.