Вопрос

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?

Это было полезно?

Решение

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()

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top