Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CatalogRule start date insert logic #1007

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

mmenozzi
Copy link
Contributor

For the same reason the rule is inserted until 24 hours after the rule end date, we should also start inserting it 24 hours before the start date. Am I wrong?

@mmenozzi mmenozzi changed the title Fix CatalogRule insert logic Fix CatalogRule start date insert logic May 27, 2020
@kiatng
Copy link
Contributor

kiatng commented May 28, 2020

I think the code is correct. I made a test:

// Original code.
$fromTime = (int) strtotime('2020-01-01');
$toTime = (int) strtotime('2020-01-02');
$toTime = $toTime ? ($toTime + 86400 - 1) : 0;
$result = [date('r', $fromTime), date('r', $toTime)];

The result is

array(2) {
  [0] => string(31) "Wed, 01 Jan 2020 00:00:00 +0000"
  [1] => string(31) "Thu, 02 Jan 2020 23:59:59 +0000"
}

Now with your code:

// Changed code.
$fromTime = (int) strtotime('2020-01-01');
$fromTime = $fromTime ? ($fromTime - 86400 + 1) : 0;
$toTime = (int) strtotime('2020-01-02');
$toTime = $toTime ? ($toTime + 86400 - 1) : 0;
$result = [date('r', $fromTime), date('r', $toTime)];

The result is wrong:

array(2) {
  [0] => string(31) "Tue, 31 Dec 2019 00:00:01 +0000"
  [1] => string(31) "Thu, 02 Jan 2020 23:59:59 +0000"
}

@mmenozzi
Copy link
Contributor Author

When we faced this issue it was a timezone issue. I remember that the day before the Black Friday we set a rule to star on the next day. We are in the Europe/Rome timezone but we have an Australian store view with Sidney timezone, so we expected to see the rule already applied in the AU store view because it was already the Black Friday day there.

I remember we debugged locally and the problem was that the rule wasn’t added at the moment we created it but it should because of the Sidney timezone. So we fixed with this change.

@kiatng
Copy link
Contributor

kiatng commented May 28, 2020

Could it be that the Australian store timezone was not configured correctly in the backend?

@mmenozzi
Copy link
Contributor Author

No it was configured correctly.

@seansan
Copy link
Contributor

seansan commented May 28, 2020

There have been some changes on this in the past also ... same problem ... I remember it was fixed (Amsterdam timezone) .... it has to do with time of running, core time of the (unix) server and the timezone.

@sreichel sreichel added Component: CatalogRule Relates to Mage_CatalogRule review needed Problem should be verified labels Jun 5, 2020
@ADDISON74
Copy link
Contributor

We should move forward this PR. @kiatng - what is your opinion?

@fballiano
Copy link
Contributor

I coulnd't really understand why this solutions fixes the problem but I think we should never use +-86400 seconds since it doesn't work the day of the daylight saving +-1 hour change.

@kiatng
Copy link
Contributor

kiatng commented May 30, 2022

We should move forward this PR. @kiatng - what is your opinion?

I think we need to replicate the issue first.

@fballiano
Copy link
Contributor

ok since nobody knows what this is about and how to replicate, I'm closing it.

@fballiano fballiano closed this May 30, 2022
@ADDISON74
Copy link
Contributor

I would leave it open. I will make time to replicate the issue. If it confirms I will go further to implement the PR and check what is happening.

@ADDISON74 ADDISON74 reopened this May 30, 2022
@fballiano
Copy link
Contributor

@ADDISON74 I would close this, @kiatng tested the coded and seemed not work #1007 (comment) (that result is wrong for sure) but using SECONDS_IN_DAY is also not a super good practise since it doesn't work the day of daylightsaving change.

@fballiano fballiano closed this Jan 13, 2023
@fballiano
Copy link
Contributor

maybe it could be converted to an issue but we need a full reproducible case

@mmenozzi
Copy link
Contributor Author

Hi guys, I'll try to provide a way to reproduce the issue in the next days.

@mmenozzi
Copy link
Contributor Author

mmenozzi commented Jan 16, 2023

Hi guys, the bug still exist. At least on version 19.4.21 of OpenMage.
Here the steps to reproduce it:

  1. First of all make sure that the current time of the machine you'll use to do the test is after the 3 PM.
  2. Install OpenMage 19.4.21 with sample data, paying attention to configure Europe/Rome as default timezone
  3. Login to admin panel and create a website, called "Australian", with a store and a store view in it
  4. Go in System -> Configuration -> General -> General, select the "Australian" website in the scope menu and configure the "AUS Eastern Standard Time (Australia/Sydney)" as timezone for the "Australian" website.
  5. Go to Catalog -> Manage products, select all products, select the "Update attributes" mass action and add all products to the "Australian" website.
  6. Now go in Promotions -> Catalog Price Rules and create a new active rule starting from tomorrow (for example if today is 2023-01-16, set the start date to 2023-01-17), which discounts all the products by 20%. Apply the rule, reindex everything and clear the cache.
  7. Now go in the frontend, in the Australian store view. Search for the product "AVIATOR SUNGLASSES" and open its product page.

If the current time is 3 PM of the 2023-01-16 in Europe/Rome, it means that in the Australia/Sydney timezone the current time is 2 AM (or 1 AM depending on the daylight saving time) of the 2023-01-17. This means that on the "Australian" store view the promotion should be already applied and we should see the discounted price of 236 for the "AVIATOR SUNGLASSES"; but what happens is that we still see the full price of 295.

If you apply the patch provided in this PR and you save and apply the catalog price rule again you'll get the discounted price.

/cc @fballiano @ADDISON74 @kiatng @seansan

@kiatng
Copy link
Contributor

kiatng commented Jan 17, 2023

Note changing fromTime to one day earlier may cause the rule to be applied too early, although it works in the test case. Here, $fromTime is inserted in the table.

->columns([
new Zend_Db_Expr($store->getWebsiteId()),
'cg.customer_group_id',
'p.entity_id',
new Zend_Db_Expr($rule->getId()),
new Zend_Db_Expr($fromTime),

[edit]

To add the rule ahead of time, change line 209

$timestamp = time();
if ($fromTime > $timestamp
|| ($toTime && $toTime < $timestamp)
) {
return;
}

to

        if ($fromTime > $timestamp + self::SECONDS_IN_DAY

@mmenozzi Can you test line 209 without changing $fromTime and see if it works for you?

@sreichel
Copy link
Contributor

Note changing fromTime to one day earlier may cause the rule to be applied too early

Also seen this .... the topic is slowly growing a beard ...

@luigifab
Copy link
Contributor

luigifab commented Jan 20, 2023

I tested the test case with and without the PR, and this is not working, like @kiatng says the rule is applied to early.

australian website:
image

default website:
image

my rule 20% for tomorrow:
image

By applying rules and reindexing at Friday, 20 January 2023 14:13:08 Europe/Paris or Saturday, 21 January 2023 12:12:21 am Australia/Sydney, this does not change anything (with the PR - without the PR price are not discounted).

@kiatng
Copy link
Contributor

kiatng commented Jan 21, 2023

Line 209:

        if ($fromTime > $timestamp
            || ($toTime && $toTime < $timestamp)
        ) {

The if statement is to restrict the number of rules to be added to the table. If the condition(s) is(are) true, it will skip the rule. The problem with this PR is that it changed the $fromTime, which is then added to the table resulting in the rule being applied too early.

Instead, we can change it to

        if ($fromTime > $timestamp + self::SECONDS_IN_DAY
            || ($toTime && $toTime < $timestamp)
        ) {

Note that the comparison for $toTime is correct as it is because the time is passed due date, the rule is skipped. However, the $fromTime is compared to 24 hours later; if $fromTime is not yet tomorrow, add the rule. Why this $timestamp + self::SECONDS_IN_DAY, has 24 hours added? It is because the cron runs only once a day. If the cron runs every 12 hours, then we do $timestamp + self::SECONDS_IN_DAY/2

@luigifab Can you test if this works.

[edit]
image

@luigifab
Copy link
Contributor

luigifab commented Jan 26, 2023

With @kiatng suggestion, discount is not applied too early, BUT to display the discount, I had to "Save and apply" my rule at 26/01/23 13:08:49 utc / Friday, 27 January 2023 12:08:49 am Australia/Sydney.

Before applying the rule, I forgot to add the product to the cart to check the price. I will try again.

@luigifab
Copy link
Contributor

It seems that the @kiatng solution works. @mmenozzi can you try?

I need to run catalogrule_apply_all (same as apply rules I think, because the cron job is started at night when my computer is off). Perhaps it is necessary to run this cron job more than once per day.

@mmenozzi mmenozzi force-pushed the patch-1 branch 2 times, most recently from 51b2e94 to 3037ae0 Compare February 14, 2023 17:50
@github-actions github-actions bot removed Component: Core Relates to Mage_Core Component: CatalogSearch Relates to Mage_CatalogSearch Component: Dataflow Relates to Mage_Dataflow Component: CatalogIndex Relates to Mage_CatalogIndex Component: CatalogRule Relates to Mage_CatalogRule phpcs Component: Cm/RedisSession Relates to Cm_RedisSession Component: Api PageRelates to Mage_Api ddev Mage.php Relates to app/Mage.php Component: ConfigurableSwatches Relates to Mage_ConfigurableSwatches Component: Adminhtml Relates to Mage_Adminhtml Component: Customer Relates to Mage_Customer Component: CurrencySymbol Relates to Mage_CurrencySymbol phpunit Component: Admin Relates to Mage_Admin labels May 12, 2023
@github-actions github-actions bot added the Component: CatalogRule Relates to Mage_CatalogRule label May 12, 2023
@fballiano
Copy link
Contributor

so sorry guys, I've rebased the PR and it screwed everything... I've rebuilt it (adding my strtotime() suggestions), do you think it misses something?

@luigifab
Copy link
Contributor

Not sure about changes between the version of the PR I tested and now.

@fballiano
Copy link
Contributor

The 86400 second cannot be used, it does not work 2 days a year, that i am more than sure

@ADDISON74
Copy link
Contributor

This issue reminds me of another one that I discussed in another forum https://magento.stackexchange.com/questions/133405/issue-when-specialpricefromdate-is-equal-with-specialpricetodate/

What happens when price_from is perfectly equal to price_to? For example both having the value 2023-07-16 00:00. Naturally it should run for a day.

@luigifab
Copy link
Contributor

Is it possible to deploy the PR on demo.openmage.org to test it again?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: CatalogRule Relates to Mage_CatalogRule review needed Problem should be verified
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants