Problem/Motivation

Currently when a product has only one variation, the variation dropdown still renders. We should hide it and only show the add to cart button.

Proposed resolution

Check variation count. If one set the value as a #value in Form API

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#2 do_not_display-2660300-2.patch1.93 KBmglaman

Comments

mglaman created an issue. See original summary.

mglaman’s picture

Status: Active » Needs review
StatusFileSize
new1.93 KB

  • bojanz committed b395958 on
    Merge pull request #319 from mglaman/2660300-hide-variations-select-if-...
  • mglaman committed c4bea2d on
    Issue #2660300: Do not display variation dropdown if only one variation
    
bojanz’s picture

Status: Needs review » Fixed

Committed.

Status: Fixed » Closed (fixed)

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

kaipipek’s picture

Is it true that this functionality is still not in production after seven years?

jsacksick’s picture

No?

    if (count($variations) === 0) {
      // Nothing to purchase, tell the parent form to hide itself.
      $form_state->set('hide_form', TRUE);
      $element['variation'] = [
        '#type' => 'value',
        '#value' => 0,
      ];
      return $element;
    }
    elseif (count($variations) === 1 && $this->getSetting('hide_single')) {
      /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $selected_variation */
      $selected_variation = reset($variations);
      $element['variation'] = [
        '#type' => 'value',
        '#value' => $selected_variation->id(),
      ];
      return $element;
    }
kaipipek’s picture

Where is this code snippet from? All our products are rendered with a variations dropdown even when they have just one variation.

kaipipek’s picture

Where is the hide_single setting in admin UI?

djroshi’s picture

Hey, I stumbled across this as I was wondering the same thing. Turns out this checkbox is on the order item type (as opposed to the product or product variation)

1. Go to /admin/commerce/config/order-item-types
2. On the order item type click the arrow next to edit and select Manage form display
3. At the top, next to Default, click Add to cart
4. Next to Purchased entity select Product variation title
5. Click the cog wheel to display the widget settings
6. Check the Hide if there's only one product variation checkbox

There is more information about configuring the add to cart form here: https://docs.drupalcommerce.org/commerce2/developer-guide/products/displ...

kaipipek’s picture

Thank you very much djroshi! I was already looking from a right place but I never figured out to change Product variations attributes to Product variation title. It was not very intuitive. :)

bessaston’s picture

Thank you djroshi. This has irritated me for a couple of years, but now no longer.

bessaston’s picture