質問

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