0

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.

1

1 Answer 1

-2

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.

4
  • if we use this attribute code $currentProduct->getResource()->getAttribute('product_attribute')->getFrontend()->getValue($currentProduct); then it is working fine Nov 19, 2018 at 7:50
  • Object Manager is never a good awnser
    – Riccardo
    Oct 21, 2020 at 9:09
  • 1
    @Riccardo, Yes I know that one, check the question feasibility and if you know the better answer then give it as well. Oct 21, 2020 at 10:24
  • Yes, @Riccardo. Please enlighten us with a better way.
    – gnicko
    Feb 21, 2022 at 22:51

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.