0

I have created a custom module, which creating an product attribute, now I need to display this attribute on the frontend, right under product SKU enter image description here

How I can I do this? Thanks everyone!

1

create a layout file in your extension

NameSpace/ModuleName/view/frontend/layout/catalog_product_view.xml

  <?xml version="1.0"?>
<page 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.product_vendor"
              template="Magento_Catalog::product/view/attribute.phtml" after="product.info.sku">
                <arguments>
                    <argument name="at_call" xsi:type="string">getProductVendor</argument>
                    <argument name="at_code" xsi:type="string">product_vendor</argument>
                    <argument name="css_class" xsi:type="string">product_vendor</argument>
                    <argument name="at_label" xsi:type="string">default</argument>
                    <argument name="add_attribute" xsi:type="string">itemprop="product_vendor"</argument>
                </arguments>
            </block>
        </referenceContainer>
    </body>

  • Can you please guide me how to edit this code for my module and attribute? Thanks – vladboing Feb 4 at 7:27
  • you can create above file in your custom theme after add above code and your custom attribute name replace to custom_attribute – Rakesh Donga Feb 4 at 7:30
  • edited, but now getting this error: Uncaught ArgumentCountError: Too few arguments to function Magento\Catalog\Model\Product\Interceptor::getCustomAttribute(), 0 passed in /var/www/html/magento2/vendor/magento/module-catalog/view/frontend/templates/product/view/attribute.phtml on line 37 and exactly 1 expected in /var/www/html/magento2/generated/code/Magento/Catalog/Model/Product/Interceptor.php:2191 – vladboing Feb 4 at 7:46
  • give me your custom attribute name – Rakesh Donga Feb 4 at 8:03
  • The name is: product_vendor – vladboing Feb 4 at 8:06
4

Create a catalog_product_view.xml

<referenceContainer name="product.info.stock.sku">
    <block class="Magento\Catalog\Block\Product\View\Description" name="product.info.cutom.attribute" template="Magento_Catalog::product/view/attribute.phtml" after="product.info.sku">
        <arguments>
            <argument name="at_call" xsi:type="string">getYourAttribute</argument>
            <argument name="at_code" xsi:type="string">your_attribute_code</argument>
            <argument name="css_class" xsi:type="string">custom_css_class</argument>
            <argument name="at_label" xsi:type="string">default</argument>
            <argument name="add_attribute" xsi:type="string">itemprop="your_attribute_code"</argument>
        </arguments>
    </block>
</referenceContainer>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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