1

I have looked through many similar questions, but many seem either far more convoluted than is necessary, or just doesn't make sense for my needs.

As the title states, I would like to remove the button if the product price is set to $0.00. Or better yet, I'd like to have the button read "Coming Soon" instead of "Add to Cart" when the price is $0.00.

I figure this isn't too difficult for some of you seasoned veterans on here, but I honestly don't know where to start.

This will be implemented on Magento 2.2.5

3
  • Where you remove this button on listing or detail page ? Commented Aug 6, 2018 at 7:03
  • Please check my answer and let me know if question will not solve. Commented Aug 6, 2018 at 9:53
  • I'd like to remove it from both listing and detail page. Commented Aug 7, 2018 at 5:58

2 Answers 2

1

If you want to remove button in product details page, you need to override the app/design/frontend/[themename]/default/Magento_Catalog/templates/product/view/addtocart.phtml in your custom theme. Then, add below code :

<?php if ($_product->getPrice() > 0):?>
    <div class="actions">
        <button type="submit"
                    title="<?= /* @escapeNotVerified */ $buttonTitle ?>"
                    class="action primary tocart"
                    id="product-addtocart-button">
            <span class="add-cart btn"><i class="fa fa-shopping-cart"></i><?= /* @escapeNotVerified */ $buttonTitle ?></span>
        </button>
        <?= $block->getChildHtml('', true) ?>
    </div>
<?php endif; ?>

I hope it will help you.

0

Override list.phtml [For catalog category view] file at

app/design/frontend/<themename>/default/Magento_Catalog/templates/product/list.phtml

and apply this customization at line no 78

<?php if ($_product->isSaleable()): ?>
    <?php if ($_product->getPrice() > 0): ?>
    <?php $postParams = $block->getAddToCartPostParams($_product); ?>
    <form data-role="tocart-form" data-product-sku="<?= $block->escapeHtml($_product->getSku()) ?>" action="<?= /* @NoEscape */ $postParams['action'] ?>" method="post">
        <input type="hidden" name="product" value="<?= /* @escapeNotVerified */ $postParams['data']['product'] ?>">
        <input type="hidden" name="<?= /* @escapeNotVerified */ Action::PARAM_NAME_URL_ENCODED ?>" value="<?= /* @escapeNotVerified */ $postParams['data'][Action::PARAM_NAME_URL_ENCODED] ?>">
        <?= $block->getBlockHtml('formkey') ?>
        <button type="submit"
                title="<?= $block->escapeHtml(__('Add to Cart')) ?>"
                class="action tocart primary">
            <span><?= /* @escapeNotVerified */ __('Add to Cart') ?></span>
        </button>
    </form>
    <?php else: ?>
        <div class="action tocart primary"><span><?= /* @escapeNotVerified */ __('Coming Soon') ?></span></div>
    <?php endif; ?>
<?php else: ?>
    <?php if ($_product->isAvailable()): ?>
        <div class="stock available"><span><?= /* @escapeNotVerified */ __('In stock') ?></span></div>
    <?php else: ?>
        <div class="stock unavailable"><span><?= /* @escapeNotVerified */ __('Out of stock') ?></span></div>
    <?php endif; ?>
<?php endif; ?>

override the addtocart.phtml [For catalog product view] file at below path and add this code :

app/design/frontend/<themename>/default/Magento_Catalog/templates/product/product/view/addtocart.phtml

<?php  if ($_product->getPrice() > 0) : ?>
        <div class="actions">
            <button type="submit"
                    title="<?= /* @escapeNotVerified */ $buttonTitle ?>"
                    class="action primary tocart"
                    id="product-addtocart-button">
                <span><?= /* @escapeNotVerified */ $buttonTitle ?></span>
            </button>
            <?= $block->getChildHtml('', true) ?>
        </div>
        <?php else: ?>
            <button type="button"
                    title="<?= $block->escapeHtml(__('Coming Soon')) ?>"
                    class="action tocart primary">
                <span><?= /* @escapeNotVerified */ __('Coming Soon') ?></span>
            </button>
        <?php endif; ?>

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.