0

How to get all the attributes of the current product in the template?

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

So I got the name, I have the attribute "allow_same_day_delivery" how do I get it?

2 Answers 2

1

Try This :-

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) { 

    echo $attribute->getAttributeCode();
    echo $attribute->getStoreLabel();
    echo $attribute->getFrontendLabel();
    echo $attribute->getFrontend()->getLabel();   
}
0

You may try below code :

$_product = $block->getProduct();
$attributes = $_product->getAttributes();
foreach($attributes as $attribute)
{
    echo $attribute->getName()."<br />";    

}
3
  • does not fit, I'm on the page of the "catalog_product_view" template, I need to find out what product is now showing Commented Apr 9, 2019 at 15:04
  • \Magento\Catalog\Block\Product\View::getProduct is a public method. why do you say you can't use it? Commented Apr 9, 2019 at 15:31
  • same method is used in vendor/magento/module-catalog/view/frontend/templates/product/view/attributes.phtml Commented Apr 9, 2019 at 15:32

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.