On the add to cart form, it would be nice to be able to use a product variation field to select a variation, rather than just the title or attributes. The attached patch adds a configurable widget for this. It needs some error checking for when the field is not available on the variations attached to a particular product, but this is a starting point.

Comments

zengenuity created an issue. See original summary.

zengenuity’s picture

Initial version.

mglaman’s picture

++ This is awesome.

This leaves a few questions

  • Should this be committed into commerce_product?
  • What do we do about useful widgets like this, if we do not commit? Place them in XYZ contrib, see popularity and move them into core?
  • Can the title widget become "deprecated" and essentially extend this (forcing title field as rendered field.)
steveoliver’s picture

Yeah, we should have done this instead of the title widget. I wonder if instead of deprecated, we can make this widget have a 'title' option to use as default/fallback and then update any configs using the title widget?

bhushan.nagaonkar’s picture

When I use this patch, I get following error,

ArgumentCountError: Too few arguments to function Drupal\commerce_product\Plugin\Field\FieldWidget\ProductVariationWidgetBase::__construct(), 6 passed in /app/web/modules/contrib/commerce/modules/product/src/Plugin/Field/FieldWidget/ProductVariationFieldWidget.php on line 66 and exactly 7 expected in Drupal\commerce_product\Plugin\Field\FieldWidget\ProductVariationWidgetBase->__construct() (line 59 of /app/web/modules/contrib/commerce/modules/product/src/Plugin/Field/FieldWidget/ProductVariationWidgetBase.php)
#0 /app/web/modules/contrib/commerce/modules/product/src/Plugin/Field/FieldWidget/ProductVariationFieldWidget.php(66): Drupal\commerce_product\Plugin\Field\FieldWidget\ProductVariationWidgetBase->__construct('commerce_produc...', Array, Object(Drupal\Core\Field\BaseFieldDefinition), Array, Array, Object(Drupal\Core\Entity\EntityTypeManager))
#1 /app/web/modules/contrib/commerce/modules/product/src/Plugin/Field/FieldWidget/ProductVariationFieldWidget.php(99): Drupal\commerce_product\Plugin\Field\FieldWidget\ProductVariationFieldWidget->__construct('commerce_produc...', Array, Object(Drupal\Core\Field\BaseFieldDefinition), Array, Array, Object(Drupal\Core\Entity\EntityTypeManager), Object(Drupal\Core\Entity\EntityFieldManager), Object(Drupal\Core\Entity\EntityTypeBundleInfo), Object(Drupal\Core\Entity\EntityDisplayRepository))
#2 /app/web/core/lib/Drupal/Core/Field/WidgetPluginManager.php(122): Drupal\commerce_product\Plugin\Field\FieldWidget\ProductVariationFieldWidget::create(Object(Drupal\Core\DependencyInjection\Container), Array, 'commerce_produc...', Array)
#3 /app/web/core/lib/Drupal/Core/Field/WidgetPluginManager.php(110): Drupal\Core\Field\WidgetPluginManager->createInstance('commerce_produc...', Array)
#4 /app/web/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php(157): Drupal\Core\Field\WidgetPluginManager->getInstance(Array)
#5 /app/web/core/modules/field_ui/src/Form/EntityDisplayFormBase.php(366): Drupal\Core\Entity\Entity\EntityFormDisplay->getRenderer('purchased_entit...')
jsacksick’s picture

Should we not extend the title widget instead? Add settings to be able to choose a different field rather than defining a completely new widget that duplicates most of the existing logic?