Magento 2 - Copy price block to another location in product layout

I am trying to find a way to duplicate the price block so that it appears in two locations of the product page for specific products, by using the Layout Update XML field inside the design tab of the product edit page (Magento Admin).

I have successfully copied the price block by creating a modified layout XML file for specific products, for example:

theme_folder\Magento_Catalog\layout\catalog_product_view_id_178.xml

Inside this file I added the following in the required location:

<block class="Magento\Catalog\Pricing\Render" name="product.price.final.second.location" after="product.info.options.wrapper">
<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>  

However there are too many products that use this modified layout and it would be easier to manage the product catalog if I could achieve the same thing just using the Layout Update XML field and entering a simple instruction like:

<copy element="product.price.final" destination="second.price.container">

Except there is no copy instruction, only a move instruction which is not what I want.

A different solution would be to have a way to select which layout XML to use from the product edit page, for example a yes/no attribute.

Is there a way to achieve this result?

1 answer

  • answered 2018-04-14 17:37 Nosastra

    I found the solution. The following XML inserted into the Layout Update XML field will copy the price block to a referenceBlock (or referenceContainer):

    <referenceBlock name="my.reference.block">
    <block class="Magento\Catalog\Pricing\Render" name="product.price.final.lower">
    <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>
    </referenceBlock>