Problem/Motivation

The Commerce Webform Order webform handler allows to reference different entity types with different identifiers (either UUID, variation_id, SKU). The query in \Drupal\commerce_webform_order\Plugin\WebformHandler\CommerceWebformOrderHandler::loadEntityValue() then tries to load the referenced entity by all these different properties. This may result in the incorrect product added to the cart, since MySQL casts a UUID string to an INT.

Steps to reproduce

Assume the following data:

commerce_product_variation:

variation_id uuid
214 d53c146e-e420-42b5-bef5-d344682acd96
1080 214abdb0-cc1e-4f67-bbb9-3e6e2a604bdf

commerce_product_variation__field_data:

variation_id
214
1080

and execute the query:

SELECT commerce_product_variation.variation_id
FROM
commerce_product_variation base_table
LEFT JOIN commerce_product_variation ON commerce_product_variation.variation_id = base_table.variation_id
LEFT JOIN commerce_product_variation_field_data commerce_product_variation_field_data ON commerce_product_variation_field_data.variation_id = base_table.variation_id
WHERE (commerce_product_variation.uuid LIKE "214abdb0-cc1e-4f67-bbb9-3e6e2a604bdf" ESCAPE '\\') or (commerce_product_variation_field_data.variation_id = "214abdb0-cc1e-4f67-bbb9-3e6e2a604bdf")

This will find the product with variation_id 214 instead of 1080:

Proposed resolution

Apply the conditional entity loading from the configuration form to \Drupal\commerce_webform_order\Plugin\WebformHandler\CommerceWebformOrderHandler::loadEntityValue() :
Check if the value is a valid UUID: assume it is a UUID
Check if the value is numeric: assume it is a variation_id
Else: assume it is a SKU

Remaining tasks

  1. Write a patch
  2. Review
  3. Commit

User interface changes

None

API changes

None

Data model changes

None

CommentFileSizeAuthor
#2 Screenshot 2024-05-23 at 13.37.26.png16.82 KBidebr
Command icon 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

idebr created an issue. See original summary.

idebr’s picture

Issue summary: View changes
StatusFileSize
new16.82 KB

idebr’s picture

Status: Active » Needs review
facine’s picture

Status: Needs review » Needs work

@idebr, thanks for debugging and working on this issue.
I've added a comment on the PR itself; I think we should address the issue from there.

facine’s picture

facine’s picture

Status: Needs work » Fixed

Fixed, thanks!

  • facine committed ea9bbf11 on 3.x authored by idebr
    Issue #3449355: Wrong product may be added to cart due to query by uuid...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.