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

Commit

Permalink
ENGCOM-4135: [256] Fixed variant collection #331
Browse files Browse the repository at this point in the history
  • Loading branch information
naydav authored Feb 5, 2019
2 parents 474d117 + f504d8d commit 14dec59
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
return $this->valueFactory->create($result);
}

$this->variantCollection->addParentId((int)$value[$linkField]);
$this->variantCollection->addParentProduct($value['model']);
$fields = $this->getProductFields($info);
$matchedFields = $this->attributeCollection->getRequestAttributes($fields);
$this->variantCollection->addEavAttributes($matchedFields);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public function resolve(
$data = [];
foreach ($value['options'] as $option) {
$code = $option['attribute_code'];
if (!isset($value['product'][$code])) {
if (!isset($value['product']['model'][$code])) {
continue;
}

foreach ($option['values'] as $optionValue) {
if ($optionValue['value_index'] != $value['product'][$code]) {
if ($optionValue['value_index'] != $value['product']['model'][$code]) {
continue;
}
$data[] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product;
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\CollectionFactory;
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\Collection as ChildCollection;
use Magento\Catalog\Model\ProductFactory;
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\Collection as ChildCollection;
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\CollectionFactory;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product as DataProvider;
Expand Down Expand Up @@ -47,9 +47,9 @@ class Collection
private $metadataPool;

/**
* @var int[]
* @var Product[]
*/
private $parentIds = [];
private $parentProducts = [];

/**
* @var array
Expand Down Expand Up @@ -83,19 +83,22 @@ public function __construct(
}

/**
* Add parent Id to collection filter
* Add parent to collection filter
*
* @param int $id
* @param Product $product
* @return void
*/
public function addParentId(int $id) : void
public function addParentProduct(Product $product) : void
{
if (!in_array($id, $this->parentIds) && !empty($this->childrenMap)) {
if (isset($this->parentProducts[$product->getId()])) {
return;
}

if (!empty($this->childrenMap)) {
$this->childrenMap = [];
$this->parentIds[] = $id;
} elseif (!in_array($id, $this->parentIds)) {
$this->parentIds[] = $id;

}
$this->parentProducts[$product->getId()] = $product;
}

/**
Expand Down Expand Up @@ -130,20 +133,23 @@ public function getChildProductsByParentId(int $id) : array
* Fetch all children products from parent id's.
*
* @return array
* @throws \Exception
*/
private function fetch() : array
{
if (empty($this->parentIds) || !empty($this->childrenMap)) {
if (empty($this->parentProducts) || !empty($this->childrenMap)) {
return $this->childrenMap;
}

$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
foreach ($this->parentIds as $id) {
foreach ($this->parentProducts as $product) {
$attributeData = $this->getAttributesCodes($product);
/** @var ChildCollection $childCollection */
$childCollection = $this->childCollectionFactory->create();
$childCollection->addAttributeToSelect($attributeData);

/** @var Product $product */
$product = $this->productFactory->create();
$product->setData($linkField, $id);
$product->setData($linkField, $product->getId());
$childCollection->setProductFilter($product);

/** @var Product $childProduct */
Expand All @@ -160,4 +166,24 @@ private function fetch() : array

return $this->childrenMap;
}

/**
* Get attributes code
*
* @param \Magento\Catalog\Model\Product $currentProduct
* @return array
*/
private function getAttributesCodes(Product $currentProduct): array
{
$attributeCodes = [];
$allowAttributes = $currentProduct->getTypeInstance()->getConfigurableAttributes($currentProduct);
foreach ($allowAttributes as $attribute) {
$productAttribute = $attribute->getProductAttribute();
if (!\in_array($productAttribute->getAttributeCode(), $attributeCodes)) {
$attributeCodes[] = $productAttribute->getAttributeCode();
}
}

return $attributeCodes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ private function assertConfigurableVariants($actualResponse)
$variantArray['product']['price']
);
$configurableOptions = $this->getConfigurableOptions();
$this->assertEquals(1, count($variantArray['attributes']));
foreach ($variantArray['attributes'] as $attribute) {
$hasAssertion = false;
foreach ($configurableOptions as $option) {
Expand Down

0 comments on commit 14dec59

Please sign in to comment.