0

I am working on a module for a customer that is designed to generate a category link to the main artist page for the product. I've created a module that's designed to get the category IDs of the artist categories then the categories of the current product. Afterwards, it compares the arrays for a matching value then gets the category name and URL from the matching category ID value.

But I'm getting this instead:
Uncaught Error: Call to a member function addAttributeToFilter() on null

Here's the code for the block:

<?php
namespace Custom\ArtistLink\Block;

class ArtistLink extends \Magento\Framework\View\Element\Template implements \Magento\Framework\DataObject\IdentityInterface
{
    protected $_coreRegistry = null;
    protected $_catalogLayer;
    protected $_categoryHelper;
    protected $_outputHelper;
    protected $_dataHelper;
    protected $_categoryCollectionFactory;
    protected $_productRepository;
    protected $_categoryRepository;
    
    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Catalog\Helper\Category $categoryHelper
     * @param \Magento\Catalog\Helper\Output $outputHelper
     * @param \Magento\Catalog\Helper\Data $dataHelper
     * @param \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
     * @param \Magento\Catalog\Model\ProductRepository $productRepository,
     * @param \Magento\Catalog\Model\CategoryRepository $categoryRepository
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\Layer\Resolver $layerResolver,
        \Magento\Framework\Registry $registry,
        \Magento\Catalog\Helper\Category $categoryHelper,
        \Magento\Catalog\Helper\Output $outputHelper,
        \Magento\Catalog\Helper\Data $dataHelper,
        \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
        \Magento\Catalog\Model\ProductRepository $productRepository,
        \Magento\Catalog\Model\CategoryRepository $categoryRepository,
        array $data = []
    )
    {
        $this->_categoryHelper = $categoryHelper;
        $this->_outputHelper = $outputHelper;
        $this->_dataHelper = $dataHelper;
        $this->_catalogLayer = $layerResolver->get();
        $this->_coreRegistry = $registry;
        $this->_categoryCollectionFactory = $categoryCollectionFactory;
        $this->_productRepository = $productRepository;
        $this->_categoryRepository = $categoryRepository;
        parent::__construct($context, $data);
    }
    
    /**
     * Get Artist category productCategories
     *
     * @return \Magento\Catalog\Model\ResourceModel\Category\Collection or array
     */
    public function getArtistCategories()
    {
        $artistCategoryId = '20';
        $artistParent = $this->_categoryRepository->get($artistCategoryId);
        $artistSubcats = $artistParent->getChildrenCategories();
        return $artistSubcats;
    }

    /**
     * Get product category collection
     *
     * @param bool $isActive
     * @return \Magento\Catalog\Model\ResourceModel\Category\Collection or array
     */
    public function getProductCategories($isActive = true)
    {
        $productCategories = $this->_categoryCollectionFactory->create();
        $productCategories->addAttributeToSelect('*');
        
        if ($isActive) {
            $productCategories->addIsActiveFilter();
        }
        
        return $productCategories;
    }

    public function getProductById($id)
    {
        return $this->_productRepository->getById($id);
    }
        
    public function getCurrentProduct()
    {  
        return $this->_registry->registry('current_product');
    }
    
    public function getIdentities()
    {
        return $this->getCurrentCategory()->getIdentities();
    }
}

And the PHTML;

<?php
$parentCategory = $block->getParentCategoryId();
$artistCategories = $block->getArtistCategories();
//$product = $block->getCurrentProduct();
$prodId = 2832;
$product = $block->getProductById($prodId);

$catIds = $product->getCategoryIds();
$productCategories = $block->getCategoryCollection()->addAttributeToFilter('entity_id', $catIds);

$categoryMatch = array_intersect($artistCategories, $productCategories);
?>

<div>
product id: <?php echo $prodId; ?>
</div>
<br /><br />
<div>
artist categories:<br />
<?php echo $artistCategories; ?>
</div>
<br /><br />
<div>
product categories:<br />
<?php echo $catIds; ?>
</div>
<br /><br />
<div>
category collection:<br />
<?php echo $productCategories; ?>
</div>
<br /><br />
<div>
array match:<br />
<?php echo $categoryMatch; ?>
</div>

It's a test phtml file meant to verify the values are displayed as expected.

This is where it fails:
$productCategories = $block->getCategoryCollection()->addAttributeToFilter('entity_id', $catIds); That returns null and I get why, but not sure what I need to do to fix it. $catIds = $product->getCategoryIds(); returns null but I'm not sure why... or where I went wrong.

I used this tutorial to help me build things out a bit: https://www.mageplaza.com/devdocs/how-get-categories-specific-product-magento-2.html.

After correcting the template to use the right function, I am now getting the following: Warning: array_intersect(): Argument #1 is not an array Which means $artistCategories isn't an array.

    public function getArtistCategories()
    {
        $artistCategoryId = '20';
        $artistParent = $this->_categoryRepository->get($artistCategoryId);
        $artistSubcats = $artistParent->getChildrenCategories();
        return $artistSubcats;
    }

is supposed to get the categories as an array, but I'm not sure what it's being returned as. Can I cast to an array?

1 Answer 1

1

Your class "ArtistLink" does not have the function "getCategoryCollection".

1
  • That was a stupid on my part but I updated my question with more information.
    – Marisa
    Aug 26, 2020 at 22:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.