magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

catalog_product_save_before observer throws date-related error when using REST API without store-level values (getFinalPrice() issue)

Open KushalDani opened this issue 5 months ago • 6 comments

Preconditions and environment

  • Magento 2.4.6-p10 (Community Edition)
  • Custom extension using catalog_product_save_before observer
  • Using REST API: POST /rest/default/V1/products

Steps to reproduce

  1. Create a custom module with an observer for catalog_product_save_before.
  2. Inside the observer, call $product->getFinalPrice().
  3. Save a product using REST API: POST /rest/default/V1/products
  4. Use the following JSON payload (no store-level pricing):
{
  "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
}

https://github.com/user-attachments/assets/218addba-ce3a-4af9-81cc-9ea2cb397b70

Expected result

  • The product is saved successfully.
  • $product->getFinalPrice() returns the correct final price or null without error.

Actual result

  • A date-related error is thrown during the API call.
  • The error occurs inside the observer when $product->getFinalPrice() is invoked.
  • The issue does not happen if the same product is saved via Admin panel.

Additional information

  • The issue seems related to store scope context missing or pricing attributes being incomplete in API context.
  • It may be caused by Magento trying to calculate special price without required store-level values.
  • The same observer logic works when the product is saved via Admin.

Temporary Workaround:

  • Avoid using $product->getFinalPrice() in catalog_product_save_before for API calls.
  • Use $product->getData('final_price') or defer final price logic until after save.

Release note

When saving a product using the REST API, and calling $product->getFinalPrice() inside the catalog_product_save_before observer, a date-related error is thrown if store-level values like special_price are not defined.

This works as expected when saving the product via the Magento Admin panel.

Triage and priority

  • [ ] Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • [x] Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • [ ] Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • [ ] Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • [ ] Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

KushalDani avatar Jun 04 '25 10:06 KushalDani

Hi @KushalDani. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce.


Join Magento Community Engineering Slack and ask your questions in #github channel. :warning: According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting. :clock10: You can find the schedule on the Magento Community Calendar page. :telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

m2-assistant[bot] avatar Jun 04 '25 10:06 m2-assistant[bot]

Hello @KushalDani,

Thanks for the report and collaboration!

We have tried to reproduce this issue in the latest 2.4-develop branch and it is reproducible for us. We are getting the below error:

TypeError: strtotime(): Argument #1 ($datetime) must be of type string, DateTime given in lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php:275

Hence confirming the issue.

Thanks

engcom-Hotel avatar Jun 05 '25 08:06 engcom-Hotel

Hi @engcom-Hotel. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
  • [ ] 3. Add Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
  • [ ] 4. Verify that the issue is reproducible on 2.4-develop branch
    Details- If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!
  • [ ] 5. Add label Issue: Confirmed once verification is complete.
  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

m2-assistant[bot] avatar Jun 05 '25 08:06 m2-assistant[bot]

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-14847 is successfully created for this GitHub issue.

github-jira-sync-bot avatar Jun 05 '25 08:06 github-jira-sync-bot

:white_check_mark: Confirmed by @engcom-Hotel. Thank you for verifying the issue.
Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

m2-assistant[bot] avatar Jun 05 '25 08:06 m2-assistant[bot]

@magento I'm working on this

KrasnoshchokBohdan avatar Jun 17 '25 16:06 KrasnoshchokBohdan

This issue has been fixed by this PR https://github.com/magento/magento2/pull/40003

engcom-Bravo avatar Aug 14 '25 07:08 engcom-Bravo