Optimizing Complex Processes as Illustrated by Magento Catalog Price Rules

March 20, 2015 Alex Simonchik Magento
Optimizing Complex Processes as Illustrated by Magento Catalog Price Rules

In Magento there is a sufficient number of complex data processes, which may take quite a long time and consume a large amount of server memory and performance, especially if the database contains a huge pool of data (products, categories, customers, orders, etc). The indexing of products attributes, categories, prices, etc may serve as an example of such a process. But into this list we can also include one more process – the application of catalog price rules (apply rules):

Optimizing Complex Processes

If your directory has a big list of products and pricing rules, the Apply Rules process most probably will not be able to complete, but instead will take a lot of time and effort.

The application of price rules works as follows: we have a collection of all active rules and they all perform their own Apply command in a loop, and after the cycle the reindexing of catalog prices is implemented.

I suggest implementing every rule as a separate shell-command to reduce the memory consumption by the number equal to the number of price rules. And then run the indexation as a separate shell-command too.

For this we will create a shell file which will execute the aforementioned commands in sequence. The file will look as follows:

As you see, we have two php scripts. The first аpplyRulesByOne.php script contains the parameter start and generates the list of all active price rules and stores their IDs into the ruleIds file. If the parameter start is replaced with a number, then the script is looking for the rule with the ID equal to that number and executes this rule.

The Script аpplyRules.php cleans cache and starts the indexation of directory prices.

So, if we take a closer look at our shell file, you will notice that it primarily generates IDs of all active price rules and launches the shell-command for its single execution.  After that the indexation is called and cache gets cleared.

My shell-file is launched by a cron-job so now I am not worried that my store may have any problems with discounts :)

The source code of the аpplyRulesByOne.php file:

The source code of the file applyRules.php:

All of these files are located in the Magento root folder. This is indicated in this line:

Conclusion! Such approach makes it possible to implement heavy scripts that are processing data in a loop.

Find one more solution here.



2 comments

  1. would this work on magento 1.9.2.1 ? Can you provide instructions to add it as a cron job? I get an error

    Could not open input file: applyRulesByOne.php
    Could not open input file: applyRulesByOne.php
    Could not open input file: applyRules.php

    when my cron is executed

  2. Also check out the logic inside of Mage_CatalogRule_Model_Rule::callbackValidateProduct() if you have a lot of websites. Magento is checking products on all websites, even if your rule only applies to a single website.

Post a new comment

top