L
L
lagudal2021-06-08 08:49:05
Magento
lagudal, 2021-06-08 08:49:05

How can I add my block just before the add to cart button?

Greetings, tell me how to add your block in catalog_product_view.xml right above the add to cart button?
If you add, for example, it is suggested here - https://meetanshi.com/blog/add-block-before-add-to...
in the referenceBlock "product.info.form.content" insert the before="product.info.addtocart" block "-

<?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.info.form.content">
            <block class="Vendor\Extension\Block\Catalog\Product\View" name="catalog.product.view.extrablock"
                   as="extra_options" before="product.info.addtocart" template="Vendor_Extension::catalog/product/view.phtml"/>
        </referenceBlock>
    </body>
</page>


then the block rises, but not only above the button, but also above the input-input number.
And it is necessary just above the button but under the input, i.e. in html to come out just before the "actions" class.
They suggested a solution - in the layout, insert my block into the referenceblock product.info.addtocart and then call it in the addtocart.phtml template using getChildHtml('my_block_name').
Now my block is displayed twice - once below the button and the second time as it should - above the button.
60bf04c66348f781136401.png
How to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lagudal, 2021-06-08
@lagudal

found, blind - there by default in the template addtocart.phtml is called immediately after the add to cart button -

</button>
            <?= $block->getChildHtml('', true) ?>

and I call him again from above.
Removed the bottom call, everything worked as it should

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question