magento2-vsbridge-indexer icon indicating copy to clipboard operation
magento2-vsbridge-indexer copied to clipboard

Products not assigned for Anchor categories

Open nagrgkgc opened this issue 4 years ago • 1 comments

Hi

products are not being indexed for parent category which has anchoring categories to it. is it necessary to have products assigned to each category and anchoring product collection does not work?

Please explain.

nagrgkgc avatar Sep 29 '20 06:09 nagrgkgc

I have applied fix as below:

extend with plugin: product/ConfigurableData.php >> afterAddData $result[$key] = $this->getCategoryIdsData($result[$key]);

` /** * @param $configProduct * @return mixed */ private function getCategoryIdsData($configProduct) { //get category ids $categoryIds = $configProduct['category_ids'] ?? '';

    if (!empty($categoryIds)) {
        $categorIds = implode(',', $categoryIds);
        $ids = $this->helper->getCategoryIdsData($categorIds);

        if (!empty($ids)) {
            $configProduct['category_ids'] = [];
            foreach ($ids as $cat) {
                $configProduct['category_ids'][] = (int)$cat;
            }
        }
    }

    return $configProduct;
}`

Helper File: `/** * @param $ids * @return false|string|string[] */ public function getCategoryIdsData($ids) { $catalogCategoryEntity = $this->connection->getTableName('catalog_category_entity'); $conditions = "entity_id in ($ids)"; $select = $this->connection->select() ->from( ['category' => $catalogCategoryEntity], ["group_concat(category.path SEPARATOR '/')"] ) ->where($conditions) ->order('category.entity_id asc'); $categoryIds = $this->connection->fetchOne($select);

    if (!empty($categoryIds)) {
        $categoryIds = $this->processCategoryIds($categoryIds);

        if (!empty($categoryIds)) {
            return $categoryIds;
        }
    }

    return '';
}`

`/** * @param $categoryIds * @return false|string[] */ private function processCategoryIds($categoryIds) { //remove root and default category $baseCategoryIds = [1,2];

    $ids = array_unique(explode('/', $categoryIds));
    $categoryIds = array_diff($ids, $baseCategoryIds);

    return $categoryIds;
}`

nagrgkgc avatar Oct 01 '20 06:10 nagrgkgc