Add a product type setting, a checkbox, titled "Allow each product to have multiple variations", enabled by default.
When on, the current Variations tab is shown.
When off, a new "commerce_product_single_variation" ("Single variation (Product information)") widget is used for the variations field. In that case, the Variations tab is hidden, as is the "Save and add variations" button. Toggling the checkbox toggles the widget.

The single variation widget shows the variation form inside a "Product information" collapsible fieldset (since "Variations" doesn't make sense as a title when there's always only one variation). We can use the IEF form element for now.

CommentFileSizeAuthor
#14 2690681-17.patch18.99 KBbojanz
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bojanz created an issue. See original summary.

johnstorey’s picture

Assigned: Unassigned » johnstorey

Bojanz - if there is no objection, I'd like to take a shot at this.

rszrama’s picture

Go for it! : )

johnstorey’s picture

@Bojanz,

After a couple of hours, the validation seems straightforward enough. Let me know if you see an issue with my plan

  • I added a validator to the product_id field when creating a new variation
  • When we first create the product, and add variations, the product_id is empty. This is because the product is created in the form submit process. So I would call the same validation check from the form validate function.

I don't see that part of the issue as being too complex, but I'm digging into the Commerce 2.x code for the first time. Am I missing some subtle point?

bojanz’s picture

The validation needs to be in a ConstraintValidator class, Form API invokes the entity validation automatically.

johnstorey’s picture

Ah, total bad thinking on my part. I see what you are thinking. I was adding the constraint when the product variation was being created, when I can just put it on the product creation form. I don't see that as elegant, but from a code point of view, it is easier to accomplish.

bojanz’s picture

You don't need to touch the form at all. The constraint & constraint validator do all the work.

deadbeef’s picture

If this is enforced, how would you handle a product that is, for example, a charitable donation - where the only varying attribute is a range of fixed amounts?

bojanz’s picture

Right, we might need to make this configurable on the product type.

sumanthkumarc’s picture

@bojanz i see the variations field is locked in product type entity. So as you said, can we add a number field in product type form at Drupal\commerce_product\Form\ProductTypeForm form method and in submit, i see

    if ($status == SAVED_NEW) {
      commerce_product_add_stores_field($this->entity);
      commerce_product_add_body_field($this->entity);
      commerce_product_add_variations_field($this->entity);
    }

so commerce_product_add_variations_field() function adds a cardinality of unlimited to variations field. Do we need to pass the cardinality, so that user can set it? also should we allow only once on initial creation for changing the cardinality?

SpartyDan’s picture

Assigned: johnstorey » SpartyDan

Sprinting at DC Baltimore.

SpartyDan’s picture

Assigned: SpartyDan » Unassigned
bojanz’s picture

Title: Enforce a 1-1 product-variation relationship when there are no attributes » Simplify the variation UX when each product has a single variation
Category: Task » Feature request

Retitling.

We need a product type setting, defaulting to true, that says "Allow each product to have multiple variations" or something similar.

bojanz’s picture

Status: Active » Needs review
FileSize
18.99 KB

Initial work. Needs more tests.

bojanz’s picture

Issue summary: View changes

Updating the summary.

  • bojanz committed 13e79a1 on 8.x-2.x
    Issue #2690681 by bojanz: Simplify the variation UX when each product...
bojanz’s picture

Status: Needs review » Fixed

Committed the final version.

Status: Fixed » Closed (fixed)

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