how to get current products custom attribute value in block(phtml) to display in products details page magento2?

magento.stackexchange https://magento.stackexchange.com/questions/250322

Domanda

Here i am trying to get current products custom attribute value in block(phtml) and after that i want to show these custom attributes values in products details page using block,phtml file magento2.

È stato utile?

Soluzione

You can get product from registry and then show it custom attribute value.

$OM =  \Magento\Framework\App\ObjectManager::getInstance();  
$registry = $OM->get('\Magento\Framework\Registry'); 
$currentProduct = $registry->registry('current_product');

if($currentProduct)
{
   echo $currentProduct->getName() . '<br />';
   echo $currentProduct->getSku() . '<br />';
   echo $currentProduct->getCustomAttribute();
} 

NOTE: ObjectManager is not recommended according to magento standard.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top