How to Manage Catalog Rules in Magento 2

Jan 17, 2019293Roman Kurilin
How to Manage Catalog Rules in Magento 2

This article is dedicated to an important Magento 2 Professional Developer certification – cart rules and its management. We will walk through the following aspects:

When would you use catalog price rules?
How do they impact performance?
How would you debug problems with catalog price rules?

When would you use catalog price rules in Magento 2?

Discounts are a powerful tool of existing customers retention and attracting new ones, so the majority of online shops usually have them – either seasonal or associated with some special occasion. Magento 2 ecommerce platform allows admin to greatly automate the process of assigning discounts with the help of catalog price rules. Сatalog price rules are used to give a discount on a product or a bundle of products.

How do they impact performance in Magento 2?

It has minimal impact on loading of the page because price calculations are performed during reindexing. Therefore, the more massive the catalog price rules are, the longer the reindexing will be performed

To make sure that catalog price rules don’t overload the performance let’s have a look at Magento code. In the vendor\magento\module-catalog-rule\etc\frontend\events.xml file we install the observer for catalog_product_get_final_price event.

Then we get the price from magento\module-catalog-rule\Observer\ProcessFrontFinalPriceObserver.php file and set it up for the product.

Now let’s consider the getRulePrice() method. Find this method in Z:\m2.loc\vendor\magento\module-catalog-rule\Model\ResourceModel\Rule.php file and find this method here; getRulePrice() method will call the getRulePrices() that performs a simple inquiry to the catalogrule_product_price table from the database. This is how you get the price.

How would you debug problems with catalog price rules in Magento 2?

To begin with, make sure that the price rule is active by going to Marketing -> Promotions > Catalog Price Rule and checking that the status is set at active. Now let’s apply the rules by pressing Apply Rules button.

catalog price rule magento 2

After that, the Catalog Rule Product indexes and Product Price, connected with it, are marked as invalid.

index management magento 2

Meanwhile, in the controller vendor\magento\module-catalog-rule\Controller\Adminhtml\Promo\Catalog\ApplyRules.php
an increment of \Magento\CatalogRule\Model\Rule\Job class is created; this increment calls the applyAll() method.

In this method the index is marked as not valid:

Then we need to launch reindex and clear the cash.
bin/magento indexer:reindex
bin/magento cache:flush
Afterwards, the rules will work, if, of course, price rule, its date ranges and conditions are configured correctly (date ranges and conditions).

In case they don’t work, you need to go to catalogrule_product_price table and check the product price there. If you found that the data is not correct, you will need to check the logs or try to disable the third-party modules if you have one; you can also go over the Catalog Price Rule reindexing with Xdebug extension – it will detect the issue.

This is all on the topic of cart rules management in Magento 2. If you have any questions or comments, please leave them down below.

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *