lechowski / 13.11.2021

Do not apply cart rule if a product belongs to a category X

In this little tutorial, we'll show you how to create a cart rule that only applies if cart products do NOT belong to a certain category. For example, we wish to have a discount of 10% for the entire catalog, but we do not want that discount applied if the products purchased belong to the category HOT SALE (ID 10).

First we create the discount rule as we normally do, and then modify the CartRule.php file.

The file that is responsible for validating the coupons is the classes / CartRule.php, inside it, there is a function called "checkValidity".

What we will do is add our code to the beginning of this function, but for this we can create a file called CartRule.php inside the folder overrides / classes / and create the override, or directly modify the original file in classes / CartRule.php ( If we update PrestaShop the change is lost and we must do it again). There are hundreds of tutorials to do overrides, so we will focus on the function itself.

 

The only thing we need to change is the category code that prevents the rule from being applied to apply the Voucher. In this case it is 10