On the product page, I want to disable add to cart button on specific products.
-
1You 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– Rakesh JesadiyaCommented 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.– Syed Muneeb Ul HasanCommented Nov 15, 2017 at 6:07
-
Can you share your attribute code?– Rakesh JesadiyaCommented Nov 15, 2017 at 6:08
-
I have created attribute with value Yes/No from the admin panel by going to Stores --> Attributes– Syed Muneeb Ul HasanCommented 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– Rakesh JesadiyaCommented Nov 15, 2017 at 6:40
Add a comment
|
1 Answer
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; ?>
-
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