Prevent 422 Insights Event - Add to Cart with Discount #1780
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Adding a product to the cart may trigger a PHP and Algolia error due to the discount amount being too large due to improper math by PHP.
PHP Version
Result
PHP Error
This error is thrown in
var/log/system.log
Algolia Error
A

422
status code is returned in the Algolia Events Debugger.Here using xdebug in PHPStorm we can see what is happening.
The product discount amount should be
0.06
. But PHP comes up with the value of0.059999999999999
instead.floatval($item->getProduct()->getPrice())
=23.99
$this->getQuoteItemSalePrice($item)
=23.93
floatval($item->getProduct()->getPrice()) - $this->getQuoteItemSalePrice($item)
=0.059999999999999
I don't understand this math and how it fails... 🤷♂️ But we can protect against it with a
round()
. (maybe it should be added to other methods in this class but I didn't observe issues from those at this time)The discount amount on this product is using the core Magento Customer Group Price feature - adding the discount amount to table

catalog_product_entity_tier_price
.