Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #82 from DivanteLtd/bugfix/magento-ee-exporting-co…
Browse files Browse the repository at this point in the history
…nfigurable-children

Magento EE - fix exporting configurable children.
  • Loading branch information
afirlejczyk authored Jul 10, 2019
2 parents 9eac49a + 850a74f commit 23bae8b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,7 @@ public function addData(array $indexData, $storeId)

foreach ($attributes as $entityId => $attributesData) {
$productData = array_merge($indexData[$entityId], $attributesData);

if ($this->settings->useMagentoUrlKeys()) {
$productData['slug'] = $productData['url_key'];
} else {
$text = $productData['name'];

if ($this->settings->useUrlKeyToGenerateSlug()) {
$text = $productData['url_key'];
}

$slug = $this->slugGenerator->generate($text, $entityId);
$productData['slug'] = $slug;
$productData['url_key'] = $slug;
}

$productData = $this->applySlug($productData);
$indexData[$entityId] = $productData;
}

Expand All @@ -104,4 +90,30 @@ public function addData(array $indexData, $storeId)

return $indexData;
}

/**
* @param array $productData
*
* @return array
*/
private function applySlug(array $productData): array
{
$entityId = $productData['id'];

if ($this->settings->useMagentoUrlKeys()) {
$productData['slug'] = $productData['url_key'];
} else {
$text = $productData['name'];

if ($this->settings->useUrlKeyToGenerateSlug()) {
$text = $productData['url_key'];
}

$slug = $this->slugGenerator->generate($text, $entityId);
$productData['slug'] = $slug;
$productData['url_key'] = $slug;
}

return $productData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,19 @@ private function prepareConfigurableProduct(array $productDTO)
*/
private function hasPrice(array $product)
{
if (!isset($product['price'])) {
return false;
}
$priceFields = [
'price',
'final_price',
];

foreach ($priceFields as $field) {
if (!isset($product[$field])) {
return false;
}

if (0 === (int)$product['price']) {
return false;
if (0 === (int)$product[$field]) {
return false;
}
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public function __construct(MetadataPool $metadataPool)

/**
* @return \Magento\Framework\EntityManager\EntityMetadataInterface
* @throws \Exception
*/
public function get()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Magento\ConfigurableProduct\Model\Product\Type\Configurable as ConfigurableType;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Helper as DbHelper;
use Divante\VsbridgeIndexerCatalog\Model\ProductMetaData;

/**
* Class Configurable
Expand Down Expand Up @@ -34,6 +35,11 @@ class Configurable
*/
private $productResource;

/**
* @var ProductMetaData
*/
private $productMetaData;

/**
* Array of the ids of configurable products from $productCollection
*
Expand Down Expand Up @@ -80,17 +86,20 @@ class Configurable
*
* @param AttributeDataProvider $attributeDataProvider
* @param Product $productResource
* @param ProductMetaData $productMetaData
* @param ResourceConnection $resourceConnection
* @param DbHelper $dbHelper
*/
public function __construct(
AttributeDataProvider $attributeDataProvider,
Product $productResource,
ProductMetaData $productMetaData,
ResourceConnection $resourceConnection,
DbHelper $dbHelper
) {
$this->attributeDataProvider = $attributeDataProvider;
$this->resource = $resourceConnection;
$this->productMetaData = $productMetaData;
$this->productResource = $productResource;
$this->dbHelper = $dbHelper;
}
Expand Down Expand Up @@ -155,7 +164,8 @@ public function getProductConfigurableAttributes(array $product)
private function getProductConfigurableAttributeIds(array $product)
{
$attributes = $this->getConfigurableProductAttributes();
$productId = $product['id'];
$linkField = $this->productMetaData->get()->getLinkField();
$productId = $product[$linkField];

if (!isset($attributes[$productId])) {
throw new \Exception(
Expand All @@ -175,7 +185,7 @@ private function getProductConfigurableAttributeIds(array $product)
private function getConfigurableProductAttributes()
{
if (!$this->configurableProductAttributes) {
$productIds = $this->getConfigurableProductIds();
$productIds = $this->getParentIds();
$attributes = $this->getConfigurableAttributesForProductsFromResource($productIds);
$this->configurableProductAttributes = $attributes;
}
Expand Down Expand Up @@ -262,19 +272,34 @@ private function getConfigurableAttributeFullInfo()
private function getConfigurableProductIds()
{
if (null === $this->configurableProductIds) {
$linkField = $this->productMetaData->get()->getLinkField();
$entityField = $this->productMetaData->get()->getIdentifierField();

$this->configurableProductIds = [];
$products = $this->productsData;

foreach ($products as $product) {
if ($product['type_id'] == ConfigurableType::TYPE_CODE) {
$this->configurableProductIds[] = $product['id'];
$entityId = $product[$entityField];
$linkId = $product[$linkField];
$this->configurableProductIds[$linkId] = $entityId;
}
}
}

return $this->configurableProductIds;
}

/**
* @return array
*/
private function getParentIds()
{
$productIds = $this->getConfigurableProductIds();

return array_keys($productIds);
}

/**
* Return all associated simple products for the configurable products in
* the current product collection.
Expand All @@ -288,13 +313,14 @@ private function getConfigurableProductIds()
public function getSimpleProducts($storeId)
{
if (null === $this->simpleProducts) {
$parentIds = $this->getConfigurableProductIds();
$parentIds = $this->getParentIds();
$childProduct = $this->productResource->loadChildrenProducts($parentIds, $storeId);

/** @var Product $product */
foreach ($childProduct as $product) {
$simpleId = $product['entity_id'];
$parentIds = explode(',', $product['parent_ids']);
$parentIds = $this->mapLinkFieldToEntityId($parentIds);
$product['parent_ids'] = $parentIds;
$this->simpleProducts[$simpleId] = $product;
}
Expand All @@ -303,6 +329,22 @@ public function getSimpleProducts($storeId)
return $this->simpleProducts;
}

/**
* @param array $linkIds
*
* @return array
*/
private function mapLinkFieldToEntityId(array $linkIds)
{
$productIds = [];

foreach ($linkIds as $id) {
$productIds[] = $this->configurableProductIds[$id];
}

return $productIds;
}

/**
* @return \Magento\Framework\DB\Adapter\AdapterInterface
*/
Expand Down

0 comments on commit 23bae8b

Please sign in to comment.