0

I am trying to display some attributes on the product view page on a magento 2 website. However i am not able to get the values to echo onto the page. I have tried using

$block->getData('price')

and

$block->getAttributeText('name')

I am trying to call upon the price value and also a custom text attribute and display/use via phtml file.

Appreciate the help. Thanks

3 Answers 3

1

In Magento 2, the price is placed on the product view page via a XML file named catalog_product_view.xml

You can view it in the catalog module's layout folder:

app/code/Magento/Catalog/view/frontend/layout/catalog_product_view.xml

Or if you installed Magento 2 with Composer:

vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml

A container named product.info.price is created on the product view page. In the container, a block named product.price.final is created.

You can move the block with the <move> method in XML (Move Instructions):

<move element="product.price.final" destination="content" before="-" />

You can also create another block that uses the same class & arguments:

<block class="Magento\Catalog\Pricing\Render" name="product.price.final.copy" after="product.info.sku">
    <arguments>
        <argument name="price_render" xsi:type="string">product.price.render.default</argument>
        <argument name="price_type_code" xsi:type="string">final_price</argument>
        <argument name="zone" xsi:type="string">item_view</argument>
    </arguments>
</block>

*The above snippet is derived from what is in the catalog_product_view.xml file.

I hope this points you in the right direction!

1

I have similar requirement to show some text attribute values on frontend on product view page after the short description.
I have modified catalog_product_view.xml and added my own template file in my custom theme.

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>
</page>

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 } ?>


I have taken reference from http://www.mohannatarajan.com/magento/magento2-get-product-attribute-value

0

In addition to @anshu-mishra, and only using XML (no custom template),

If your attribute is output is a number, just add the argument "at_type" with value "text".

<argument name="at_type" xsi:type="string">text</argument>

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.