0

I am new to Magento and would like to add a custom block underneath the 'Add to Cart' button.

Here is my catalog_product_view.xml file:

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="product.info.form.content">

            <block
                    class="Vendor\ProductPage\Block\Product"
                    name="product.info.customblock"
                    after="product.info.addtocart"
                    template="Vendor_ProductPage::catalog/product/view/custom_block.phtml">
            </block>
        </referenceContainer>
</body>
</page>

I have tried clearing my cache, re-compiling Magento, different file names and locations for my template file.

My template file is found in Vendor/ProductPage/view/frontend/templates/catalog/product/view/custom_block.phtml

My layout file is found in Vendor/ProductPage/view/frontend/layout/catalog_product_view.xml

Any suggestions would be great!

Many thanks Pete

2
  • Hey Peter, everything looks good, it should render the block. Do you get any errors? Also, some stupid checks like the module is enabled? Try a different browser, not the one you are usually working with, I've had funny caching issues with Magento when using the same browser. Try to add a simple phtml file (maybe a js alert) inside a default.xml in your module, just to make sure that everything is working properly from inside your module.
    – Dan Staver
    Feb 22, 2022 at 6:05
  • Hi Dan. Thanks for getting back. I had tried a combination of different file names (removing special characters), module is enabled as I managed to extend other layouts, just not this one! Feb 22, 2022 at 9:21

1 Answer 1

1

You can use below code to call the custom block below the add to cart button.

  <referenceContainer name="product.info.extrahint">
        <block
                class="Vendor\ProductPage\Block\Product"
                name="product.info.customblock"
                template="Vendor_ProductPage::catalog/product/view/custom_block.phtml"
                before="-"
        >
        </block>
    </referenceContainer>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.