18

I'm creating a new module in Magento 2. I try to get product information in the phtml file. I have get successfully entity_id and product name information. but when I get product type information is getting wrong. The following code used.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product'); //get current product

echo $product->getId() . '<br/>';
echo $product->getName() . '<br/>';
echo $product->getProductType() . '<br/>'; //getting wrong product type

3 Answers 3

58

You should try

$product->getTypeId()

It will return configurable, simple, grouped, virtual, downloadable and bundle.

3
  • Does it work for collection and product object both?
    – Tejas Vyas
    Commented Apr 16, 2018 at 6:40
  • 2
    it also returns other types like grouped , virtual , ... Commented May 20, 2018 at 6:07
  • there is also bundle Commented Sep 14, 2020 at 19:47
1

For ordered product list, from order collection we can use $item->getProductType() to get the Product type.

Sample code set

<?php 
......
foreach ($orders->getAllItems() as $item) {
  echo $item->getName()."-".$item->getProductType();
}
......
?>

Tested with magento 2.4.5_p1

-6

Try this :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product'); //get current product

$product->getTypeId() // it will return the product type

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.