【问题标题】:Displaying Custom attribute in Product LIST PAGE, and Product Details page Under my Price attribute在产品列表页面和产品详细信息页面中显示自定义属性在我的价格属性下
【发布时间】:2020-12-21 13:05:44
【问题描述】:

我创建了一个名为布料材质的自定义属性,我想在我的产品列表页面和产品详细信息页面中显示它。

有人可以建议我如何实现它吗?当前的博客文章已过时。我正在使用 Magento 2.3.3。

我还需要以编程方式进行。当我通过选择在商店目录上显示来执行此操作时,它会在更多详细信息下显示。

【问题讨论】:

    标签: magento magento2 magento-2.3


    【解决方案1】:

    首先,在您的自定义主题中创建 catalog_product_view.xml。 (如果已经存在,则修改此文件)

    app/design/frontend/Vendor/theme/Magento_Catalog/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" name="cloth.material" template="Magento_Catalog::view/cloth_material.phtml" after="product.info.price"/>
            </referenceContainer>
        </body>
    </page>
    

    现在创建cloth_material.phtml 并在这个文件中添加以下代码:

    app/design/frontend/Vendor/theme/Magento_Catalog/templates/product/view/cloth_material.phtml 并在此文件中添加以下代码:

    <?php
       $_product = $block->getProduct();
       // First method
       $attribute_value = $_product->getResource()->getAttribute('your_attribute_code')->getFrontend()->getValue($_product);
       // second method
       $attribute_value = $_product->getAttributeText('your_attribute_code');
    ?>
    

    【讨论】:

    • 就像我的属性出现在更多信息选项卡下。我希望它出现在 PRICE 下。我该怎么做?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 2019-06-02
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多