Describe your bug or feature request.
When creating a "Buy X Get Y" promotion with a 100% discount and enabling the "Automatically add the offer product to the cart if it isn't in it already" option, a new entry in the commerce_order_item table is created with a NULL order_id whenever the product page is viewed or added to the cart.
This issue occurs when using the Calculated Formatter for prices with the option "Apply promotions to the calculated price" enabled.
These orphaned commerce_order_item entities seem to be generated during price resolution and are not properly cleaned up, leading to unnecessary clutter in the database and potential confusion or performance issues.
If a bug, provide steps to reproduce it from a clean install.
1. Install Drupal with Commerce Kickstart or a fresh Commerce installation.
2. Create a "Buy X Get Y" promotion with:
- Offer type: Buy X Get Y
- Discount: 100% off
- Option enabled: "Automatically add the offer product to the cart if it isn't in it already"
2. Disable all caching and clear caches.
3. Visit the product display page for the qualifying "Buy X" product.
4. Add the product to your cart.
Issue fork commerce-3536032
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
jsacksick commentedAny chance you could test the patch from the attached MR? This is a completely blind fix.
Comment #5
jsacksick commentedhm.. Maybe not the right fix...as we have $get_order_items[$order_item->id()] = $order_item; later on... Maybe the whole auto add logic should be bypassed in case an unsaved order is passed?
We could key the arrays by uuid instead, but that increases the scope of the changes... And will break potential classes extending the BuyXGetY plugin, so I wouldn't risk that...
Comment #6
jsacksick commentedSo I believe with my changes, the BuyXGetY auto add feature won't work when the price calculated formatter invokes the processsor, at the same time, I don't think it makes sense for the formatter to trigger the creation of a new order item that is then orphaned and saved into the DB permanently.
Comment #8
jsacksick commentedIn the absence of feedback, still decided to go ahead with the fix as I believe it doesn't make sense for the calculated price formatter to trigger permanent changes and order items being saved into the DB.