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

Commit

Permalink
#438: Adjust adding configurable products to the shopping cart
Browse files Browse the repository at this point in the history
  • Loading branch information
lenaorobei committed Aug 1, 2019
1 parent cea3e79 commit aeb7cc5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@

namespace Magento\ConfigurableProductGraphQl\Model\Cart\BuyRequest;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\QuoteGraphQl\Model\Cart\BuyRequest\BuyRequestDataProviderInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\ConfigurableProductGraphQl\Model\Options\Collection as OptionCollection;

/**
* DataProvider for building super attribute options in buy requests
Expand All @@ -21,26 +25,62 @@ class SuperAttributeDataProvider implements BuyRequestDataProviderInterface
private $arrayManager;

/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @var OptionCollection
*/
private $optionCollection;

/**
* SuperAttributeDataProvider constructor.
* @param ArrayManager $arrayManager
* @param ProductRepositoryInterface $productRepository
* @param OptionCollection $optionCollection
*/
public function __construct(
ArrayManager $arrayManager
ArrayManager $arrayManager,
ProductRepositoryInterface $productRepository,
OptionCollection $optionCollection
) {
$this->arrayManager = $arrayManager;
$this->productRepository = $productRepository;
$this->optionCollection = $optionCollection;
}

/**
* @inheritdoc
*/
public function execute(array $cartItemData): array
{
$superAttributes = $this->arrayManager->get('configurable_attributes', $cartItemData, []);
$parentSku = $this->arrayManager->get('parent_sku', $cartItemData);
if ($parentSku === null) {
return [];
}
$sku = $this->arrayManager->get('data/sku', $cartItemData);

$superAttributesData = [];
foreach ($superAttributes as $superAttribute) {
$superAttributesData[$superAttribute['id']] = $superAttribute['value'];
try {
$parentProduct = $this->productRepository->get($parentSku);
$product = $this->productRepository->get($sku);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__('Could not find specified product.'));
}

$this->optionCollection->addProductId((int)$parentProduct->getId());
$options = $this->optionCollection->getAttributesByProductId((int)$parentProduct->getId());

$superAttributesData = [];
foreach ($options as $option) {
$code = $option['attribute_code'];
foreach ($option['values'] as $optionValue) {
if ($optionValue['value_index'] === $product->getData($code)) {
$superAttributesData[$option['attribute_id']] = $optionValue['value_index'];
break;
}
}
}
return ['super_attribute' => $superAttributesData];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,11 @@ type AddConfigurableProductsToCartOutput {

input ConfigurableProductCartItemInput {
data: CartItemInput!
variant_sku: String @deprecated(reason: "Use CartItemInput.sku instead")
configurable_attributes: [ConfigurableCartItemAttributesInput]!
variant_sku: String @deprecated(reason: "Use CartItemInput.sku instead.")
parent_sku: String! @doc(description: "Configurable product SKU.")
customizable_options:[CustomizableOptionInput!]
}

input ConfigurableCartItemAttributesInput {
id: Int!
value: Int!
}

type ConfigurableCartItem implements CartItemInterface {
customizable_options: [SelectedCustomizableOption]!
configurable_options: [SelectedConfigurableOption!]! @resolver(class: "Magento\\ConfigurableProductGraphQl\\Model\\Resolver\\ConfigurableCartItemOptions")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\Quote\Model\Quote;
use Magento\QuoteGraphQl\Model\Cart\BuyRequest\BuyRequestBuilder;

Expand Down Expand Up @@ -87,7 +86,11 @@ public function execute(Quote $cart, array $cartItemData): void
*/
private function extractSku(array $cartItemData): string
{
if (!isset($cartItemData['data']['sku']) || empty($cartItemData['data']['sku'])) {
// Need to keep this for configurable product and backward compatibility.
if (!empty($cartItemData['parent_sku'])) {
return (string)$cartItemData['parent_sku'];
}
if (empty($cartItemData['data']['sku'])) {
throw new GraphQlInputException(__('Missed "sku" in cart item data'));
}
return (string)$cartItemData['data']['sku'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ public function testAddConfigurableProductToCart()

$quantity = 2;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
$sku = $product['sku'];
$parentSku = $product['sku'];
$sku = 'simple_20';
$attributeId = (int) $product['configurable_options'][0]['attribute_id'];
$optionId = $product['configurable_options'][0]['values'][1]['value_index'];

$query = $this->getQuery(
$maskedQuoteId,
$parentSku,
$sku,
$attributeId,
$optionId,
$quantity
);

$response = $this->graphQlMutation($query);

$cartItem = current($response['addConfigurableProductsToCart']['cart']['items']);
self::assertEquals($quantity, $cartItem['quantity']);
self::assertEquals($sku, $cartItem['product']['sku']);
self::assertEquals($parentSku, $cartItem['product']['sku']);
self::assertArrayHasKey('configurable_options', $cartItem);

$option = current($cartItem['configurable_options']);
Expand All @@ -67,32 +67,6 @@ public function testAddConfigurableProductToCart()
self::assertArrayHasKey('value_label', $option);
}

/**
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
* @expectedException \Exception
* @expectedExceptionMessage You need to choose options for your item
*/
public function testAddProductWithInvalidOptions()
{
$searchResponse = $this->graphQlQuery($this->getFetchProductQuery('configurable'));
$product = current($searchResponse['products']['items']);

$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
$sku = $product['sku'];
$attributeId = (int) $product['configurable_options'][0]['attribute_id'];

$query = $this->getQuery(
$maskedQuoteId,
$sku,
$attributeId,
9999,
1
);

$this->graphQlMutation($query);
}

/**
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable_sku.php
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
Expand All @@ -105,15 +79,13 @@ public function testAddProductIfQuantityIsNotAvailable()
$product = current($searchResponse['products']['items']);

$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
$sku = $product['sku'];
$attributeId = (int) $product['configurable_options'][0]['attribute_id'];
$optionId = $product['configurable_options'][0]['values'][1]['value_index'];
$parentSku = $product['sku'];
$sku = 'simple_20';

$query = $this->getQuery(
$maskedQuoteId,
$parentSku,
$sku,
$attributeId,
$optionId,
2000
);

Expand All @@ -122,25 +94,20 @@ public function testAddProductIfQuantityIsNotAvailable()

/**
* @param string $maskedQuoteId
* @param string $parentSku
* @param string $sku
* @param int $optionId
* @param int $value
* @param int $quantity
* @return string
*/
private function getQuery(string $maskedQuoteId, string $sku, int $optionId, int $value, int $quantity): string
private function getQuery(string $maskedQuoteId, string $parentSku, string $sku, int $quantity): string
{
return <<<QUERY
mutation {
addConfigurableProductsToCart(
input:{
cart_id:"{$maskedQuoteId}"
cart_items:{
configurable_attributes:[{
id:{$optionId}
value:{$value}
}
]
parent_sku: "{$parentSku}"
data:{
sku:"{$sku}"
quantity:{$quantity}
Expand Down

0 comments on commit aeb7cc5

Please sign in to comment.