Get Free Audit

Optimizing Complex Processes as Illustrated by Magento Catalog Price Rules

Mar 20, 2015

1904 Alex Simonchik

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:

Vlad Yunusov
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Vlad


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.

Vlad Yunusov
Partner With Us Looking for a partner that will help you to grow your business? We are the right company to develop your webstore. Feel free to get in touch with us. We will be happy to discuss your business opportunities and provide you with a Free Quote. Talk to Vlad
Tags:

8 Comments

  1. Hi Eugeny,
    Thank you for your comment!

    My article was written for Magento 1.7 or 1.6, much water has flown under the bridge since that time…
    It seems like Magento 1.9.3.2 hasn’t method applyAllRules and Fatal Error happens because of that. Please get in touch with our support team at [email protected] and they will discuss with you solution that will work on your Magento version.

  2. Thank you for your solution. But i have a problem: i try to use code described above and see that error after Finish...:

    Finish…applyAllRules…PHP Fatal error: Call to undefined method Mage_CatalogRule_Model_Resource_Rule::applyAllRules() in /home/ivoltinvest/web/domain.com/public_html/shell/applyRules.php on line 13

    Magento version is 1.9.3.2. Can you help me? Thanks.

  3. Thanks for sharing this video tutorial, I was working to set catalog price rules in my Magento store, Your vide and this post helped me a lot.

  4. Hi John,

    as it’s said above, all of the files are located in the Magento root folder. So, you should place the script there as well.

  5. 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.

  6. 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

Post a new comment

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