Skip to content

Commit

Permalink
🔃 [EngCom] Public Pull Requests - GraphQL
Browse files Browse the repository at this point in the history
Accepted Public Pull Requests:
  • Loading branch information
sidolov authored Feb 12, 2019
2 parents 23bc18f + b3f1945 commit 5276b27
Show file tree
Hide file tree
Showing 17 changed files with 995 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Catalog\Model\Product;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
Expand Down Expand Up @@ -45,12 +46,14 @@ public function resolve(
$data = [];
foreach ($value['options'] as $option) {
$code = $option['attribute_code'];
if (!isset($value['product']['model'][$code])) {
/** @var Product|null $model */
$model = $value['product']['model'] ?? null;
if (!$model || !$model->getData($code)) {
continue;
}

foreach ($option['values'] as $optionValue) {
if ($optionValue['value_index'] != $value['product']['model'][$code]) {
if ($optionValue['value_index'] != $model->getData($code)) {
continue;
}
$data[] = [
Expand Down
30 changes: 30 additions & 0 deletions app/code/Magento/ConfigurableProductGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.
type Mutation {
addConfigurableProductsToCart(input: AddConfigurableProductsToCartInput): AddConfigurableProductsToCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AddSimpleProductsToCart")
}

type ConfigurableProduct implements ProductInterface, PhysicalProductInterface, CustomizableProductInterface @doc(description: "ConfigurableProduct defines basic features of a configurable product and its simple product variants") {
variants: [ConfigurableVariant] @doc(description: "An array of variants of products") @resolver(class: "Magento\\ConfigurableProductGraphQl\\Model\\Resolver\\ConfigurableVariant")
Expand Down Expand Up @@ -35,3 +38,30 @@ type ConfigurableProductOptionsValues @doc(description: "ConfigurableProductOpti
store_label: String @doc(description: "The label of the product on the current store")
use_default_value: Boolean @doc(description: "Indicates whether to use the default_label")
}

input AddConfigurableProductsToCartInput {
cart_id: String!
cartItems: [ConfigurableProductCartItemInput!]!
}

type AddConfigurableProductsToCartOutput {
cart: Cart!
}

input ConfigurableProductCartItemInput {
data: CartItemDetailsInput!
variant_sku: String!
customizable_options:[CustomizableOptionInput!]
}

type ConfigurableCartItem implements CartItemInterface {
customizable_options: [SelectedCustomizableOption]!
configurable_options: [SelectedConfigurableOption!]!
}

type SelectedConfigurableOption {
id: Int!
option_label: String!
value_id: Int!
value_label: String!
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public function __construct(
* @param Quote $cart
* @param array $cartItems
* @throws GraphQlInputException
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException
*/
public function execute(Quote $cart, array $cartItems): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function __construct(
* @return void
* @throws GraphQlNoSuchEntityException
* @throws GraphQlInputException
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function execute(Quote $cart, array $cartItemData): void
{
Expand Down
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,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;
}
}
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);
}
}
Loading

0 comments on commit 5276b27

Please sign in to comment.