2

On the product page, I want to disable add to cart button on specific products.

5
  • 1
    You can create simple product attribute with Value Yes/No and set it to product and based on boolean value you can set condition inside addtocart.phtml file Commented Nov 15, 2017 at 6:02
  • Thanks, I have created attribute with name 'Disable add to cart''. Now in addtocart.phtml file, how I will call this attribute? Can you please tell me the code for it. Commented Nov 15, 2017 at 6:07
  • Can you share your attribute code? Commented Nov 15, 2017 at 6:08
  • I have created attribute with value Yes/No from the admin panel by going to Stores --> Attributes Commented Nov 15, 2017 at 6:13
  • did you check your attribute_code for above attributes? based on those attributes we need required to add condition Commented Nov 15, 2017 at 6:40

1 Answer 1

2

You can keep below code in your addtocart.phtml file, if your attribute name is disable_add_to_cart,

<?php if ($_product->getDisableAddToCart()): ?>   
    <div class="box-tocart">
        <div class="fieldset">
            <?php if ($block->shouldRenderQuantity()): ?>
            <div class="field qty">
                <label class="label" for="qty"><span><?php /* @escapeNotVerified */ echo __('Qty') ?></span></label>
                <div class="control">
                    <input type="number"
                           name="qty"
                           id="qty"
                           maxlength="12"
                           value="<?php /* @escapeNotVerified */ echo $block->getProductDefaultQty() * 1 ?>"
                           title="<?php /* @escapeNotVerified */ echo __('Qty') ?>" class="input-text qty"
                           data-validate="<?php echo $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
                           />
                </div>
            </div>
            <?php endif; ?>
            <div class="actions">
                <button type="submit"
                        title="<?php /* @escapeNotVerified */ echo $buttonTitle ?>"
                        class="action primary tocart"
                        id="product-addtocart-button">
                    <span><?php /* @escapeNotVerified */ echo $buttonTitle ?></span>
                </button>
                <?php echo $block->getChildHtml('', true) ?>
            </div>
        </div>
    </div>
    <?php endif; ?>
2
  • Thanks but it's not working, I have created only attribute set "disableAddToCart". Commented Nov 15, 2017 at 7:20
  • please add $_product->getDisableAddToCart() in condition in above mytypo mistake. Use getDisableAddToCart() Commented Nov 15, 2017 at 8:32

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.