Skip to content

Prevent 422 Insights Event - Add to Cart with Discount #1780

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

PromInc
Copy link
Contributor

@PromInc PromInc commented Jul 7, 2025

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

php -v
PHP 8.2.28 (cli) (built: Mar 13 2025 18:13:24) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.28, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.28, Copyright (c), by Zend Technologies
    with Xdebug v3.4.2, Copyright (c) 2002-2025, by Derick Rethans

Result

PHP Error

This error is thrown in var/log/system.log

main.CRITICAL: Unable to send add to cart event due to Algolia events model misconfiguration: Discount must be a valid decimal number and total length must be no longer than 16 characters [] []

Algolia Error

A 422 status code is returned in the Algolia Events Debugger.
image

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 of 0.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)

image

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.
image

SELECT * FROM catalog_product_index_price WHERE entity_id = 5981 AND customer_group_id = 22;
 entity_id | customer_group_id | website_id | tax_class_id | price     | final_price | min_price | max_price | tier_price 
-----------+-------------------+------------+--------------+-----------+-------------+-----------+-----------+------------
      5981 |                22 |          2 |            2 | 23.990000 |   23.930000 | 23.930000 | 23.930000 |  23.930000 

SELECT * FROM catalog_product_entity_tier_price WHERE entity_id = 5981 AND customer_group_id = 22;
 value_id | entity_id | all_groups | customer_group_id | qty    | value     | website_id | percentage_value 
----------+-----------+------------+-------------------+--------+-----------+------------+------------------
   490854 |      5981 |          0 |                22 | 1.0000 | 23.930000 |          0 |           [NULL] 

Adding a product to the cart may trigger a PHP and Algolia error due to the discount amount being too large.

## PHP Error
```
main.CRITICAL: Unable to send add to cart event due to Algolia events model misconfiguration: Discount must be a valid decimal number and total length must be no longer than 16 characters [] []
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant