12

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
39

You should try

$product->getTypeId()

It will return configurable, simple, grouped, virtual or downloadable

| improve this answer | |
  • Does it work for collection and product object both? – Tejas Vyas Apr 16 '18 at 6:40
  • it also returns other types like grouped , virtual , ... – CompactCode May 20 '18 at 6:07
0

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
| improve this answer | |

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.