Skip to content

Commit

Permalink
GraphQL-418: [Shipping methods] Set Shipping Methods on Cart
Browse files Browse the repository at this point in the history
  • Loading branch information
naydav committed Mar 1, 2019
1 parent f3c9247 commit 0ef7711
Show file tree
Hide file tree
Showing 21 changed files with 392 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public function execute(
try {
$this->billingAddressManagement->assign($cart->getId(), $billingAddress, $useForShipping);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
throw new GraphQlInputException(__($e->getMessage()), $e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public function execute(
try {
$this->shippingAddressManagement->assign($cart->getId(), $shippingAddress);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
throw new GraphQlInputException(__($e->getMessage()), $e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Checkout\Api\Data\ShippingInformationInterface;
use Magento\Checkout\Api\Data\ShippingInformationInterfaceFactory;
use Magento\Checkout\Api\ShippingInformationManagementInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote\Address as QuoteAddress;

/**
* Assign shipping method to cart
*/
class AssignShippingMethodToCart
{
/**
* @var ShippingInformationInterfaceFactory
*/
private $shippingInformationFactory;

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

/**
* @param ShippingInformationInterfaceFactory $shippingInformationFactory
* @param ShippingInformationManagementInterface $shippingInformationManagement
*/
public function __construct(
ShippingInformationInterfaceFactory $shippingInformationFactory,
ShippingInformationManagementInterface $shippingInformationManagement
) {
$this->shippingInformationFactory = $shippingInformationFactory;
$this->shippingInformationManagement = $shippingInformationManagement;
}

/**
* Assign shipping method to cart
*
* @param CartInterface $cart
* @param QuoteAddress $quoteAddress
* @param string $carrierCode
* @param string $methodCode
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
public function execute(
CartInterface $cart,
QuoteAddress $quoteAddress,
string $carrierCode,
string $methodCode
): void {
/** @var ShippingInformationInterface $shippingInformation */
$shippingInformation = $this->shippingInformationFactory->create([
'data' => [
/* 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) */
ShippingInformationInterface::SHIPPING_ADDRESS => $quoteAddress,
ShippingInformationInterface::SHIPPING_CARRIER_CODE => $carrierCode,
ShippingInformationInterface::SHIPPING_METHOD_CODE => $methodCode,
],
]);

try {
$this->shippingInformationManagement->saveAddressInformation($cart->getId(), $shippingInformation);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()), $e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ public function execute(QuoteAddress $address): array
$addressData = $this->dataObjectConverter->toFlatArray($address, [], AddressInterface::class);
$addressData['model'] = $address;

if ($address->getShippingMethod()) {
list($carrierCode, $methodCode) = explode('_', $address->getShippingMethod(), 2);
$shippingAmount = $address->getShippingAmount();
}

$addressData = array_merge($addressData, [
'address_id' => $address->getId(),
'country' => [
Expand All @@ -56,12 +51,6 @@ public function execute(QuoteAddress $address): array
'label' => $address->getRegion()
],
'street' => $address->getStreet(),
'selected_shipping_method' => [
'carrier_code' => $carrierCode ?? null,
'method_code' => $methodCode ?? null,
'label' => $address->getShippingDescription(),
'amount' => $shippingAmount ?? null
],
'items_weight' => $address->getWeight(),
'customer_notes' => $address->getCustomerNotes()
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;

/**
* Get customer address. Throws exception if customer is not owner of address
* Get customer address
*/
class GetCustomerAddress
{
Expand Down Expand Up @@ -52,7 +52,7 @@ public function execute(int $addressId, int $customerId): AddressInterface
__('Could not find a address with ID "%address_id"', ['address_id' => $addressId])
);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
throw new GraphQlInputException(__($e->getMessage()), $e);
}

if ((int)$customerAddress->getCustomerId() !== $customerId) {
Expand Down
88 changes: 88 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Cart/GetQuoteAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Model\Quote\Address as QuoteAddress;
use Magento\Quote\Model\Quote\AddressFactory as QuoteAddressFactory;
use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource;


/**
* Get quote address
*/
class GetQuoteAddress
{
/**
* @var QuoteAddressFactory
*/
private $quoteAddressFactory;

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

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

/**
* @param AddressRepositoryInterface $addressRepository
*/
public function __construct(AddressRepositoryInterface $addressRepository)
{
$this->addressRepository = $addressRepository;
}

/**
* Get quote address
*
* @param int $quoteAddressId
* @param int|null $customerId
* @return AddressInterface
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
* @throws GraphQlAuthorizationException
*/
public function execute(int $quoteAddressId, ?int $customerId): QuoteAddress
{
$quoteAddress = $this->quoteAddressFactory->create();

$this->quoteAddressResource->load($quoteAddress, $quoteAddressId);
if (null === $quoteAddress->getId()) {
throw new GraphQlNoSuchEntityException(
__('Could not find a cart address with ID "%cart_address_id"', ['cart_address_id' => $quoteAddressId])
);
}

$quoteAddressCustomerId = (int)$quoteAddress->getCustomerId();

/* Guest cart, allow operations */
if (!$quoteAddressCustomerId && null === $customerId) {
return $quoteAddress;
}

if ($quoteAddressCustomerId !== $customerId) {
throw new GraphQlAuthorizationException(
__(
'The current user cannot use cart address with ID "%cart_address_id"',
['cart_address_id' => $quoteAddressId]
)
);
}
return $quoteAddress;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function createBasedOnCustomerAddress(CustomerAddress $customerAddress):
try {
$quoteAddress->importCustomerAddressData($customerAddress);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
throw new GraphQlInputException(__($e->getMessage()), $e);
}
return $quoteAddress;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ public function __construct(
*
* @param ContextInterface $context
* @param CartInterface $cart
* @param array $billingAddress
* @param array $billingAddressInput
* @return void
* @throws GraphQlInputException
* @throws GraphQlAuthenticationException
* @throws GraphQlAuthorizationException
* @throws GraphQlNoSuchEntityException
*/
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddress): void
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddressInput): void
{
$customerAddressId = $billingAddress['customer_address_id'] ?? null;
$addressInput = $billingAddress['address'] ?? null;
$useForShipping = isset($billingAddress['use_for_shipping'])
? (bool)$billingAddress['use_for_shipping'] : false;
$customerAddressId = $billingAddressInput['customer_address_id'] ?? null;
$addressInput = $billingAddressInput['address'] ?? null;
$useForShipping = isset($billingAddressInput['use_for_shipping'])
? (bool)$billingAddressInput['use_for_shipping'] : false;

if (null === $customerAddressId && null === $addressInput) {
throw new GraphQlInputException(
Expand All @@ -97,13 +97,13 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b
}

if (null === $customerAddressId) {
$billingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
$billingAddressInput = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
} else {
$this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType());
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$context->getUserId());
$billingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress);
$billingAddressInput = $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress);
}

$this->assignBillingAddressToCart->execute($cart, $billingAddress, $useForShipping);
$this->assignBillingAddressToCart->execute($cart, $billingAddressInput, $useForShipping);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Set single shipping address for a specified shopping cart
*/
class SetShippingAddressOnCart implements SetShippingAddressesOnCartInterface
class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface
{
/**
* @var QuoteAddressFactory
Expand Down Expand Up @@ -58,16 +58,16 @@ public function __construct(
/**
* @inheritdoc
*/
public function execute(ContextInterface $context, CartInterface $cart, array $shippingAddresses): void
public function execute(ContextInterface $context, CartInterface $cart, array $shippingAddressesInput): void
{
if (count($shippingAddresses) > 1) {
if (count($shippingAddressesInput) > 1) {
throw new GraphQlInputException(
__('You cannot specify multiple shipping addresses.')
);
}
$shippingAddress = current($shippingAddresses);
$customerAddressId = $shippingAddress['customer_address_id'] ?? null;
$addressInput = $shippingAddress['address'] ?? null;
$shippingAddressInput = current($shippingAddressesInput);
$customerAddressId = $shippingAddressInput['customer_address_id'] ?? null;
$addressInput = $shippingAddressInput['address'] ?? null;

if (null === $customerAddressId && null === $addressInput) {
throw new GraphQlInputException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ interface SetShippingAddressesOnCartInterface
*
* @param ContextInterface $context
* @param CartInterface $cart
* @param array $shippingAddresses
* @param array $shippingAddressesInput
* @return void
* @throws GraphQlInputException
* @throws GraphQlAuthorizationException
* @throws GraphQlAuthenticationException
* @throws GraphQlNoSuchEntityException
*/
public function execute(ContextInterface $context, CartInterface $cart, array $shippingAddresses): void;
public function execute(ContextInterface $context, CartInterface $cart, array $shippingAddressesInput): void;
}
Loading

0 comments on commit 0ef7711

Please sign in to comment.