3

Custom Module - layout/catalog_product_view.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>
        <referenceContainer name="product.info.main">
                <block class="Einfochips\ConcreteMix\Block\Index" name="concretemix_index" template="Einfochips_ConcreteMix::concretemix_prod_detail.phtml" 
                       after="product.info.price" before="product.info.addtocart" />
        </referenceContainer>
    </body>
</page>

Tried with above code but it is not working. Here I need to display my custom container before Add to Cart button and after product's short description

1
  • Can't add block before button addto cart untill you modifine block template. Can only add some block after short description.
    – HoangHieu
    Sep 11, 2018 at 6:07

2 Answers 2

3

You can add your custom block by layout via some default block such as product.info.addtocart product.info.addtocart.additional product.info.options.wrapper product.info.options.wrapper.bottom. All of them have

<?= echo $block->getChildHtml('', true); ?>

Allow you add your custom block as child block

Approach

<referenceBlock name="product.info.addtocart">
    <block class="Magento\Framework\View\Element\Template" 
           name="your-block-name" 
           template="your-block-template.phtml" />
</referenceBlock>

Another approach by add custom container

<referenceContainer name="product.info.main">
     <container name="product.info.afterprice" after="product.info.price" label="Product info afterprice" htmlTag="div" htmlClass="product-info-after-price">
            <block class="Magento\Framework\View\Element\Template" 
                   name="your-block-name" 
                   template="your-block-template.phtml" />
     </container>
</referenceContainer>

So the way depends on where you want do place block, you can change block reference for your requirement

1

Do you really need your own container? Containers will not render by itself unlike blocks in containers. You have to call $block->getChildHtml(NAME); from parent block's template. I would rather simply add block inside of product.info.main. I do not see any profit for doing this at least not from the code you show us.

1
  • Rephrased my question. Not custom container, how do I call my custom module's block (html) before add to cart button and after short description ? Nov 22, 2017 at 5:08

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.