每当我们创建自定义产品属性时,我们可能需要在产品页面上显示它,这里有两种显示形式:
1.在现有产品标签中(在“更多信息” 标签中)
2.在新标签中(通过添加新标签)
1.在现有产品标签中
要在“更多信息”标签中显示属性,您需要将Storefront上的目录页面上的“可见” 设置 为“是”。
在此之后,您设置的属性将显示在这里:
2.在新标签中
有时我们可能需要添加一个新标签并在其中显示属性值(主要用于Text Area 类型的属性或者在显示值时任何自定义)。在这种情况下,我们需要一个自定义模块或主题,只需遵循以下基本步骤:
注意:我们将使用名为Test Attribute的属性和代码 test_attribute作为参考,您可以根据您的属性进行修改
步骤1
根据您的要求创建app/code/Vendor/ModuleName/view/frontend/layout/catalog_product_view.xml或app/design/frontend/Vendor/ThemeName/Magento_Catalog/layout/catalog_product_view.xml
步骤2
然后将此代码添加到文件中:
<?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> <referenceBlock name="product.info.details"> <block class="Magento\Catalog\Block\Product\View\Description" name="product.test_attribute" template="product/view/attribute.phtml" group="detailed_info"> <arguments> <argument name="at_call" xsi:type="string">getTestAttribute</argument> <argument name="at_code" xsi:type="string">test_attribute</argument> <argument name="css_class" xsi:type="string">test_attribute</argument> <argument name="at_label" xsi:type="string">none</argument> <argument name="title" translate="true" xsi:type="string">Test Attribute</argument> </arguments> </block> </referenceBlock> </body> </page>
步骤3
应该注意的是,我们使用getTestAttribute方法时属性代码为test_attribute,您可根据要求修改
步骤4
保存之后,该属性将显示在产品页面的新标签中,如下所示: