Skip to content

Pre-fill new variant fields when adding a variant#14392

Open
emilythericky wants to merge 2 commits into
openfoodfoundation:masterfrom
emilythericky:wishlist-560-duplicate-variant-info
Open

Pre-fill new variant fields when adding a variant#14392
emilythericky wants to merge 2 commits into
openfoodfoundation:masterfrom
emilythericky:wishlist-560-duplicate-variant-info

Conversation

@emilythericky

@emilythericky emilythericky commented Jun 10, 2026

Copy link
Copy Markdown

When a hub manager adds a variant to a product, copy the producer, category, tax category, unit, unit value and price from the most recently created variant, and default the quantity to 0.

Previously these fields were blank, which caused confusing save errors when the required producer, category and unit fields were left empty — behaviour that regressed when those fields moved from the product to the variant. Defaulting the quantity to 0 (and not copying the on-demand flag) leaves new variants out of stock until the user sets a quantity, preventing accidental sales.

What? Why?

When adding a variant on the bulk products page, the new row was blank. The producer and category fields are required, so leaving them empty caused save errors — and because those columns are often hidden, users couldn't see why the save failed. Many resorted to creating new products instead of adding variants.

This previously worked: the old products page auto-filled the category when adding a variant. The behaviour was lost when producer and category moved from the product to the variant during the data model refactor, and the new bulk products page never reinstated it.

This PR copies the key fields from the most recently created variant onto a new variant row: producer, category, tax category, unit, unit value and price. Quantity (on hand) defaults to 0 and the variant is left not on-demand, so a new variant starts out of stock and can't be sold accidentally before stock is set. All fields remain editable.

Before - Save button doesn't appear until a field is edited + Producer/Category not copied + confusing error message when trying to save

Screenshot from 2026-06-10 15-11-51 Screenshot from 2026-06-10 15-12-00

After

Screenshot from 2026-06-10 15-09-07 Screenshot from 2026-06-10 15-09-22

What should we test?

  • Visit the admin Products (bulk edit) page.
  • On a product that already has a variant, click New variant.
  • Confirm the new row pre-fills producer, category, tax category, unit,
    unit value and price from the last variant.
  • Confirm the quantity (On hand) is 0 and the variant is not on-demand.
  • Confirm you can Save immediately without a producer/category/unit
    error.
  • Confirm every pre-filled field can still be edited/overridden.
  • Add several variants in a row and confirm each inherits from the
    previous one.

Release notes

Changelog Category (reviewers may add a label for the release notes):

  • User facing changes

The title of the pull request will be included in the release notes.

@emilythericky

This comment was marked as resolved.

@sigmundpetersen sigmundpetersen moved this from All the things 💤 to Code review 🔎 in OFN Delivery board Jun 11, 2026
@emilythericky emilythericky force-pushed the wishlist-560-duplicate-variant-info branch from a032f50 to 08d3048 Compare June 11, 2026 15:23
When a hub manager adds a variant to a product, copy the producer,
category, tax category, unit, unit value and price from the most
recently created variant, and default the quantity to 0.

Previously these fields were blank, which caused confusing save errors
when the required producer, category and unit fields were left empty —
behaviour that regressed when those fields moved from the product to the
variant. Defaulting the quantity to 0 (and not copying the on-demand
flag) leaves new variants out of stock until the user sets a quantity,
preventing accidental sales.
@emilythericky emilythericky force-pushed the wishlist-560-duplicate-variant-info branch from 08d3048 to 444b667 Compare June 11, 2026 18:01
@rioug rioug added the user facing changes Thes pull requests affect the user experience label Jun 12, 2026

@rioug rioug left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @emilythericky, the changes look good, there are just a couple of things I would like cleaned up.

I also noticed, this changes the behaviour from copying data from the product's first variant to copying data from the product's last variant. @RachL @mariocarabotta are you ok with this change ?

Comment thread app/helpers/admin/products_helper.rb Outdated

def copy_template_fields(template, new_variant)
NEW_VARIANT_TEMPLATE_FIELDS.each do |field|
next unless template.respond_to?(field) && new_variant.respond_to?(:"#{field}=")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary we control both the template and the new variant, using a non existent field would be a bug that we should fix. Could you remove this guard clause ?

'<input id="new_variant_id" type="hidden" name="form[products][0][variants_attributes][1][id]">',
);

form.dispatchEvent(new Event("custom-event"));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use the event type used in production rails-nested-form:add here ? You'll also need to update the html to use rails-nested-form:add->bulk-form#toggleFormChanged

Suggested change
form.dispatchEvent(new Event("custom-event"));
form.dispatchEvent(new Event("rails-nested-form:add"));

'<input id="existing_variant_id" type="hidden" name="form[products][0][variants_attributes][0][id]" value="42">',
);

form.dispatchEvent(new Event("custom-event"));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above, use rails-nested-form:add event type.

@github-project-automation github-project-automation Bot moved this from Code review 🔎 to In Progress ⚙ in OFN Delivery board Jun 12, 2026
Remove the respond_to? guard in copy_template_fields: both the template
and the new variant are Spree::Variant instances we control, so a
missing field would be a bug to fix, not a case to skip.

Use the production rails-nested-form:add event in the bulk form
controller tests instead of a custom event name.
@emilythericky

Copy link
Copy Markdown
Author

I also noticed, this changes the behaviour from copying data from the product's first variant to copying data from the product's last variant. @RachL @mariocarabotta are you ok with this change ?

That was intentional -- the most recently added variant is usually the closest match for the next one. Discussed this with @mkllnk in the issue here . Happy to change that if folks prefer it the other way around.

@sigmundpetersen sigmundpetersen moved this from In Progress ⚙ to Code review 🔎 in OFN Delivery board Jun 12, 2026
@sigmundpetersen sigmundpetersen requested a review from rioug June 12, 2026 09:10
@emilythericky

Copy link
Copy Markdown
Author

there are just a couple of things I would like cleaned up.

changes are in!

@rioug rioug left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great ! thanks @emilythericky 🙏

@rioug

rioug commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

That was intentional -- the most recently added variant is usually the closest match for the next one. Discussed this with @mkllnk in the issue here . Happy to change that if folks prefer it the other way around.

All good then, I had missed the discussion in the ticket. The change makes sense to me, I just wanted to flag it because it's a different behaviour that what we currently have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

user facing changes Thes pull requests affect the user experience

Projects

Status: Code review 🔎

Development

Successfully merging this pull request may close these issues.

duplicate information from product when adding a product variant

3 participants