In ecommerce stores, showing the right product price is very important. Sometimes, products in Magento 2 may show a price of zero by mistake. If customers add these to their cart, it can confuse and lead to lost sales. We can restrict zero-price products from being added to the cart to avoid this. Magento 2 makes this easy using its event-observer system.
In this blog, learn how to create a simple custom module that blocks zero-priced products from being added to the cart. You’ll also learn how to add settings in the admin to turn this on or off and change the error message.
ON THIS PAGE
5 Simple Steps to Restrict Add to Cart Action for Zero Price Products in Magento 2

Step 1: Create the Module Registration File
The first step is to register your custom module using registration.php.
Path: app/code/Klizer/RestrictZeroPrice/registration.php
<?php
/**
* Klizer
*
* @category Klizer
* @package Klizer_RestrictZeroPrice
* @copyright Copyright © 2025 Klizer. All rights reserved.
* @author Klizer - info@klizer.com
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Klizer_RestrictZeroPrice',
__DIR__
);
Step 2: Define the Module in module.xml
Once registered, define the module using the path below:
Path: app/code/Klizer/RestrictZeroPrice/etc/module.xml
<?xml version="1.0"?>
<!--
/**
* Klizer
*
* @category Klizer
* @package Klizer_RestrictZeroPrice
* @copyright Copyright © 2025 Klizer. All rights reserved.
* @author Klizer - info@klizer.com
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework: Module/etc/module.xsd">
<module name="Klizer_RestrictZeroPrice" setup_version="1.0.0"/>
</config>
Step 3: Define the Event in events.xml
Event Name: checkout_cart_product_add_before
Then you need to define the event using the path below:
Path: app/code/Klizer/RestrictZeroPrice/etc/events.xml
<?xml version="1.0"?>
<!--
/**
* Klizer
*
* @category Klizer
* @package Klizer_RestrictZeroPrice
* @copyright Copyright © 2025 Klizer. All rights reserved.
* @author Klizer - info@klizer.com
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework: Event/etc/events.xsd">
<event name="checkout_cart_product_add_before">
<observer name="restrict_zero_price_product" instance="Klizer\RestrictZeroPrice\Observer\CheckZeroPrice"/>
</event>
</config>
Step 4: Create observer CheckZeroPrice.php
Now, create an observer file in the path below:
Path: app/code/Klizer/RestrictZeroPrice/Observer/CheckZeroPrice.php
<?php
/**
* Klizer
*
* @category Klizer
* @package Klizer_RestrictZeroPrice
* @copyright Copyright © 2025 Klizer. All rights reserved.
* @author Klizer - info@klizer.com
*/
namespace Klizer\RestrictZeroPrice\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Event\Observer;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
class CheckZeroPrice implements ObserverInterface
{
protected $scopeConfig;
public function __construct(ScopeConfigInterface $scopeConfig)
{
$this->scopeConfig = $scopeConfig;
}
public function execute(Observer $observer)
{
// Get Config Values
$isEnabled = $this->scopeConfig->isSetFlag('restrict_zero_price/general/enable', ScopeInterface::SCOPE_STORE);
$errorMessage = $this->scopeConfig->getValue('restrict_zero_price/general/error_message', ScopeInterface::SCOPE_STORE);
if (!$isEnabled) {
return;
}
$product = $observer->getEvent()->getProduct();
$price = $product->getPrice();
if ($price == 0) {
throw new LocalizedException(__($errorMessage));
}
}
}
Step 5: Create System configuration file system.xml
Now, create the System configuration file in the path below:
Path: app/code/Klizer/RestrictZeroPrice/etc/adminhtml/system.xml
<?xml version="1.0"?>
<!--
/**
* Klizer
*
* @category Klizer
* @package Klizer_RestrictZeroPrice
* @copyright Copyright © 2025 Klizer. All rights reserved.
* @author Klizer - info@klizer.com
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="klizer" translate="label" sortOrder="100">
<label>Klizer Extensions</label>
</tab>
<section id="restrict_zero_price" translate="label" type="text" sortOrder="999999" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Restrict Zero Price</label>
<tab>klizer</tab>
<resource>Klizer_RestrictZeroPrice::config</resource>
<group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>General Settings</label>
<field id="enable" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable Restriction</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="error_message" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Error Message</label>
<validate>required-entry</validate>
</field>
</group>
</section>
</system>
</config>
=> Frontend :

Here are the screenshots of the admin panel: Admin Panel -> Stores -> Settings -> Configuration -> Klizer -> Restrict Zero Price
From here, you can enable/disable the module and set the error message.

Securing Your Store by Restricting Zero-Price Products from Being Added to Cart
Preventing zero-priced products from being added to the cart helps protect your store from accidental purchases and lost revenue. With Magento 2’s flexible event-observer system, you can apply this restriction easily using a custom module and admin settings.
You can consider working with the best Magento solution partners like Klizer for more advanced features or help setting this up. You can also explore our expert Magento services to optimize your store for better performance and security.