You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Magento 2:如何将商品价格区块复制到商品布局的另一位置

实现Magento特定商品价格区块双位置显示的布局方案

Got it, let's tackle this problem of showing the product price block in two places on a specific product page in Magento. You're already on the right track using the product-specific layout XML file, so let's refine that approach with working code and explanations:

方法1:使用商品ID专属布局XML文件

在你的主题目录下的Magento_Catalog/layout/catalog_product_view_id_178.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>
        <!-- 在目标容器中添加第二个价格区块,示例用content.top容器,可根据需求替换 -->
        <referenceContainer name="content.top">
            <block class="Magento\Catalog\Block\Product\View\Description" 
                   name="product.price.second" 
                   template="Magento_Catalog::product/view/price.phtml">
                <arguments>
                    <argument name="callable" xsi:type="array">
                        <item name="0" xsi:type="object">Magento\Catalog\Pricing\Render\PriceBox</item>
                        <item name="1" xsi:type="string">render</item>
                    </argument>
                </arguments>
                <block class="Magento\Framework\Pricing\Render" 
                       name="product.price.render.second" 
                       as="product_price">
                    <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>
            </block>
        </referenceContainer>
    </body>
</page>

关键细节说明:

  • 唯一区块名称:新区块的name(product.price.second)和内部渲染区块的name(product.price.render.second)必须是唯一的,不能和原价格区块的名称重复(原区块通常是product.price.final和product.price.render.default)。
  • 目标容器替换:把content.top换成你需要显示价格的目标容器名称(比如product.info.main、product.info.media附近的容器,可通过Magento布局调试工具确认容器名称)。
  • 保持一致性:区块的class和template与原价格区块完全一致,确保显示样式、价格逻辑(比如促销价、含税价)和原区块完全相同。

方法2:使用后台商品编辑页的Layout Update XML字段

如果你不想单独创建XML文件,也可以直接在后台商品编辑页的「设计」标签下,将以下代码粘贴到Layout Update XML字段中:

<referenceContainer name="content.top">
    <block class="Magento\Catalog\Block\Product\View\Description" 
           name="product.price.second" 
           template="Magento_Catalog::product/view/price.phtml">
        <arguments>
            <argument name="callable" xsi:type="array">
                <item name="0" xsi:type="object">Magento\Catalog\Pricing\Render\PriceBox</item>
                <item name="1" xsi:type="string">render</item>
            </argument>
        </arguments>
        <block class="Magento\Framework\Pricing\Render" 
               name="product.price.render.second" 
               as="product_price">
            <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>
    </block>
</referenceContainer>

最后必做步骤

  1. 清除缓存:修改布局后,一定要进入Magento后台的「系统」->「工具」->「缓存管理」,刷新所有缓存(尤其是布局缓存)。
  2. 测试验证:访问该商品页面,检查两个位置的价格是否正常显示,确认促销价、价格动态变化等逻辑是否和原区块一致。

内容的提问来源于stack exchange,提问作者LiquidLunch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:49:26