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

graphQl-509: save_in_address_book has no impact on Address Book #873

Merged
merged 15 commits into from
Oct 13, 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,96 @@
<?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\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Customer\Api\Data\AddressInterfaceFactory;
use Magento\Customer\Api\Data\RegionInterface;
use Magento\Customer\Api\Data\RegionInterfaceFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Quote\Model\Quote\Address as QuoteAddress;

/**
* Save Address to Customer Address Book.
*/
class SaveQuoteAddressToCustomerAddressBook
{
/**
* @var AddressInterfaceFactory
*/
private $addressFactory;

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

/**
* @var RegionInterfaceFactory
*/
private $regionFactory;

/**
* @param AddressInterfaceFactory $addressFactory
* @param AddressRepositoryInterface $addressRepository
* @param RegionInterfaceFactory $regionFactory
*/
public function __construct(
AddressInterfaceFactory $addressFactory,
AddressRepositoryInterface $addressRepository,
RegionInterfaceFactory $regionFactory
) {
$this->addressFactory = $addressFactory;
$this->addressRepository = $addressRepository;
$this->regionFactory = $regionFactory;
}

/**
* Save Address to Customer Address Book.
*
* @param QuoteAddress $quoteAddress
* @param int $customerId
*
* @return void
* @throws GraphQlInputException
*/
public function execute(QuoteAddress $quoteAddress, int $customerId): void
{
try {
/** @var AddressInterface $customerAddress */
$customerAddress = $this->addressFactory->create();
$customerAddress->setFirstname($quoteAddress->getFirstname())
->setLastname($quoteAddress->getLastname())
->setMiddlename($quoteAddress->getMiddlename())
->setPrefix($quoteAddress->getPrefix())
->setSuffix($quoteAddress->getSuffix())
->setVatId($quoteAddress->getVatId())
->setCountryId($quoteAddress->getCountryId())
->setCompany($quoteAddress->getCompany())
->setRegionId($quoteAddress->getRegionId())
->setFax($quoteAddress->getFax())
->setCity($quoteAddress->getCity())
->setPostcode($quoteAddress->getPostcode())
->setStreet($quoteAddress->getStreet())
->setTelephone($quoteAddress->getTelephone())
->setCustomerId($customerId);

/** @var RegionInterface $region */
$region = $this->regionFactory->create();
$region->setRegionCode($quoteAddress->getRegionCode())
->setRegion($quoteAddress->getRegion())
->setRegionId($quoteAddress->getRegionId());
$customerAddress->setRegion($region);

$this->addressRepository->save($customerAddress);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()), $e);
}
}
}
46 changes: 43 additions & 3 deletions app/code/Magento/QuoteGraphQl/Model/Cart/GetShippingAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Model\Quote\Address;
use Magento\QuoteGraphQl\Model\Cart\Address\SaveQuoteAddressToCustomerAddressBook;

/**
* Get shipping address
Expand All @@ -23,12 +24,21 @@ class GetShippingAddress
*/
private $quoteAddressFactory;

/**
* @var SaveQuoteAddressToCustomerAddressBook
*/
private $saveQuoteAddressToCustomerAddressBook;

/**
* @param QuoteAddressFactory $quoteAddressFactory
* @param SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
*/
public function __construct(QuoteAddressFactory $quoteAddressFactory)
{
public function __construct(
QuoteAddressFactory $quoteAddressFactory,
SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
) {
$this->quoteAddressFactory = $quoteAddressFactory;
$this->saveQuoteAddressToCustomerAddressBook = $saveQuoteAddressToCustomerAddressBook;
}

/**
Expand Down Expand Up @@ -62,16 +72,46 @@ public function execute(ContextInterface $context, array $shippingAddressInput):
);
}

$shippingAddress = $this->createShippingAddress($context, $customerAddressId, $addressInput);

return $shippingAddress;
}

/**
* Create shipping address.
*
* @param ContextInterface $context
* @param int|null $customerAddressId
* @param array|null $addressInput
*
* @return \Magento\Quote\Model\Quote\Address
* @throws GraphQlAuthorizationException
*/
private function createShippingAddress(
ContextInterface $context,
?int $customerAddressId,
?array $addressInput
) {
$customerId = $context->getUserId();

if (null === $customerAddressId) {
$shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);

// need to save address only for registered user and if save_in_address_book = true
if (0 !== $customerId
&& isset($addressInput['save_in_address_book'])
&& (bool)$addressInput['save_in_address_book'] === true
) {
$this->saveQuoteAddressToCustomerAddressBook->execute($shippingAddress, $customerId);
}
} else {
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

$shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress(
(int)$customerAddressId,
$context->getUserId()
$customerId
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\GraphQl\Model\Query\ContextInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote\Address;
use Magento\QuoteGraphQl\Model\Cart\Address\SaveQuoteAddressToCustomerAddressBook;

/**
* Set billing address for a specified shopping cart
Expand All @@ -29,16 +30,24 @@ class SetBillingAddressOnCart
*/
private $assignBillingAddressToCart;

/**
* @var SaveQuoteAddressToCustomerAddressBook
*/
private $saveQuoteAddressToCustomerAddressBook;

/**
* @param QuoteAddressFactory $quoteAddressFactory
* @param AssignBillingAddressToCart $assignBillingAddressToCart
* @param SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
*/
public function __construct(
QuoteAddressFactory $quoteAddressFactory,
AssignBillingAddressToCart $assignBillingAddressToCart
AssignBillingAddressToCart $assignBillingAddressToCart,
SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
) {
$this->quoteAddressFactory = $quoteAddressFactory;
$this->assignBillingAddressToCart = $assignBillingAddressToCart;
$this->saveQuoteAddressToCustomerAddressBook = $saveQuoteAddressToCustomerAddressBook;
}

/**
Expand Down Expand Up @@ -101,6 +110,15 @@ private function createBillingAddress(
): Address {
if (null === $customerAddressId) {
$billingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);

$customerId = $context->getUserId();
// need to save address only for registered user and if save_in_address_book = true
if (0 !== $customerId
&& isset($addressInput['save_in_address_book'])
&& (bool)$addressInput['save_in_address_book'] === true
) {
$this->saveQuoteAddressToCustomerAddressBook->execute($billingAddress, $customerId);
}
} else {
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
Expand All @@ -111,6 +129,7 @@ private function createBillingAddress(
(int)$context->getUserId()
);
}

return $billingAddress;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\GraphQl\Model\Query\ContextInterface;
use Magento\Quote\Api\Data\CartInterface;
Expand All @@ -21,6 +20,7 @@ class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface
* @var AssignShippingAddressToCart
*/
private $assignShippingAddressToCart;

/**
* @var GetShippingAddress
*/
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ input CartAddressInput {
postcode: String
country_code: String!
telephone: String!
save_in_address_book: Boolean!
save_in_address_book: Boolean
}

input SetShippingMethodsOnCartInput {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ private function setBillingAddress(string $cartId): void
telephone: "88776655"
region: "TX"
country_code: "US"
save_in_address_book: false
}
}
}
Expand Down Expand Up @@ -298,7 +297,6 @@ private function setShippingAddress(string $cartId): array
postcode: "887766"
country_code: "US"
telephone: "88776655"
save_in_address_book: false
}
}
]
Expand Down
Loading