I have to remove add to cart button from product detail page when product store has 0. How to achieve this
-
you can get store id and add condition to show or not the add to cart button.– AshwiniCommented Jan 31, 2017 at 6:38
-
@Ashwini, How it's? Can you give small demo.– PinkuCommented Jan 31, 2017 at 7:06
-
blog.chapagain.com.np/… Follow this link to get store id in magento2– AshwiniCommented Jan 31, 2017 at 9:33
-
and using addStoreFilter() this method you can apply condition.– AshwiniCommented Jan 31, 2017 at 9:37
2 Answers
Override the template by copying Magento\Catalog\view\frontend\templates\product\view\addtocart.phtml
to Vendor\Module\view\frontend\templates\product\view\addtocart.phtml
.
Original code:
<?php
/**
* Copyright © 2016 Magento. 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()): ?>
<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; ?>
<script type="text/x-magento-init">
{
"#product_addtocart_form": {
"Magento_Catalog/product/view/validation": {
"radioCheckboxClosest": ".nested"
}
}
}
</script>
<?php if (!$block->isRedirectToCartEnabled()) : ?>
<script type="text/x-magento-init">
{
"#product_addtocart_form": {
"catalogAddToCart": {
"bindSubmit": false
}
}
}
</script>
<?php endif; ?>
Replace line 13 with:
<?php
/**
* Copyright © 2016 Magento. 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() && $_product->getExtensionAttributes()->getStockItem()->getQty()): ?>
<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; ?>
<script type="text/x-magento-init">
{
"#product_addtocart_form": {
"Magento_Catalog/product/view/validation": {
"radioCheckboxClosest": ".nested"
}
}
}
</script>
<?php if (!$block->isRedirectToCartEnabled()) : ?>
<script type="text/x-magento-init">
{
"#product_addtocart_form": {
"catalogAddToCart": {
"bindSubmit": false
}
}
}
</script>
<?php endif; ?>
Notice the difference: <?php if ($_product->isSaleable() && $_product->getExtensionAttributes()->getStockItem()->getQty()): ?>
Create or update file Vendor\Module\view\frontend\layout\catalog_product_view.xml
:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="product.info.addtocart" template="Vendor_Module::product/view/addtocart.phtml"/>
</body>
</page>
Flush your cache & (re)load the product view page.
-
-
Hi @Pinku, have you overriden that template successfully? Commented Feb 2, 2017 at 2:22
-
Hi @Rendy, Not yet, i am still working. if you have code than or idea then please share. So that i can go ahead– PinkuCommented Feb 6, 2017 at 5:08
-
I have added the
catalog_product_view.xml
to override the template. ChangeVendor
andModule
to fit your vendor and module name. Commented Feb 6, 2017 at 8:58
This Code is remove 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)
{
// get product data
$page_name = $this->request->getFullActionName();
$page_arr = [];
$page_arr[] = "catalog_product_view";
if (in_array($page_name, $page_arr)) {
return false;
} else {
return $product->isSalable();
}
}
}