magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

magento/magento2#39959 catalog_product_save_before observer throws date-related error when using REST API without store-level values (getFinalPrice() issue)

Open KrasnoshchokBohdan opened this issue 5 months ago • 3 comments

Adjusted the processing of SpecialFromDate to ensure proper formatting when the date is provided as a DateTimeInterface instance. This prevents errors arising during getFinalPrice() execution under certain scenarios.

Description (*)

I focused on investigation error TypeError: strtotime(): Argument #1 ($datetime) must be of type string, DateTime given in lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php:275 the reason for it must be \Magento\Catalog\Observer\SetSpecialPriceStartDate::execute image

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes magento/magento2#39959

Manual testing scenarios (*)

  1. Create a custom module with an observer for catalog_product_save_before.
  2. Inside the observer, call $product->getFinalPrice().
  3. Save product from storeview scope from admin panel (check the video from issue description)
  4. Save a product using REST API: POST /rest/default/V1/products
  5. Use the following JSON payload (no store-level pricing):
payload
{
  "product": {
    "sku": "testing-special-date",
    "price": 120,
    "status": 0,
    "visibility": 4,
    "type_id": "simple",
    "extension_attributes": {
      "stock_item": {
        "stock_id": 1,
        "qty": 207,
        "is_in_stock": true
      }
    },
    "custom_attributes": [
      {
        "attribute_code": "tax_class_id",
        "value": "2"
      },
      {
        "attribute_code": "special_price",
        "value": "100.00"
      }
    ]
  },
  "saveOptions": true
}

Questions or comments

Contribution checklist (*)

  • [ ] Pull request has a meaningful description of its purpose
  • [ ] All commits are accompanied by meaningful commit messages
  • [ ] All new or changed code is covered with unit/integration tests (if applicable)
  • [ ] README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • [ ] All automated tests passed successfully (all builds are green)

KrasnoshchokBohdan avatar Jun 17 '25 16:06 KrasnoshchokBohdan

Hi @KrasnoshchokBohdan. Thank you for your contribution! Here are some useful tips on how you can test your changes using Magento test environment. :exclamation: Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s) For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here :information_source: Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation. Join Magento Community Engineering Slack and ask your questions in #github channel.

m2-assistant[bot] avatar Jun 17 '25 16:06 m2-assistant[bot]

@magento run all tests

KrasnoshchokBohdan avatar Jun 17 '25 16:06 KrasnoshchokBohdan

@magento run all tests

KrasnoshchokBohdan avatar Jun 19 '25 14:06 KrasnoshchokBohdan

@magento run all tests

KrasnoshchokBohdan avatar Jun 23 '25 06:06 KrasnoshchokBohdan

@magento run Unit Tests

KrasnoshchokBohdan avatar Jun 23 '25 11:06 KrasnoshchokBohdan

@magento run all tests

engcom-Hotel avatar Jul 17 '25 06:07 engcom-Hotel

@magento run Integration Tests, Unit Tests, Static Tests

KrasnoshchokBohdan avatar Jul 19 '25 12:07 KrasnoshchokBohdan

@magento run all tests

KrasnoshchokBohdan avatar Jul 19 '25 17:07 KrasnoshchokBohdan

@magento run Integration Tests

engcom-Dash avatar Jul 21 '25 05:07 engcom-Dash

Hello @engcom-Hotel The review comments have been addressed by @KrasnoshchokBohdan hence moving this PR for review.

Thanks!

engcom-Dash avatar Jul 21 '25 07:07 engcom-Dash

@magento run Functional Tests B2B, Functional Tests CE

engcom-Hotel avatar Jul 22 '25 06:07 engcom-Hotel

@magento run all tests

engcom-Dash avatar Jul 23 '25 08:07 engcom-Dash

@KrasnoshchokBohdan Thank you for the contribution & collaboration.

✔️ QA Passed

Preconditions:

  1. Install fresh 2.4-develop

Steps to reproduce:

  1. Create a custom module and add observer for event "catalog_product_save_before". In the Observer call $product->getFinalPrice()

    Screenshot 2025-07-23 at 4 22 17 PM
  2. Save product from storeview scope keeping 'Use Default Value' checkbox checked for 'Special Price From' field.

  3. Save a product using REST API: POST /rest/default/V1/products. Use payload given in the description.

Before: ❌

An exception caught in the exception.log.Also we were not able to get the value of Final Price.

image image

After: ✔️

Screenshot 2025-07-23 at 4 38 47 PM image

The builds are failing hence moving this PR in Extended Testing.

engcom-Dash avatar Jul 23 '25 11:07 engcom-Dash

@magento run Functional Tests B2B, Functional Tests EE, Integration Tests

engcom-Dash avatar Jul 23 '25 12:07 engcom-Dash

@magento run all tests

engcom-Dash avatar Jul 24 '25 05:07 engcom-Dash

@magento run Functional Tests B2B, Functional Tests CE, Functional Tests EE

engcom-Dash avatar Jul 24 '25 07:07 engcom-Dash

The Functional B2B Tests failure are inconsistent and flaky. They neither part of PR nor failing because of the PR changes.

Build 1: https://public-results-storage-prod.magento-testing-service.engineering/reports/magento/magento2/pull/40003/b05d0e7fd25ea4c186d590b5b1db626f/Functional/allure-report-b2b/index.html#categories/bdbf199525818fae7a8651db9eafe741

image

Build 2: https://public-results-storage-prod.magento-testing-service.engineering/reports/magento/magento2/pull/40003/e1e4450bfa89915989e0974b59151eb8/Functional/allure-report-b2b/index.html#categories/8fb3a91ba5aaf9de24cc8a92edc82b5d

image

The Functional CE Tests failure are inconsistent and flaky. They neither part of PR nor failing because of the PR changes.

Build 1: https://public-results-storage-prod.magento-testing-service.engineering/reports/magento/magento2/pull/40003/fc984899a5304299bcac49c15d727c34/Functional/allure-report-ce/index.html#categories/bdbf199525818fae7a8651db9eafe741

image

Build 2: https://public-results-storage-prod.magento-testing-service.engineering/reports/magento/magento2/pull/40003/37a17ebce7468af014a4c45bf48c3262/Functional/allure-report-ce/#categories/8fb3a91ba5aaf9de24cc8a92edc82b5d

image

Hence moving this PR in Merge In Progress.

engcom-Dash avatar Jul 24 '25 10:07 engcom-Dash