1

I want to add my custom block directly above the add to cart button. If i do it like here - https://meetanshi.com/blog/add-block-before-add-to-cart-button-in-magento-2-product-page/

<?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>

block is displayed not only before the button but also before qty input field.

I see a solution, for example: I add my custom block to referenceblock "product.info.addtocart" and in the template addtocart.phtml call it with getChildHtml('my_block_name'). But now my block is displayed two times. enter image description here

Any idea?

1
  • Can you please check you are not calling getChildHtml('my_block_name') multiple times in addtocart.phtml. Also you have removed added block from layout file. Commented Jun 8, 2021 at 5:39

1 Answer 1

1

I have reviewd your question.

You have done like - for example: I add my custom block to referenceblock "product.info.addtocart" and in the template addtocart.phtml call it with getChildHtml('my_block_name'). But now my block is displayed two times.

Your way is correct. Now you just need to change in addtocart.phtml file.

Please remove getChildHtml('my_block_name') from your phtml file and please move <?= $block->getChildHtml('', true) ?> above add to cart button.

In addtocart.phtml file, there is already called getChildHtml('', true) ?> after add to cart button so you need to move this code above add to cart button and you have called your custom block as well before button so it was displaying two times.

Updated addtocart.phtml file

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/** @var $block \Magento\Catalog\Block\Product\View */
?>
<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php if ($_product->isSaleable()) :?>
<div class="box-tocart">
    <div class="fieldset">
        <?php if ($block->shouldRenderQuantity()) :?>
        <div class="field qty">
            <label class="label" for="qty"><span><?= $block->escapeHtml(__('Qty')) ?></span></label>
            <div class="control">
                <input type="number"
                       name="qty"
                       id="qty"
                       min="0"
                       value="<?= $block->getProductDefaultQty() * 1 ?>"
                       title="<?= $block->escapeHtmlAttr(__('Qty')) ?>"
                       class="input-text qty"
                       data-validate="<?= $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
                       />
            </div>
        </div>
        <?php endif; ?>
        <?= $block->getChildHtml('', true) ?>
        <div class="actions">
            <button type="submit"
                    title="<?= $block->escapeHtmlAttr($buttonTitle) ?>"
                    class="action primary tocart"
                    id="product-addtocart-button" disabled>
                <span><?= $block->escapeHtml($buttonTitle) ?></span>
            </button>
        </div>
    </div>
</div>
<?php endif; ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "Magento_Catalog/js/validate-product": {}
        }
    }
</script>

Like this you need to move - https://prnt.sc/14o0qs3

So all the child blocks will be called which are called under referenceblock "product.info.addtocart"

2
  • Thanks, it's work now.
    – iskander
    Commented Jun 8, 2021 at 7:26
  • Glad to hear that. Cheers!
    – ZealousWeb
    Commented Jun 8, 2021 at 7:28

Your Answer

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

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