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

GraphQl shipping address coverage #192

Merged
merged 21 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e327bb6
graphQl: shipping address concept
Sep 22, 2018
ef8f126
graph-ql: concept of shipping address coverage, added separate flows …
Oct 4, 2018
a0bb0fc
graphQl: updated shipping address coverage content
Nov 1, 2018
c8dbdf4
graphQl: fixed multi shipping model
Nov 2, 2018
bf0aaad
graphQl: merge mainline
Nov 2, 2018
0a6c0b5
graphQl: removed multishipping implementation, fixed namespaces
Nov 2, 2018
90831f5
graphQl: removed not needed exception, added test coverage
Nov 5, 2018
5d2b1c8
graphQl: fixed dependency issue
Nov 5, 2018
e1f09fb
graphQl: added extension point for multishipping
Nov 5, 2018
55b6f4e
graphQl: fixed SetShippingAddressesInterface param
Nov 5, 2018
7c21b3b
graphQl: removed set shipping addresses chain, using di preference in…
Nov 6, 2018
2644b54
Merge branches 'graph-ql-shipping-address-coverage' and 'set-shipping…
Nov 6, 2018
c857857
graphQl: fixed composer json
Nov 6, 2018
240cfb3
graphQl: fixed typos and wrong descriptions
Nov 7, 2018
afd128e
graphQl: removed redundant multishipping references
Nov 13, 2018
d6f393a
Merge branches '2.3-develop' and 'graph-ql-shipping-address-coverage'…
Nov 13, 2018
2b5c1aa
Merge branches '2.3-develop' and 'graph-ql-shipping-address-coverage'…
Nov 16, 2018
313544e
Merge remote-tracking branch 'origin/2.3-develop' into graph-ql-shipp…
Nov 19, 2018
4694c06
GraphQL-39: Manage Shipping methods on Cart
Nov 19, 2018
c498773
GraphQL-39: Manage Shipping methods on Cart
Nov 19, 2018
39225d4
GraphQL-39: Manage Shipping methods on Cart
Nov 19, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Authorization\Model\UserContextInterface;
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote\Address;
use Magento\Quote\Model\ShippingAddressManagementInterface;
use Magento\Customer\Api\AddressRepositoryInterface;

/**
* Set single shipping address for a specified shopping cart
*/
class SetShippingAddressOnCart implements SetShippingAddressesOnCartInterface
{
/**
* @var ShippingAddressManagementInterface
*/
private $shippingAddressManagement;

/**
* @var AddressRepositoryInterface
*/
private $addressRepository;

/**
* @var Address
*/
private $addressModel;

/**
* @param ShippingAddressManagementInterface $shippingAddressManagement
* @param AddressRepositoryInterface $addressRepository
* @param Address $addressModel
*/
public function __construct(
ShippingAddressManagementInterface $shippingAddressManagement,
AddressRepositoryInterface $addressRepository,
Address $addressModel
) {
$this->shippingAddressManagement = $shippingAddressManagement;
$this->addressRepository = $addressRepository;
$this->addressModel = $addressModel;
}

/**
* @inheritdoc
*/
public function execute(ContextInterface $context, CartInterface $cart, array $shippingAddresses): void
{
if (count($shippingAddresses) > 1) {
throw new GraphQlInputException(
__('You cannot specify multiple shipping addresses.')
);
}
$shippingAddress = current($shippingAddresses);
$customerAddressId = $shippingAddress['customer_address_id'] ?? null;
$addressInput = $shippingAddress['address'] ?? null;

if (!$customerAddressId && !$addressInput) {
throw new GraphQlInputException(
__('The shipping address must contain either "customer_address_id" or "address".')
);
}
if ($customerAddressId && $addressInput) {
throw new GraphQlInputException(
__('The shipping address cannot contain "customer_address_id" and "address" at the same time.')
);
}
if ($customerAddressId) {
if ((!$context->getUserId()) || $context->getUserType() == UserContextInterface::USER_TYPE_GUEST) {
throw new GraphQlAuthorizationException(
__(
'Guest users cannot manage addresses.'
)
);
}
/** @var AddressInterface $customerAddress */
$customerAddress = $this->addressRepository->getById($customerAddressId);
$shippingAddress = $this->addressModel->importCustomerAddressData($customerAddress);
} else {
$shippingAddress = $this->addressModel->addData($addressInput);
}

$this->shippingAddressManagement->assign($cart->getId(), $shippingAddress);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Quote\Api\Data\CartInterface;

/**
* Extension point for setting shipping addresses for a specified shopping cart
*
* All objects that are responsible for setting shipping addresses on a cart via GraphQl
*should implement this interface.
*/
interface SetShippingAddressesOnCartInterface
{

/**
* Set shipping addresses for a specified shopping cart
*
* @param ContextInterface $context
* @param CartInterface $cart
* @param array $shippingAddresses
* @return void
* @throws GraphQlInputException
*/
public function execute(ContextInterface $context, CartInterface $cart, array $shippingAddresses): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?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\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
use Magento\Quote\Model\ShippingAddressManagementInterface;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\QuoteGraphQl\Model\Cart\SetShippingAddressesOnCartInterface;

/**
* Class SetShippingAddressesOnCart
*
* Mutation resolver for setting shipping addresses for shopping cart
*/
class SetShippingAddressesOnCart implements ResolverInterface
{
/**
* @var MaskedQuoteIdToQuoteIdInterface
*/
private $maskedQuoteIdToQuoteId;

/**
* @var ShippingAddressManagementInterface
*/
private $shippingAddressManagement;

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

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

/**
* @var SetShippingAddressesOnCartInterface
*/
private $setShippingAddressesOnCart;

/**
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
* @param ShippingAddressManagementInterface $shippingAddressManagement
* @param GetCartForUser $getCartForUser
* @param ArrayManager $arrayManager
* @param SetShippingAddressesOnCartInterface $setShippingAddressesOnCart
*/
public function __construct(
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
ShippingAddressManagementInterface $shippingAddressManagement,
GetCartForUser $getCartForUser,
ArrayManager $arrayManager,
SetShippingAddressesOnCartInterface $setShippingAddressesOnCart
) {
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
$this->shippingAddressManagement = $shippingAddressManagement;
$this->getCartForUser = $getCartForUser;
$this->arrayManager = $arrayManager;
$this->setShippingAddressesOnCart = $setShippingAddressesOnCart;
}

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

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

$maskedCartId = $args['input']['cart_id'];
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());

$this->setShippingAddressesOnCart->execute($context, $cart, $shippingAddresses);

return [
'cart' => [
'cart_id' => $maskedCartId,
'model' => $cart
]
];
}
}
4 changes: 3 additions & 1 deletion app/code/Magento/QuoteGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"magento/module-quote": "*",
"magento/module-checkout": "*",
"magento/module-catalog": "*",
"magento/module-store": "*"
"magento/module-store": "*",
"magento/module-customer": "*",
"magento/module-authorization": "*"
},
"suggest": {
"magento/module-graph-ql": "*"
Expand Down
28 changes: 28 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/graphql/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\QuoteGraphQl\Model\Cart\SetShippingAddressesOnCartInterface"
type="Magento\QuoteGraphQl\Model\Cart\SetShippingAddressOnCart" />
<virtualType name="multishippingPaymentSpecification" type="Magento\Payment\Model\Method\Specification\Composite">
<arguments>
<argument name="specifications" xsi:type="array">
<item name="enabled" xsi:type="string">Magento\Multishipping\Model\Payment\Method\Specification\Enabled</item>
</argument>
</arguments>
</virtualType>
<type name="Magento\Multishipping\Block\Checkout\Billing">
<arguments>
<argument name="paymentSpecification" xsi:type="object">multishippingPaymentSpecification</argument>
</arguments>
</type>
<type name="Magento\Multishipping\Model\Checkout\Type\Multishipping">
<arguments>
<argument name="paymentSpecification" xsi:type="object">multishippingPaymentSpecification</argument>
</arguments>
</type>
</config>
10 changes: 8 additions & 2 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@ type Query {
}

type Mutation {
createEmptyCart: String @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CreateEmptyCart") @doc(description:"Creates empty shopping cart for guest or logged in user")
createEmptyCart: String @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\Cart\\CreateEmptyCart") @doc(description:"Creates an empty shopping cart for a 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")
setShippingAddressesOnCart(input: SetShippingAddressesOnCartInput): SetShippingAddressesOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetShippingAddressesOnCart")
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\\SetShippingMethodsOnCart")
addSimpleProductsToCart(input: AddSimpleProductsToCartInput): AddSimpleProductsToCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AddSimpleProductsToCart")
}

input SetShippingAddressesOnCartInput {
cart_id: String!
shipping_addresses: [ShippingAddressInput!]!
}

input ShippingAddressInput {
customer_address_id: Int # Can be provided in one-page checkout and is required for multi-shipping checkout
address: CartAddressInput
cart_items: [CartItemQuantityInput!]
Expand Down
Loading