2

I created a custom attribute for hiding add to card button, it is working only for simple product. I want Hide to add to cart button for all product.

Create product attribute for hide_add_to_cart and placed below code in

addtocart.phtml

<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php if($_product->getData('hide_add_to_cart')) :?>
    <div class="box-tocart">
        <div class="fieldset">
           <div class="actions disablebtn">
                <button type="submit"

                        class="action primary tocart"
                        id="product-addtocart-button" disabled>
                         <?php //echo $_product->getData('hide_add_to_cart'); ?>
                </button>
                <?= $block->getChildHtml('', true) ?>
            </div>
        </div>
    </div>
    <?php else : ?>


    <?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><?= /* @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 endif; ?>
    <?php if ($block->isRedirectToCartEnabled()) : ?>
    <script type="text/x-magento-init">
        {
            "#product_addtocart_form": {
                "Magento_Catalog/product/view/validation": {
                    "radioCheckboxClosest": ".nested"
                }
            }
        }
    </script>
    <?php else : ?>
    <script type="text/x-magento-init">
        {
            "#product_addtocart_form": {
                "Magento_Catalog/js/validate-product": {}
            }
        }
    </script>
    <?php endif; ?>


    <?php endif; ?>
2
  • 1
    what code, you have tried..
    – Amit Bera
    Commented Aug 22, 2019 at 10:50
  • 1
    @AmitBera see updated my question
    – Priya
    Commented Aug 22, 2019 at 10:58

1 Answer 1

0

Hide Add To Cart Button

app\code\Vendorename\Modulename\etc

di.xml

<?xml version="1.0" encoding="UTF-8"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Product">
        <plugin name="hidecartbutton" type="Vendorename\Modulename\Plugin\Hidecartbutton" sortOrder="1"/>
    </type>
</config>

app\code\Vendorename\Modulename\Plugin

Hidecartbutton.php

<?php

namespace Vendorename\Vendorename\Plugin;

use Magento\Catalog\Model\Product;

class Hidecartbutton
{
    public function afterIsSaleable(Product $product, $isSaleable)
    {
        // Add your logic here
        return false;
    }
}

I Hope This Helps You.

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.