0

I was trying to display a custom attribute to product view page and overwrote the base file with child theme but after that only the custom attribute is showing and rest has disappeared i.e add to cart, QTY and short description . Any idea why?

app/design/frontend/ThemePackage/themename/Magento_Catalog/layout/catalog_product_view.xml

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <container name="product.info.main" htmlTag="div" htmlClass="product-info-main product-shop col-md-7 col-sm-7 col-xs-12" after="product.info.media">
                <block class="Magento\Catalog\Block\Product\View\Description" name="product.info.measurements" template="product/view/customattribute.phtml" after="product.info.overview">
                    <arguments>
                        <argument name="at_call" xsi:type="string">getMeasurements</argument>
                        <argument name="at_code" xsi:type="string">measurements</argument>
                        <argument name="css_class" xsi:type="string">measurements</argument>
                        <argument name="at_label" xsi:type="string">measurements</argument>
                        <argument name="add_attribute" xsi:type="string">itemprop="measurements"</argument>
                    </arguments>
                </block>
            </container>
        </referenceContainer>
    </body>

app/design/frontend/ThemePackage/themename/Magento_Catalog/templates/product/view/customattribute.phtml

<?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?>">
    <div class="value" <?php echo $_attributeAddAttribute;?>><?php echo $_attributeValue; ?></div>
</div>
<?php } ?>

1 Answer 1

0

Try the below code for your xml:

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="product.info.main">
        <block class="Magento\Catalog\Block\Product\View\Description" name="product.info.measurements" template="product/view/customattribute.phtml" after="product.info.overview">
            <arguments>
                <argument name="at_call" xsi:type="string">getMeasurements</argument>
                <argument name="at_code" xsi:type="string">measurements</argument>
                <argument name="css_class" xsi:type="string">measurements</argument>
                <argument name="at_label" xsi:type="string">measurements</argument>
                <argument name="add_attribute" xsi:type="string">itemprop="measurements"</argument>
            </arguments>
        </block>
    </referenceContainer>
</body>

If you check "product.info.main" name is already assigned for right block in product details page. If you use the same name then it will replace the right container with your new one. So in your case use referenceContainer in place of container.

6
  • Good point but <move element="product.info.main" destination="product-view-wrapper" /> is added to the base xml file, that I am overriding so now the custom attribute isnt showing Oct 1, 2018 at 14:45
  • pastebin.com/Sh68V6hW you can view it here Oct 1, 2018 at 14:47
  • Above code is not working for you? I am seeing that you are using Infortis theme. Oct 1, 2018 at 14:51
  • Custom attribute isn't showing but add to cart and other things are showing and yes, it is infortis theme Oct 1, 2018 at 14:53
  • on which section you want to show? Oct 1, 2018 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.