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

GraphQL 296 set billing address #321

Merged
merged 6 commits into from
Feb 12, 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
@@ -0,0 +1,72 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart\Address;

use Magento\Framework\Api\ExtensibleDataObjectConverter;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\QuoteGraphQl\Model\Cart\Address\Mapper\Address;

/**
* Collect and return information about a billing address
*/
class BillingAddressDataProvider
{
/**
* @var CartRepositoryInterface
*/
private $cartRepository;

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

/**
* @var ExtensibleDataObjectConverter
*/
private $dataObjectConverter;

/**
* AddressDataProvider constructor.
*
* @param CartRepositoryInterface $cartRepository
* @param Address $addressMapper
* @param ExtensibleDataObjectConverter $dataObjectConverter
*/
public function __construct(
CartRepositoryInterface $cartRepository,
Address $addressMapper,
ExtensibleDataObjectConverter $dataObjectConverter
) {
$this->cartRepository = $cartRepository;
$this->addressMapper = $addressMapper;
$this->dataObjectConverter = $dataObjectConverter;
}

/**
* Collect and return information about a billing addresses
*
* @param CartInterface $cart
* @return null|array
*/
public function getCartAddresses(CartInterface $cart): ?array
{
$cart = $this->cartRepository->get($cart->getId());
$billingAddress = $cart->getBillingAddress();

if (!$billingAddress) {
return null;
}
$billingData = $this->dataObjectConverter->toFlatArray($billingAddress, [], AddressInterface::class);
$addressData = array_merge($billingData, $this->addressMapper->toNestedArray($billingAddress));

return $addressData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* @author Atwix Team
* @copyright Copyright (c) 2018 Atwix (https://www.atwix.com/)
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart\Address\Mapper;

use Magento\Quote\Model\Quote\Address as QuoteAddress;

/**
* Class Address
*
* Extract the necessary address fields from an Address model
*/
class Address
{

/**
* Converts Address model data to nested array
*
* @param QuoteAddress $address
* @return array
*/
public function toNestedArray(QuoteAddress $address): array
{
$addressData = [
'country' => [
'code' => $address->getCountryId(),
'label' => $address->getCountry()
],
'region' => [
'code' => $address->getRegionCode(),
'label' => $address->getRegion()
],
'street' => $address->getStreet(),
'selected_shipping_method' => [
'code' => $address->getShippingMethod(),
'label' => $address->getShippingDescription(),
'free_shipping' => $address->getFreeShipping(),
],
'items_weight' => $address->getWeight(),
'customer_notes' => $address->getCustomerNotes()
];

if (!$address->hasItems()) {
return $addressData;
}

$addressItemsData = [];
foreach ($address->getAllItems() as $addressItem) {
$addressItemsData[] = [
'cart_item_id' => $addressItem->getQuoteItemId(),
'quantity' => $addressItem->getQty()
];
}
$addressData['cart_items'] = $addressItemsData;

return $addressData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart\Address;

use Magento\Framework\Api\ExtensibleDataObjectConverter;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\QuoteGraphQl\Model\Cart\Address\Mapper\Address;

/**
* Class AddressDataProvider
*
* Collect and return information about cart shipping and billing addresses
*/
class ShippingAddressesDataProvider
{
/**
* @var ExtensibleDataObjectConverter
*/
private $dataObjectConverter;

/**
* @var CartRepositoryInterface
*/
private $cartRepository;

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

/**
* AddressDataProvider constructor.
*
* @param ExtensibleDataObjectConverter $dataObjectConverter
* @param CartRepositoryInterface $cartRepository
* @param Address $addressMapper
*/
public function __construct(
ExtensibleDataObjectConverter $dataObjectConverter,
CartRepositoryInterface $cartRepository,
Address $addressMapper
) {
$this->dataObjectConverter = $dataObjectConverter;
$this->cartRepository = $cartRepository;
$this->addressMapper = $addressMapper;
}

/**
* Collect and return information about shipping addresses
*
* @param CartInterface $cart
* @return array
*/
public function getCartAddresses(CartInterface $cart): array
{
$cart = $this->cartRepository->get($cart->getId());
$addressData = [];
$shippingAddresses = $cart->getAllShippingAddresses();

if ($shippingAddresses) {
foreach ($shippingAddresses as $shippingAddress) {
$shippingData = $this->dataObjectConverter->toFlatArray($shippingAddress, [], AddressInterface::class);
$addressData[] = array_merge($shippingData, $this->addressMapper->toNestedArray($shippingAddress));
}
}

return $addressData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?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\Data\AddressInterface;
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
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\Api\BillingAddressManagementInterface;
use Magento\Customer\Api\AddressRepositoryInterface;

/**
* Set billing address for a specified shopping cart
*/
class SetBillingAddressOnCart
{
/**
* @var BillingAddressManagementInterface
*/
private $billingAddressManagement;

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

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

/**
* @var CheckCustomerAccount
*/
private $checkCustomerAccount;

/**
* @param BillingAddressManagementInterface $billingAddressManagement
* @param AddressRepositoryInterface $addressRepository
* @param Address $addressModel
* @param CheckCustomerAccount $checkCustomerAccount
*/
public function __construct(
BillingAddressManagementInterface $billingAddressManagement,
AddressRepositoryInterface $addressRepository,
Address $addressModel,
CheckCustomerAccount $checkCustomerAccount
) {
$this->billingAddressManagement = $billingAddressManagement;
$this->addressRepository = $addressRepository;
$this->addressModel = $addressModel;
$this->checkCustomerAccount = $checkCustomerAccount;
}

/**
* @inheritdoc
*/
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddress): void
{
$customerAddressId = $billingAddress['customer_address_id'] ?? null;
$addressInput = $billingAddress['address'] ?? null;
$useForShipping = $billingAddress['use_for_shipping'] ?? false;

if (null === $customerAddressId && null === $addressInput) {
throw new GraphQlInputException(
__('The billing address must contain either "customer_address_id" or "address".')
);
}
if ($customerAddressId && $addressInput) {
throw new GraphQlInputException(
__('The billing address cannot contain "customer_address_id" and "address" at the same time.')
);
}
$addresses = $cart->getAllShippingAddresses();
if ($useForShipping && count($addresses) > 1) {
throw new GraphQlInputException(
__('Using the "use_for_shipping" option with multishipping is not possible.')
);
}
if (null === $customerAddressId) {
$billingAddress = $this->addressModel->addData($addressInput);
} else {
$this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType());

/** @var AddressInterface $customerAddress */
$customerAddress = $this->addressRepository->getById($customerAddressId);
$billingAddress = $this->addressModel->importCustomerAddressData($customerAddress);
}

$this->billingAddressManagement->assign($cart->getId(), $billingAddress, $useForShipping);
}
}
48 changes: 48 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/BillingAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\QuoteGraphQl\Model\Cart\Address\BillingAddressDataProvider;

/**
* @inheritdoc
*/
class BillingAddress implements ResolverInterface
{
/**
* @var BillingAddressDataProvider
*/
private $addressDataProvider;

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

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($value['model'])) {
throw new LocalizedException(__('"model" value should be specified'));
}

$cart = $value['model'];

return $this->addressDataProvider->getCartAddresses($cart);
}
}
Loading