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

Base shipping methods support #342

Merged
merged 17 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -11,9 +11,9 @@
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Api\BillingAddressManagementInterface;
use Magento\Quote\Model\Quote\Address as QuoteAddress;

/**
* Set billing address for a specified shopping cart
Expand All @@ -38,22 +38,22 @@ public function __construct(
* Assign billing address to cart
*
* @param CartInterface $cart
* @param QuoteAddress $billingAddress
* @param AddressInterface $billingAddress
* @param bool $useForShipping
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
public function execute(
CartInterface $cart,
QuoteAddress $billingAddress,
AddressInterface $billingAddress,
bool $useForShipping
): void {
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 @@ -11,8 +11,8 @@
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote\Address as QuoteAddress;
use Magento\Quote\Model\ShippingAddressManagementInterface;

/**
Expand All @@ -38,20 +38,20 @@ public function __construct(
* Assign shipping address to cart
*
* @param CartInterface $cart
* @param QuoteAddress $shippingAddress
* @param AddressInterface $shippingAddress
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
public function execute(
CartInterface $cart,
QuoteAddress $shippingAddress
AddressInterface $shippingAddress
): void {
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\AddressInterface;
use Magento\Quote\Api\Data\CartInterface;

/**
* 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 AddressInterface $quoteAddress
* @param string $carrierCode
* @param string $methodCode
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
public function execute(
CartInterface $cart,
AddressInterface $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 @@ -51,12 +51,6 @@ public function execute(QuoteAddress $address): array
'label' => $address->getRegion()
],
'street' => $address->getStreet(),
'selected_shipping_method' => [
'code' => $address->getShippingMethod(),
'label' => $address->getShippingDescription(),
'free_shipping' => $address->getFreeShipping(),
'amount' => $address->getShippingAmount()
],
'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
82 changes: 82 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Cart/GetQuoteAddress.php
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\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\AddressInterfaceFactory;
use Magento\Quote\Model\ResourceModel\Quote\Address as AddressResource;

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

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

/**
* @param AddressInterfaceFactory $quoteAddressFactory
* @param AddressResource $quoteAddressResource
*/
public function __construct(
AddressInterfaceFactory $quoteAddressFactory,
AddressResource $quoteAddressResource
) {
$this->quoteAddressFactory = $quoteAddressFactory;
$this->quoteAddressResource = $quoteAddressResource;
}

/**
* 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): AddressInterface
{
$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 Down
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