-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔃 [EngCom] Public Pull Requests - GraphQL
Accepted Public Pull Requests:
- Loading branch information
Showing
17 changed files
with
995 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
app/code/Magento/QuoteGraphQl/Model/Cart/Address/BillingAddressDataProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
app/code/Magento/QuoteGraphQl/Model/Cart/Address/Mapper/Address.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
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; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
app/code/Magento/QuoteGraphQl/Model/Cart/Address/ShippingAddressesDataProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.