0

I am trying to get the category name of a series of products. When I show the products individually the category is correct and changes but when I show more than one product it always shows me the category of the first in all the other products.

My test code is:

        $product = $objectManager->get('Magento\Catalog\Model\Product')->load(2510);

        $CodigoArticulo = $product->getSku();
        $articulo = $product->getName();
        $cats = $product->getCategoryIds();
                


        echo "<div class='row'>";
        echo "<p class='text-bold'>Código artículo: ".$CodigoArticulo."</p>";
        echo "<p class='text-bold'>Artículo: ".$articulo."</p>";
        echo "<p>Categoria Ids:</p>";
        echo "<p>".var_dump($cats)."</p>";
        foreach ($cats as $category_id) {
            $_cat = $objectManager->get('Magento\Catalog\Model\Category')->load($category_id) ;
            echo "<p>Categoria Name: ".$_cat->getName()."</p>";
        } 
        echo "</div>";
        echo "<hr>";


        $product = $objectManager->get('Magento\Catalog\Model\Product')->load(3167);

        $CodigoArticulo = $product->getSku();
        $articulo = $product->getName();
        $cats = $product->getCategoryIds();
                


        echo "<div class='row'>";
        echo "<p class='text-bold'>Código artículo: ".$CodigoArticulo."</p>";
        echo "<p class='text-bold'>Artículo: ".$articulo."</p>";
        echo "<p>Categoria Ids:</p>";
        echo "<p>".var_dump($cats)."</p>";
        foreach ($cats as $category_id) {
            $_cat = $objectManager->get('Magento\Catalog\Model\Category')->load($category_id) ;
            echo "<p>Categoria Name: ".$_cat->getName()."</p>";
        } 
        echo "</div>";
        echo "<hr>";

I know that it is not a code optimized in real environment, I use a for each, it is only for testing.

The result is the same ('Categoria name'):

enter image description here

But I run only a product show the correct category:

        $product = $objectManager->get('Magento\Catalog\Model\Product')->load(3167);

        $CodigoArticulo = $product->getSku();
        $articulo = $product->getName();
        $cats = $product->getCategoryIds();
                


        echo "<div class='row'>";
        echo "<p class='text-bold'>Código artículo: ".$CodigoArticulo."</p>";
        echo "<p class='text-bold'>Artículo: ".$articulo."</p>";
        echo "<p>Categoria Ids:</p>";
        echo "<p>".var_dump($cats)."</p>";
        foreach ($cats as $category_id) {
            $_cat = $objectManager->get('Magento\Catalog\Model\Category')->load($category_id) ;
            echo "<p>Categoria Name: ".$_cat->getName()."</p>";
        } 
        echo "</div>";
        echo "<hr>";

The result:

enter image description here

At this point I am confused because I do not understand why if I show the products individually, it correctly shows me the name of the category, instead when I show more than one it shows me the category of the first one in all the products.

Maybe someone can me help.

2

1 Answer 1

2

ObjectManager::get returns a singleton instance of the class, you are calling getCategoryIds from the same object even you load with a different ID, that is why it returned the same value.

ObjectManager::create would return a new instance and will solve your issue.

However, ObjectManager should never be called explicitly, you should inject the product repository instead.

1
  • Ok I understand is the same object but I'm not an expert of Magento and I don't know how to use the product repository class.
    – ruzD
    Commented Jul 2, 2021 at 10:27

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.