Zend certified PHP/Magento developer

Category name from store view, only when it exist – Magento2.4

We use the following code, to get the category name of a specific product.

This works fine, but we face the issue with the store views. It only works well if the specific store view does have a unique value for the category name.

See this part $cond[] = $items->getSelect()->getConnection()->quoteInto("category_name.store_id=?", $this->_storeManager->getStore()->getId());

But when the “Use Default Value” checkbox is checked, then there is no value for the store view and it returns an empty value.

How can we fix this, by editing our code that it only fetch the store view value if it exist and otherwise it should use store id 0 (global)?

CODE:

<?php

namespace ModuleProductRelatedModel;

use MagentoStoreModelStore;

class CategoryInfoFinder
{
    /**
     * @var MagentoEavModelConfig
     */
    private $eavConfig;
    
    protected $_storeManager;

    public function __construct(
        MagentoEavModelConfig $eavConfig,
        MagentoStoreModelStoreManagerInterface $storeManager
    ) {
        $this->eavConfig = $eavConfig;
        $this->_storeManager = $storeManager;
    }

    public function addCategoryInfo(MagentoCatalogModelResourceModelProductCollection $items)
    {
        $productIds = [];
        foreach ($items as $product) {
            $productIds[] = $product->getId();
        }

        $select = $items->getSelect()->getConnection()->select()->from(['category_product' => 'catalog_category_product'])
            ->where('product_id in (?)', $productIds)
            ->reset(MagentoFrameworkDBSelect::COLUMNS)
            ->columns(['product_id' => 'category_product.product_id']);

        $categoryNameAttribute = $this->eavConfig->getAttribute(MagentoCatalogModelCategory::ENTITY, 'name');

        $cond[] = "category_name.entity_id=category_product.category_id";
        $cond[] = $items->getSelect()->getConnection()->quoteInto("category_name.attribute_id=?", $categoryNameAttribute->getId());
        $cond[] = $items->getSelect()->getConnection()->quoteInto("category_name.store_id=?", $this->_storeManager->getStore()->getId());           

        $select = $select->joinLeft(
            ['category_name' => 'catalog_category_entity_' . $categoryNameAttribute->getBackendType()],
            implode(' AND ', $cond),
            ['category_name' => 'value', 'category_id' => 'category_name.entity_id']
        );

        $select->order('category_product.category_id ' . MagentoFrameworkDataCollection::SORT_ORDER_DESC);

        $categoryInfoList = $items->getSelect()->getConnection()->fetchAll($select);

        foreach ($items as $product) {
            $categoryInfo = $this->findCategoryInfoForProduct($product, $categoryInfoList);

            if ($categoryInfo) {
                $product->setCategoryName($categoryInfo['category_name']);
                $product->setCategorySlug('category-' . $categoryInfo['category_id']);
            }
        }

        return $items;
    }

    /**
     * @param $product
     * @param array $categoryInfoList
     * @return bool|mixed
     */
    private function findCategoryInfoForProduct($product, array $categoryInfoList)
    {
        foreach ($categoryInfoList as $categoryInfo) {
            if ($categoryInfo['product_id'] == $product->getId()) {
                return $categoryInfo;
            }
        }

        return false;
    }
}