Issue

Currently the Product Variation Attributes Widget functionality disables the form element that only has a single value. This is confusing to users as they are not sure what is selected or if they can actually purchase the product. The user can't click on the elements because they are disabled and in some themes, they can be greyed out etc

Work around.

Add a setting to the widget to define whether to disable attributes with a single value.

Comments

jnrfred created an issue. See original summary.

jnrfred’s picture

StatusFileSize
new2.17 KB

Attach is patch to add a setting to widget.

jnrfred’s picture

Status: Active » Needs review
mglaman’s picture

Status: Needs review » Needs work
  1. +++ b/modules/product/src/Plugin/Field/FieldWidget/ProductVariationAttributesWidget.php
    @@ -42,6 +42,45 @@ class ProductVariationAttributesWidget extends ProductVariationWidgetBase implem
    +    return [
    +        'enable_form_element' => FALSE,
    +      ] + parent::defaultSettings();
    

    Needs config schema update.

  2. +++ b/modules/product/src/Plugin/Field/FieldWidget/ProductVariationAttributesWidget.php
    @@ -42,6 +42,45 @@ class ProductVariationAttributesWidget extends ProductVariationWidgetBase implem
    +    if ($this->getSetting('enable_form_element') == 1) {
    
    @@ -180,7 +219,7 @@ class ProductVariationAttributesWidget extends ProductVariationWidgetBase implem
    +      if ($attribute_element['#required'] && count($attribute->getValues()) === 1 && $this->getSetting('enable_form_element') == 0) {
    

    The default setting value is FALSE but we're checking 0.

jnrfred’s picture

Status: Needs work » Needs review
StatusFileSize
new2.97 KB

Config schema updated and changed default settings value.

deaom’s picture

Status: Needs review » Needs work

I applied the patch, it applies, but when I do drush entup nothing is updated, the same with the drush updb.
When I go to the commerce configuration for product variation types after I cleared all caches, in the manage form display, I seem to not find/see the widget. I also tried adding a new field, still no luck with the widget. Maybe it's located somewhere else?

Br,
Dea

mglaman’s picture

Status: Needs work » Needs review

seem to not find/see the widget. I also tried adding a new field, still no luck with the widget. Maybe it's located somewhere else?

It adds a setting to the existing attribute selection widget on the order item purchased entity field. Check there.

deaom’s picture

Status: Needs review » Reviewed & tested by the community

Thank you for the guidance @mglaman. I found the widget and the add to cart button is present on the product. But I don't see the change when I disable or enable the widget on the default display or on the add to cart display. I have just one color available for a product and it is always shown, no matter what the widget says. I'm probablly doing something wrong here or the code is not working properly for me.

BR

deaom’s picture

Status: Reviewed & tested by the community » Needs review
cornifex’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new245.94 KB
new257.78 KB

Steps to Verify

  1. Make sure the Order Items' "Add to Cart" form display is using the "Product Variation Attributes" widget.
  2. Tick "Do not disable form element with single attribute value".
  3. Add an attribute with a single value if you don't already have one, and make sure a product uses it.

Result

Before
Before
After
After

bojanz’s picture

Status: Reviewed & tested by the community » Needs work
+    $element['enable_form_element'] = [
+      '#type' => 'checkbox',
+      '#title' => $this->t('Do not disable form element with single attribute value'),
+      '#default_value' => $this->getSetting('enable_form_element'),
+    ];

The setting name has no relation to the label, and both feel odd (the description has a double negative, which takes a second to process).

bojanz’s picture

Discussed with the team, we said that we should test to see if we can just remove the disabling completely. Don't even know if ajax is still triggered by a single option element.

rszrama’s picture

+1 from me; disabling the form element is a bit of a hack here, simplifying form processing (i.e. we can safely disable it b/c a required single value element will have its value populated in the form state regardless) at the expense of visual changes on the front-end (the grayed out disabled element). Presumably we can disable the Ajax listener on the element if unnecessary Ajax refreshes is a problem.

bojanz’s picture

Title: Add a widget setting to define whether to disable product variation attributes with a single value. » Stop disabling attributes on the add to cart form when they only have 1 value
Category: Feature request » Task
bojanz’s picture

Status: Needs work » Needs review
StatusFileSize
new3.41 KB

Let's try this patch.

Confirmed that it doesn't trigger #ajax.

  • bojanz committed 4f5d0a4 on 8.x-2.x authored by jnrfred
    Issue #3020173 by jnrfred, bojanz, cornifex, mglaman, rszrama: Stop...
bojanz’s picture

Status: Needs review » Fixed

Tests are green. Committed.

Commit credit goes to jnrfred, for his earlier efforts.

Thanks, everyone!

Status: Fixed » Closed (fixed)

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