I have some experience in M1 and M2 but am somewhat struggling to grasp how to load the child category. At the minute it loads the default M2 category with [0] or [1] next category etc. But I just want to load the last category the product is attached too. Imagine Default > Electrics > Stereo > Cables. I just want to load the cables version, I know I could do it by doing [3] but imagine if another category only had Default > Electrics > Stereo it would then be [2] and I need that logic to be dynamic.

Pease can someone help, I've been crawling for hours and getting more and more confused.

This code relates to a custom related products module which is built to simply load products in the same last category view.

class Related extends Template { protected $_productCollectionFactory; protected $_categoryFactory; protected $_registry;

public function __construct(
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Catalog\Model\CategoryRepository $categoryRepository
    $this->_categoryFactory = $categoryFactory;
    $this->_productCollectionFactory = $productCollectionFactory;
    $this->_registry = $registry;
    $this->categoryRepository = $categoryRepository;

public function getRelatedProducts()

    $categoryIds = $this->_registry->registry('current_product')->getCategoryIds();

    $category = $this->_categoryFactory->create()->load($categoryIds[0]);
    $collection = $this->_productCollectionFactory->create();
        ->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
        ->addAttributeToFilter('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
    return $collection;



2 Answers 2


Why dont you just get count and there you will find your last item id ?

some thing like this :

$categoryIds = $this->_registry->registry('current_product')->getCategoryIds();    
$catId = $categoryIds[count($categoryIds) - 1]?

It should work

  • Hi, this is really good but how do you ensure the count and category is for the last category assigned on the product rather than the first or second?
    – Bradley
    Apr 15, 2018 at 18:40
  • category assigned are in nested / node tree, so parent,child and child would be the last.. i have not tested that code yet but let me know if it works. Apr 15, 2018 at 18:45

To find dynamically the last item of the array, the end php function should help you. For instance,

$categoryIds = [1, 3, 5];
$catId = end($categoryIds);

this would return $catId = 5 and this would adjust itself if the categoryIds had more values or less.

  • Hi, this method worked really well. I replaced: $category = $this->_categoryFactory->create()->load($categoryIds[0]); With: $category = $this->_categoryFactory->create()->load(end($categoryIds)); Which pulled through the last ID I require. Is this the best practice VS count, guess it doesn't really matter?
    – Bradley
    Apr 16, 2018 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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