Magento 2 – Move Product Price Block Before Add to Cart Button

designlayoutmagento2theme-overridingxml

I am using custom theme and trying to move the "product.info.price" block before the addtocart button.

I have tried, direct into addtocart.phtml file but not working.

<?php echo $this->getPriceHtml($_product) ?>

app\design\frontend\my_package\my_theme\Magento_Catalog\templates\product\view\addtocart.phtml

If I used :

<move element="product.info.price" destination="product.info.addtocart" />

Then it moves the price but it only appears on simple products without custom options.

If I used :

<move element="product.info.price" destination="product.info.addtocart.additional" />

Then it moves the price but it only appears on simple products with custom options.

So, How I can show the price block for both simple product and simple product with custom options.

Best Answer

Go to below location

Magento_root/vendor/magento/module-catalog/view/frontend/templates/product/view

Copy file with Name addtocart.phtml & paste it to below location in your theme

Magento_root/app/design/frontend/{Package}/{theme}/Magento_Catalog/templates/product/view

Put the below code into the file above this <div class="actions"> present at around line no.31 .

<?php echo $this->getLayout()
          ->createBlock('Magento\Catalog\Pricing\Render',
                  "product.price.final",
                    [
                        'data' => [
                            'price_render' => 'product.price.render.default',
                            'price_type_code' => 'final_price',
                            'zone' => 'item_view'
                        ]
                   ]
                  )
          ->toHtml();?>

Now go to the below location

Magento_root/app/design/frontend/Package/theme/Magento_Catalog/layout/

Put the below code into the file with name 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>
         <referenceBlock name="product.price.final" remove="true"/>
    </body>
</page>

Note: Put your cache disabled or run the below command while you are doing this changes

php bin/magento cache:flush
Related Topic