Problem/Motivation
Since #3089040: Single product with multiple variation types, AddToCartForm::setFormDisplay() (modules/cart/src/Form/AddToCartForm.php, lines 156-178) switches the purchased entity widget from commerce_product_variation_attributes to commerce_product_variation_title as soon as a product's enabled variations span more than one variation type.
That fallback is needed when the variation types have different attribute fields. But it also applies when every type has exactly the same attribute fields. In that case the attributes widget works unchanged: ProductVariationAttributeMapper already selects across variation types when the attribute field names match.
Example: a product sold as "1 year" and "1-month trial". The two variations use separate variation types only because a per-type setting differs (e.g. Commerce License's allow_renewal), and both types carry the same attribute_plan field. Customers get a select list of variation titles instead of the plan radios.
Steps to reproduce
- Create a product attribute "Plan" with the values "Year" and "Trial", and set its element type to radios.
- Create a second product variation type that uses the same order item type as the default one, and add the "Plan" attribute to both variation types.
- Allow both variation types on the default product type.
- Create a product with one variation of each type, "Year" and "Trial".
- View the product: the add to cart form shows a select list of variation titles instead of the "Plan" radios.
Proposed resolution
Only fall back to the title widget when the variation types do not share their attribute fields. Keep the attributes widget when every variation type used by the product's enabled variations:
- has the same attribute fields, in the same order, and
- uses the same order item type.
Products with a single variation type, and products whose variation types differ, keep the current behavior.
Remaining tasks
- Review the merge request: the change in
AddToCartForm::setFormDisplay()and a kernel test covering both cases (shared attributes keep the attributes widget, different attributes fall back to the title widget).
User interface changes
Products with mixed variation types that share their attribute fields show the attribute widgets instead of the variation title select list.
API changes
None. AddToCartForm gets a new protected $attributeFieldManager property and a protected variationTypesShareAttributes() method.
Data model changes
None.
Issue fork commerce-3629640
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 #5
jsacksick commentedImplemented a different approach and merged, thanks!