Do not display variation dropdown if only one variation

Created on 1 February 2016, almost 9 years ago
Updated 4 December 2023, about 1 year ago

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

📌 Task
Status

Fixed

Version

2.0

Component

Product

Created by

🇺🇸United States mglaman WI, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

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

  • 🇮🇱Israel jsacksick

    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;
        }
  • Where is this code snippet from? All our products are rendered with a variations dropdown even when they have just one variation.

  • Where is the hide_single setting defined in admin UI?

  • 🇳🇿New Zealand djroshi

    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...

  • 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. :)

Production build 0.71.5 2024