In Magento 1 to display brand or any attribute value. It was easy. We can directly add our code in catalog/product/view.phtml file. But in Magneto 2, It is very strange for us. Because there is no view.phtml file.:(
So You are thinking how to display the value of attribute in product detail page.
Let’s start.
1. Create the brand attribute and add the option of brand
2. Select the brand in product from admin side
3. Create the catalog_product_view.xml file on the path app/design/<Vendor>/<Theme>/Magento_Catalog/layout.
Add this code
<?xml version="1.0"?>
<page layout="2columns-left" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="product.info.price">
<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.brand" template="product/view/brand_info.phtml" before="-">
<arguments>
<argument name="at_call" xsi:type="string">getBrand</argument>
<argument name="at_code" xsi:type="string">brand</argument>
<argument name="css_class" xsi:type="string">brand</argument>
<argument name="at_label" xsi:type="string">default</argument>
<argument name="add_attribute" xsi:type="string">itemprop="brand"</argument>
</arguments>
</block>
</referenceContainer>
</body>
</page>
4. Now create the brand_info.phtml in the path app/design/<Vendor>/<Theme>/Magento_Catalog/templates/product/view.
Add the code:
<?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; ?>
5. Now check in product detail page.
So You are thinking how to display the value of attribute in product detail page.
Let’s start.
1. Create the brand attribute and add the option of brand
2. Select the brand in product from admin side
3. Create the catalog_product_view.xml file on the path app/design/<Vendor>/<Theme>/Magento_Catalog/layout.
Add this code
<?xml version="1.0"?>
<page layout="2columns-left" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="product.info.price">
<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.brand" template="product/view/brand_info.phtml" before="-">
<arguments>
<argument name="at_call" xsi:type="string">getBrand</argument>
<argument name="at_code" xsi:type="string">brand</argument>
<argument name="css_class" xsi:type="string">brand</argument>
<argument name="at_label" xsi:type="string">default</argument>
<argument name="add_attribute" xsi:type="string">itemprop="brand"</argument>
</arguments>
</block>
</referenceContainer>
</body>
</page>
4. Now create the brand_info.phtml in the path app/design/<Vendor>/<Theme>/Magento_Catalog/templates/product/view.
Add the code:
<?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; ?>
5. Now check in product detail page.
No comments:
Post a Comment