Skip to content

Commit

Permalink
Merge pull request #480 from afoucret/fix_children_indexing_visibility
Browse files Browse the repository at this point in the history
Fix children visibility during product indexing.
  • Loading branch information
afoucret authored Aug 9, 2017
2 parents 259c1bd + 7c1073a commit 5e47940
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,18 @@ public function addData($storeId, array $indexData)
$productIds = array_keys($indexData);
$indexData = $this->addAttributeData($storeId, $productIds, $indexData);

$relationsByChildId = $this->resourceModel->loadChildrens($productIds);
$relationsByChildId = $this->resourceModel->loadChildrens($productIds, $storeId);

if (!empty($relationsByChildId)) {
$allChildrenIds = array_keys($relationsByChildId);
$childrenIndexData = $this->addAttributeData($storeId, $allChildrenIds);
$allChildrenIds = array_keys($relationsByChildId);
$childrenIndexData = $this->addAttributeData($storeId, $allChildrenIds);

foreach ($childrenIndexData as $childrenId => $childrenData) {
$enabled = current($childrenData['status']) == 1;
if ($enabled === false) {
unset($childrenIndexData[$childrenId]);
}
}

foreach ($relationsByChildId as $childId => $relations) {
foreach ($relations as $relation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ public function __construct(
* Warning the result use children ids as a key and list of parents as value
*
* @param array $productIds List of parent product ids.
* @param int $storeId Store id.
*
* @return array
*/
public function loadChildrens($productIds)
public function loadChildrens($productIds, $storeId)
{
$children = [];

Expand All @@ -85,7 +86,7 @@ public function loadChildrens($productIds)
$relation = $typeInstance->getRelationInfo();

if ($relation->getTable() && $relation->getParentFieldName() && $relation->getChildFieldName()) {
$select = $this->getRelationQuery($relation, $productIds);
$select = $this->getRelationQuery($relation, $productIds, $storeId);
$data = $this->getConnection()->fetchAll($select);

foreach ($data as $relationRow) {
Expand Down Expand Up @@ -154,10 +155,11 @@ protected function getEntityTypeId()
*
* @param \Magento\Framework\DataObject $relation Relation Instance
* @param array $parentIds The parent product Ids (array of entity_id)
* @param int $storeId Store id.
*
* @return \Magento\Framework\DB\Select
*/
private function getRelationQuery($relation, $parentIds)
private function getRelationQuery($relation, $parentIds, $storeId)
{
$linkField = $this->getEntityMetaData($this->getEntityTypeId())->getLinkField();
$entityIdField = $this->getEntityMetaData($this->getEntityTypeId())->getIdentifierField();
Expand Down Expand Up @@ -195,6 +197,31 @@ private function getRelationQuery($relation, $parentIds)

$select->group("main.{$childFieldName}");

return $this->addWebsiteFilter($select, "main", $childFieldName, $storeId);
}

/**
* Add website clauses to products selected.
*
* @param \Magento\Framework\DB\Select $select Original select.
* @param string $productTableName Product table name in the original select.
* @param string $productFieldName Product id field name in the original select.
* @param int $storeId Store id.
*
* @return \Magento\Framework\DB\Select $select
*/
private function addWebsiteFilter(\Magento\Framework\DB\Select $select, $productTableName, $productFieldName, $storeId)
{
$websiteId = $this->getStore($storeId)->getWebsiteId();
$indexTable = $this->getTable('catalog_product_website');

$visibilityJoinCond = $this->getConnection()->quoteInto(
"websites.product_id = $productTableName.$productFieldName AND websites.website_id = ?",
$websiteId
);

$select->useStraightJoin(true)->join(['websites' => $indexTable], $visibilityJoinCond, []);

return $select;
}
}

0 comments on commit 5e47940

Please sign in to comment.