12

I have developed the online store on magento platform. Everything works fine except the Catalog price rule for sale. I have created the simple rule that applies 15% discount on all products. When I save and apply the rule, it works fine the whole day. But after 12 at night, the sale is no more visible. I tried to apply the rules from the admin panel by clicking on apply rules, but at night it does not even allow me to apply rules manually. I can apply the rule next day in the morning and the it works fine but again till 12 am.

I have tried the following. I have removed the special prices that were defined separately on products, so the sale is implemented only by price rule. I have set the cron job but it also does not work at night. I have check the date and timings for the rules.

I found that it could be because of time settings. I am using GMT+5. Is there anything that can be done to keep the rule working for day and night.

Thanks

Mehak
  • 143
  • 1
  • 2
  • 6

5 Answers5

24

Yes, this is a bug in Magento (or some logic beyond my understanding). When Magento displays products on frontend, it checks if there are catalog rules for this date. And the date used for this check is your local, so in your case GMT+5. However, when catalog rules are being applied, it uses GMT date. So that means that you aren't able to apply rules until 5 AM.

The problem is in Mage_CatalogRule_Model_Action_Index_Refresh::execute() method. You will have to rewrite this function/class either in your extension, or via the local version of the file.

File location: app/code/core/Mage/CatalogRule/Model/Action/Index/Refresh.php

You have to replace line 121:

$timestamp = $coreDate->gmtTimestamp('Today');

with this line:

$timestamp = Mage::app()->getLocale()->date(null, null, null, true)->get(Zend_Date::TIMESTAMP);

After that you should be able to apply the rules.

Roman Snitko
  • 3,655
  • 24
  • 29
Alexei Yerofeyev
  • 2,093
  • 1
  • 17
  • 21
  • thanks for your reply. By creating a local version, did you mean to create the same hierarchy in app/code/local/Mage ? – Mehak Aug 13 '14 at 10:19
  • Yes, exactly. It is not the healthiest way for Magento modification, but it is the easiest one. – Alexei Yerofeyev Aug 13 '14 at 11:17
  • Yes. I applied the solution and it worked great. The Price rule remained enable after 12. Thanks a million. – Mehak Aug 14 '14 at 12:11
  • could you please point the path to the location of the original file? – Mike Jan 18 '15 at 14:55
  • The path is app/code/core/Mage/CatalogRule/Model/Action/Index/Refresh.php for Magento 1.8+. If you have older Magento version, then I'm afraid this solution won't help you. – Alexei Yerofeyev Jan 18 '15 at 22:06
  • You should get a medal for this answer! You are a saviour! Great find and fix! – Abul Hassan Feb 13 '17 at 20:13
  • +1 for figuring out core bug. I want to point out that `$timestamp = $coreDate->gmtTimestamp();` will have the same effect. *Today* that's passed as parameter to `strtotime` causes issues. – Mladen Ilić Sep 08 '17 at 14:00
4

In magento 1.9.2.2 this didn't work for me. I installed AOE scheduler and using it i changed the catalogrule_apply_all cron expression from 0 1 * * * to 30 */6 * * * and it started working. Hope this helps someone.

Lakshin Karunaratne
  • 446
  • 1
  • 6
  • 19
1

As stated above by @Alexei-Zerofezev the problem is with the indexer.

I had this problem as well, the issue exists when there is an offset of the local timezone greater than +01:00.

Basically just because magento is using the gmtTimestamp for the rule date which in the above stated case results in the day before today.

Therefor I developed a small module https://github.com/Chuvisco88/Chuvisco_CatalogRuleFix to fix the issue. If someone ever has this problem, please give it a try.

Chuvisco
  • 101
  • 1
  • 6
0

I used a shell script instead of the (huge) AOE page @Lakshin Karunaratne suggested.

require_once 'abstract.php';

class X043_Shell_PriceRuleSetter extends Mage_Shell_Abstract
{
    public function run()
    {
        // stuff and thingies
        umask(0);
        Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
        setlocale(LC_ALL, 'en_US.UTF-8'); 

        try {
            Mage::getModel('catalogrule/rule')->applyAll();
            Mage::getModel('catalogrule/flag')->loadSelf()
                ->setState(0)
                ->save();
        } catch (Mage_Core_Exception $e) {
            Mage::logException($e);
        } catch (Exception $e) {
            Mage::logException($e);
        }

        return $this;
    }
}

$shell = new X043_Shell_PriceRuleSetter();
$shell->run();
PvdL
  • 1,578
  • 1
  • 20
  • 33
0

Extends lakshin-karunaratne answer above.

Make sure you move cataloguerule_apply_all away from midnight but not before catalog_product_index_price_reindex_all.

From "Solving Magento"
"Important is that rule prices must be calculated for the current day and before the catalog price re-indexing starts. Otherwise the observer will not get any active rule prices to augment the price index."

Therefore you would need to calculate the offset from GMT time to ensure both cataloguerule_apply_all and catalog_product_index_price_reindex_all are executed on the same day.

This can be easily achieved by using AOE Scheduler https://github.com/AOEpeople/Aoe_Scheduler

hejhog
  • 243
  • 1
  • 2
  • 8