4

I have certain products, which customers can buy only one.

So whether needs to remove Qty box for that products only or can be managed from settings?

1
  • You can make attribute to add condition for limit in qty box box or hide qty box
    – Kul
    Jan 17, 2017 at 9:26

2 Answers 2

5

There is a config in Admin: Log in to Admin > PRODUCTS > Catalog > choose a Product > Advanced Inventory > Maximum Qty Allowed in Shopping Cart > set to 1.

enter image description here

Overwrite as below

magento\app\design\frontend\Vendor\YourThemeName\Magento_Catalog\templates\product\view\addtocart.phtml

Replace

if ($block->shouldRenderQuantity())

With

if ($block->shouldRenderQuantity() && ($_product->getExtensionAttributes()->getStockItem()->getMaxSaleQty() != 1)):

U need to add

$_product->getExtensionAttributes()->getStockItem()->getMaxSaleQty()
4
  • Not able to get value in magento\vendor\magento\module-catalog\view\frontend\templates\product\view\addtocart.phtml
    – Jackson
    Jan 17, 2017 at 9:49
  • Okay! I will check again. Jan 17, 2017 at 9:52
  • Able to get $_product->getStockItem($_product->getId()) still max_sale_qty missing
    – Jackson
    Jan 17, 2017 at 9:58
  • Should try: $_product->getExtensionAttributes()->getStockItem()->getMaxSaleQty() Jan 17, 2017 at 10:06
2

Create an after plugin for shouldRenderQuantity. In your di.xml, add this.

<type name="Magento\Catalog\Block\Product\View">
    <plugin name="Vendor_Module::configurable-no-render-quantity-field" type="Vendor\Module\Plugin\Block\Product\View" />
</type>

Then in your plugin, do something like this:

public function afterShouldRenderQuantity(\Magento\Catalog\Block\Product\View $subject, $result)
{
    if ($subject->getProduct()->getTypeId() === \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
        return false;
    }
    
    return $result;
}

Modify the if condition in my sample to achieve what you want.

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.