Friday, January 22

Magento2 How to get custom attribute value in Product View page


In Magento2 product detail page you can able to see the product description, short description, price, etc.. these are all the attributes already exists in Magento. But every business may need some more additional attribute based on their products. let's say if you want to create additional attribute and you want to show those attributes in product view page, you need to follow the below steps.

Let's assume I want to specify the product warranty in product detail page.

Step 1 : Create a new attribute 'warranty' in Magento back end under STORES->Attributes->Product.

Step 2 : Assign the newly created attribute 'warranty' to the default attribute set. You can see the attribute set option under STORES->Attributes->Attribute set.
I assume you already know how to create and assign the attribute to attribute set. So we don't want to go deeper.

Step 3: Now we can able to see the attribute while creating the products. we can give whatever value for the attribute.

Step 4: Edit the catalog_product_view.xml file and update the below content. you can add this section inside the product.info.main container. you can add your block next to product.info.overview block. So it will be shown next to the short description.



getWarranty
warranty
warranty 
warranty 
itemprop="warranty "



Step 5: Create a new file warranty.phtml under mageno2root/vendor/magento/module-catalog/view/frontend/templates/product/view with the below content.

<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
$_code = $block->getAtCode();
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();

if ($_attributeLabel && $_attributeLabel == 'default') {
$_attributeLabel = $_product->getResource()->getAttribute($_code)->getFrontendLabel();
}
$_attributeValue =$_product->getResource()->getAttribute($_code)->getFrontend()->getValue($_product);
?>
<?php if ($_attributeValue): ?>
<div class="product attibute <?php echo $_className?>">
<?php if ($_attributeLabel != 'none'): ?><strong class="type"><?php echo $_attributeLabel?></strong><?php endif; ?>
<div class="value" <?php echo $_attributeAddAttribute;?>><?php echo $_attributeValue; ?></div>
</div>
<?php endif; ?>

That's it. Now we can able to see the warranty value in product detail page next to short description section.

Hope it helps for someone who starts to work in Magento2.

If you are looking for a post to get an attribute value Magento1.x Here you go

5 comments:

  1. Great Read! I'll come back for more :D

    Japs Buidon is a Social Media Specialist and SEO from a renowned Magento Development Company in Florida. He loves hiking as well as electronics.

    ReplyDelete
  2. Thanks for the Post. It really help

    ReplyDelete
  3. Thanks for sharing this information.It was very nice blog to learn about magento

    ReplyDelete
  4. Thanks for sharing this information.It was very nice blog to learn about
    magento

    ReplyDelete
  5. Informative Content on magento Thanks for sharing

    ReplyDelete