0

Im running Magento2.3.4 and would like to disable the add to cart bottom on some product pages because they are to complicated to ship.

Have created an Yes/No attribute "afhentes" and I would like to disable the add to cart by enable this attribute.

One the same time I would like a text displayed indsteed: "Pick up in store"

Have found some around the net but they does not seem to work.

Hope someone can help med out with this matter.

5

2 Answers 2

1

Add this below code in your

app/design/frontend/[Theme_Vendor]/[Theme]/Magento_Catalog/templates/product/view/addtocart.phtml

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

// @codingStandardsIgnoreFile

/** @var $block \Magento\Catalog\Block\Product\View */
?>
<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php if ($_product->isSaleable()): ?>
    <?php if ($_product->getData('afhentes') != 1): ?>
        <div class="box-tocart">
            <div class="fieldset">
                <?php if ($block->shouldRenderQuantity()): ?>
                <div class="field qty">
                    <label class="label" for="qty"><span><?= /* @escapeNotVerified */ __('Qty') ?></span></label>
                    <div class="control">
                        <input type="number"
                            name="qty"
                            id="qty"
                            value="<?= /* @escapeNotVerified */ $block->getProductDefaultQty() * 1 ?>"
                            title="<?= /* @escapeNotVerified */ __('Qty') ?>"
                            class="input-text qty"
                            data-validate="<?= $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
                            />
                    </div>
                </div>
                <?php endif; ?>
                <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>
            </div>
        </div>
    <?php else: ?>
        <b><?= __('Pick up in store'); ?></b>
    <?php endif; ?>
<?php endif; ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "Magento_Catalog/js/validate-product": {}
        }
    }
</script>

Flush the cache and check, It works for me, Check this screenshot.

enter image description here

Hope this work for you.

16
  • I use the Ultimo theme so that might be the issue. addtocart.phtml is not in the app etc folder. Ultimo uses the file from the vendor dir. Commented Feb 24, 2020 at 14:09
  • So you can copy the vendor file in the ultimo theme path which i given and update the code. Commented Feb 24, 2020 at 14:32
  • Gets this error for the variable _product "Exception #0 (Exception): Notice: Undefined variable: _product in /var/www/testmigreparkogf.dev/public_html/vendor/magento/module-catalog/view/frontend/templates/product/view/addtocart.phtml on line 9" Commented Feb 24, 2020 at 14:38
  • have you replaced whole file? Commented Feb 24, 2020 at 14:40
  • Yes I have. Will look a bit closer and see what it could be Commented Feb 24, 2020 at 14:42
0

Override the product/view/addtocart.phtml in your custom theme or module and then you can add condition

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

// @codingStandardsIgnoreFile

/** @var $block \Magento\Catalog\Block\Product\View */
?>
<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php if ($_product->isSaleable()): ?>
    <?php if ($_product->getAfhentes()): ?>   
        <div class="box-tocart">
            <div class="fieldset">
                <?php if ($block->shouldRenderQuantity()): ?>
                <div class="field qty">
                    <label class="label" for="qty"><span><?= /* @escapeNotVerified */ __('Qty') ?></span></label>
                    <div class="control">
                        <input type="number"
                            name="qty"
                            id="qty"
                            value="<?= /* @escapeNotVerified */ $block->getProductDefaultQty() * 1 ?>"
                            title="<?= /* @escapeNotVerified */ __('Qty') ?>"
                            class="input-text qty"
                            data-validate="<?= $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
                            />
                    </div>
                </div>
                <?php endif; ?>
                <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>
            </div>
        </div>
    <?php else: ?>
        <p><?php /* @escapeNotVerified */ echo __('Pick up in store') ?></p>
    <?php endif; ?>
<?php endif; ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "Magento_Catalog/js/validate-product": {}
        }
    }
</script>
3
  • I replace my addtocart.phtml with this code correct? Commented Feb 23, 2020 at 14:35
  • my attribute yes/no name is: afhentes Commented Feb 23, 2020 at 14:41
  • Yes you need to also copy the product variable but now I have updated my answer so you can replace the addtocart.phtml in your theme or module Commented Feb 24, 2020 at 15:51

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.