magento2
magento2 copied to clipboard
catalog_product_save_before observer throws date-related error when using REST API without store-level values (getFinalPrice() issue)
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
- Create a custom module with an observer for
catalog_product_save_before. - Inside the observer, call
$product->getFinalPrice(). - Save a product using REST API: POST /rest/default/V1/products
- 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()incatalog_product_save_beforefor 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”.
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.
- For more details, review the Magento Contributor Assistant documentation.
- Add a comment to assign the issue:
@magento I am working on this - To learn more about issue processing workflow, refer to the Code Contributions.
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.
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
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: XXXXXlabel to the ticket, indicating the functional areas it may be related to. - [ ] 4. Verify that the issue is reproducible on
2.4-developbranchDetails
- If the issue is reproducible on2.4-developbranch, please, add the labelReproduced 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: Confirmedonce verification is complete. - [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.
:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-14847 is successfully created for this GitHub issue.
: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.
@magento I'm working on this
This issue has been fixed by this PR https://github.com/magento/magento2/pull/40003