Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Product variants form fixes (#13017)
Łukasz Adamczykc998cb74fb5d
diff --git a/admin/app/controllers/spree/admin/products_controller.rb b/admin/app/controllers/spree/admin/products_controller.rbindex e410f98ccc..8fa5186b34 100644--- a/admin/app/controllers/spree/admin/products_controller.rb+++ b/admin/app/controllers/spree/admin/products_controller.rb@@ -173,7 +173,7 @@ module Spreejoins(option_type: :product_option_types).includes(option_type: :option_values).merge(@product.product_option_types).- reorder("#{Spree::ProductOptionType.table_name}.position").+ reorder("#{Spree::ProductOptionType.table_name}.position", "#{Spree::Variant.table_name}.position").uniq.group_by(&:option_type).each_with_index do |option, index|option_type, option_values = optiondiff --git a/admin/app/javascript/spree/admin/controllers/variants_form_controller.js b/admin/app/javascript/spree/admin/controllers/variants_form_controller.jsindex b1eeef2a45..83beb4328c 100644--- a/admin/app/javascript/spree/admin/controllers/variants_form_controller.js+++ b/admin/app/javascript/spree/admin/controllers/variants_form_controller.js@@ -128,25 +128,29 @@ export default class extends CheckboxSelectAll {const nestingLevel = internalName.split('/').lengthif (nestingLevel === 1) {- const firstOptionKey = Object.keys(this.optionsValue)[0]- const newOptionValues = this.optionsValue[firstOptionKey].values.filter((value) => value.text !== internalName)- if (newOptionValues.length === 0) {- const newOptionsValue = this.optionsValue- delete newOptionsValue[firstOptionKey]- this.optionsValue = newOptionsValue- this.optionsContainerTarget.querySelector(`#option-${firstOptionKey}`).remove()- } else {- this.optionsValue = {- ...this.optionsValue,- [firstOptionKey]: {- ...this.optionsValue[firstOptionKey],- values: newOptionValues+ const sortedOptions = Object.entries(this.optionsValue).sort((a, b) => a[1].position - b[1].position)+ const firstOptionKey = sortedOptions[0]?.[0]++ if (firstOptionKey !== undefined) {+ const newOptionValues = this.optionsValue[firstOptionKey].values.filter((value) => value.text !== internalName)+ if (newOptionValues.length === 0) {+ const newOptionsValue = this.optionsValue+ delete newOptionsValue[firstOptionKey]+ this.optionsValue = newOptionsValue+ this.removeOption(firstOptionKey)+ } else {+ this.optionsValue = {+ ...this.optionsValue,+ [firstOptionKey]: {+ ...this.optionsValue[firstOptionKey],+ values: newOptionValues+ }}- }- this.optionsContainerTarget.querySelector(`#option-${firstOptionKey} [data-name="${internalName}"]`).remove()+ this.optionsContainerTarget.querySelector(`#option-${firstOptionKey} [data-name="${internalName}"]`).remove()+ }+ checkbox.checked = false}- checkbox.checked = false}delete newStockValue[internalName]@@ -463,25 +467,42 @@ export default class extends CheckboxSelectAll {this.variantsTableTarget.classList.remove('d-none')const nestingLevel = Math.min(keys.length, 2)- let idx = 0++ // This is the index for the product variant attributes+ // We only want to count nested options if we have more than one option type+ let idx+ if (nestingLevel > 1) {+ idx = this.variantsContainerTarget.querySelectorAll('.nested').length+ } else {+ idx = this.variantsContainerTarget.querySelectorAll('[data-variants-form-target="variant"]').length+ }for (let i = 0; i < nestingLevel; i++) {this.variantsValue.forEach((variant) => {const { name, internalName } = this.calculateVariantName(variant, keys, i)if (currentVariants.has(internalName) || this.ignoredVariants.has(internalName)) {- idx++return}currentVariants.add(internalName)const existingVariant = this.variantsContainerTarget.querySelector(`[data-variant-name="${internalName}"]`)if (existingVariant) {- if (i === 0 && nestingLevel > 1) {- existingVariant.querySelectorAll("input[type='hidden']").forEach((input) => input.remove())-- this.prepareParentVariant(existingVariant, internalName)+ if (i === 0) {+ if (nestingLevel > 1) {+ existingVariant.querySelectorAll("input[type='hidden']").forEach((input) => input.remove())++ this.prepareParentVariant(existingVariant, internalName)+ } else if (nestingLevel === 1) {+ // We need to generate hidden inputs when we only have a single option left+ // And there are no inputs already+ if (existingVariant.querySelectorAll("input[type='hidden']").length === 0) {+ const inputs = this.createInputsForVariant(keys, variant, idx)+ inputs.forEach((input) => existingVariant.appendChild(input))+ idx+++ }+ }}- idx+++return}@@ -537,10 +558,10 @@ export default class extends CheckboxSelectAll {}this.preparePriceInputs(variantTarget, internalName, idx)-this.prepareStockInputs(variantTarget, internalName, idx)++ idx++}- idx++variantNameContainer.textContent = nameif (previousVariant) {@@ -570,7 +591,9 @@ export default class extends CheckboxSelectAll {}refreshParentInputs() {- const firstOption = Object.values(this.optionsValue)[0]+ const sortedOptions = Object.entries(this.optionsValue).sort((a, b) => a[1].position - b[1].position)+ const firstOption = sortedOptions[0]?.[1]+if (firstOption) {firstOption.values.forEach((option) => {this.currenciesValue.forEach((currency) => {@@ -899,12 +922,25 @@ export default class extends CheckboxSelectAll {let { optionId } = event.paramsoptionId = String(optionId)- const option = this.optionsContainerTarget.querySelector(`#option-${optionId}`)- option.remove()- this.optionsValue = { ...this.optionsValue, [optionId]: null }+ this.removeOption(optionId)++ const newOptionsValue = {}+ Object.entries(this.optionsValue).filter((option) => option[0] !== optionId).forEach((option) => {+ newOptionsValue[option[0]] = option[1]+ })++ this.optionsValue = newOptionsValuethis.refreshParentInputs()}+ removeOption(optionId) {+ const option = this.optionsContainerTarget.querySelector(`#option-${optionId}`)+ if (option) option.remove()++ const optionTypeInput = this.optionsContainerTarget.querySelector(`#product_option_type_ids_${optionId}`)+ if (optionTypeInput) optionTypeInput.remove()+ }+optionFormTemplate(optionName, optionValues, id, availableOptions) {const template = this.optionFormTemplateTarget.content.cloneNode(true)@@ -1045,14 +1081,24 @@ export default class extends CheckboxSelectAll {priceForVariant(variantName, currency) {const existingPrice = this.pricesValue[variantName]?.[currency.toLowerCase()]+if (existingPrice) {return {...existingPrice,amount: existingPrice.amount ? parseFloat(existingPrice.amount) : existingPrice.amount}- }+ } else {+ const parentName = variantName.split('/')[0]+ const parentPrices = Object.entries(this.pricesValue)+ .filter(([internalName, prices]) => internalName.startsWith(parentName) && prices[currency.toLowerCase()] !== undefined)+ .map(([_key, prices]) => parseFloat(prices[currency.toLowerCase()].amount))+ .sort((priceAmountA, priceAmountB) => priceAmountA - priceAmountB)- return { amount: null, id: null }+ return {+ amount: parentPrices[0] ?? null,+ id: null+ }+ }}updatePriceForVariant(variantName, newPrice, currency) {diff --git a/admin/app/views/spree/admin/products/form/_variants.html.erb b/admin/app/views/spree/admin/products/form/_variants.html.erbindex b2cf1bd0ba..2a3bb2465d 100644--- a/admin/app/views/spree/admin/products/form/_variants.html.erb+++ b/admin/app/views/spree/admin/products/form/_variants.html.erb@@ -27,7 +27,6 @@<div data-variants-form-target="optionsContainer" class="options-creator__options_container"><% @product.product_option_types.includes(:option_type).reorder('spree_product_option_types.position').each do |product_option_type| %><% option_type = product_option_type.option_type %>- <%= hidden_field_tag "product[option_type_ids][]", option_type.id %><div class="options-creator__option" data-variants-form-target="option" id="option-<%= option_type.id %>"><% if can?(:manage_option_types, @product) %><button class="draggable" type="button">@@ -161,7 +160,7 @@</div></div><%= render 'spree/admin/products/form/variants/variant_template', default_stock_location: default_stock_location_for_product(@product) %>- <div class="variants-table__body" data-variants-form-target="variantsContainer">+ <div class="variants-table__body" data-variants-form-target="variantsContainer" data-test-id="product-variants-table"></div><div class="variants-table__footer"><%= Spree.t('admin.variants_form.total_inventory_html', stock_location: current_store.default_stock_location.name, count: raw("<span data-variants-form-target='stockItemsCount'>#{@product.total_on_hand}</span>") )%>diff --git a/admin/spec/controllers/spree/admin/products_controller_spec.rb b/admin/spec/controllers/spree/admin/products_controller_spec.rbindex 36068418e6..98feaea858 100644--- a/admin/spec/controllers/spree/admin/products_controller_spec.rb+++ b/admin/spec/controllers/spree/admin/products_controller_spec.rb@@ -1004,6 +1004,19 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller doexpect(product.reload.variant_ids).to eq([variant2.id, variant3.id])endend++ context 'when all variants are removed' do+ before do+ product_params.delete(:variants_attributes)+ end++ it 'removes the product variants' do+ send_request++ expect(product.reload.variant_ids).to be_empty+ expect(product.option_types).to be_empty+ end+ endendcontext 'setting track_inventory to false' dodiff --git a/admin/spec/features/spree/admin/products/products_spec.rb b/admin/spec/features/spree/admin/products/products_spec.rbindex 082fb94d3a..4787656fcf 100644--- a/admin/spec/features/spree/admin/products/products_spec.rb+++ b/admin/spec/features/spree/admin/products/products_spec.rb@@ -14,6 +14,14 @@ describe 'Products', type: :feature dootend+ def select_option_value(option_value_name, index: 0, create: false)+ select_element = all('[data-multi-tom-select-target="select"]')[index]++ within(select_element) do+ tom_select(option_value_name, from: 'new_option_values_-ts-control', create: create)+ end+ end+context 'listing products' docontext 'sorting' dobefore do@@ -288,6 +296,880 @@ describe 'Products', type: :feature doexpect(product.barcode).to eq('123456')expect(product.master.stock_items.first.count_on_hand).to eq(10)end++ it 'allows adding new options', js: true do+ # Add the first option - Color+ find('span', text: Spree.t('admin.variants_form.add_option.empty')).click+ tom_select('Color', from: Spree.t(:option_name), create: true)+ select_option_value('Black', index: 0, create: true)+ select_option_value('White', index: 1, create: true)+ click_on 'Done'++ within('#option-Color') do+ expect(page).to have_content('Black')+ expect(page).to have_content('White')+ end++ # Add the second option - Size+ find('span', text: Spree.t('admin.variants_form.add_option.not_empty')).click+ tom_select('Size', from: Spree.t(:option_name), create: true)+ select_option_value('Small', index: 0, create: true)+ select_option_value('Medium', index: 1, create: true)+ select_option_value('Large', index: 2, create: true)+ click_on 'Done'++ within('#option-Size') do+ expect(page).to have_content('Small')+ expect(page).to have_content('Medium')+ expect(page).to have_content('Large')+ end++ within('[data-test-id="product-variants-table"]') do+ within('[data-variant-name="Black/Small"]') do+ within('.column-price') { find('input').set(10) }+ within('.column-quantity') { find('input').set(100) }+ end++ within('[data-variant-name="Black/Medium"]') do+ within('.column-price') { find('input').set(11) }+ within('.column-quantity') { find('input').set(110) }+ end++ within('[data-variant-name="Black/Large"]') do+ within('.column-price') { find('input').set(12) }+ within('.column-quantity') { find('input').set(120) }+ end++ within('[data-variant-name="White/Small"]') do+ within('.column-price') { find('input').set(20) }+ within('.column-quantity') { find('input').set(200) }+ end++ within('[data-variant-name="White/Medium"]') do+ within('.column-price') { find('input').set(21) }+ within('.column-quantity') { find('input').set(210) }+ end++ within('[data-variant-name="White/Large"]') do+ within('.column-price') { find('input').set(22) }+ within('.column-quantity') { find('input').set(220) }+ end+ end++ # Update product variants+ click_on 'Update', match: :first+ expect(page).to have_content('successfully updated')++ color_option = Spree::OptionType.find_by(name: 'color')+ size_option = Spree::OptionType.find_by(name: 'size')++ within("#option-#{color_option.id}") do+ expect(page).to have_content('Black')+ expect(page).to have_content('White')+ end++ within("#option-#{size_option.id}") do+ expect(page).to have_content('Small')+ expect(page).to have_content('Medium')+ expect(page).to have_content('Large')+ end++ within('[data-test-id="product-variants-table"]') do+ within('[data-variant-name="Black/Small"]') do+ within('.column-price') { expect(page).to have_field(with: 10) }+ within('.column-quantity') { expect(page).to have_field(with: 100) }+ end++ within('[data-variant-name="Black/Medium"]') do+ within('.column-price') { expect(page).to have_field(with: 11) }+ within('.column-quantity') { expect(page).to have_field(with: 110) }+ end++ within('[data-variant-name="Black/Large"]') do+ within('.column-price') { expect(page).to have_field(with: 12) }+ within('.column-quantity') { expect(page).to have_field(with: 120) }+ end++ within('[data-variant-name="White/Small"]') do+ within('.column-price') { expect(page).to have_field(with: 20) }+ within('.column-quantity') { expect(page).to have_field(with: 200) }+ end++ within('[data-variant-name="White/Medium"]') do+ within('.column-price') { expect(page).to have_field(with: 21) }+ within('.column-quantity') { expect(page).to have_field(with: 210) }+ end++ within('[data-variant-name="White/Large"]') do+ within('.column-price') { expect(page).to have_field(with: 22) }+ within('.column-quantity') { expect(page).to have_field(with: 220) }+ end+ end++ expected_variant_options_text = [+ 'Color: Black, Size: Small',+ 'Color: Black, Size: Medium',+ 'Color: Black, Size: Large',+ 'Color: White, Size: Small',+ 'Color: White, Size: Medium',+ 'Color: White, Size: Large'+ ]++ expect(product.reload.variants.map(&:options_text)).to eq(expected_variant_options_text)++ # Update product variants to check if the order is preserved+ click_on 'Update', match: :first+ expect(page).to have_content('successfully updated')++ expect(product.reload.variants.map(&:options_text)).to eq(expected_variant_options_text)+ end++ it 'allows adding new option values', js: true do+ # Add the first option - Color+ find('span', text: Spree.t('admin.variants_form.add_option.empty')).click+ tom_select('Color', from: Spree.t(:option_name), create: true)+ select_option_value('Black', index: 0, create: true)+ select_option_value('White', index: 1, create: true)+ click_on 'Done'++ within('#option-Color') do+ expect(page).to have_content('Black')+ expect(page).to have_content('White')+ end++ # Update product variants+ click_on 'Update', match: :first+ expect(page).to have_content('successfully updated')+ within('.alerts-container') { find('button').click }++ color_option = Spree::OptionType.find_by(name: 'color')++ within("#option-#{color_option.id}") do+ click_on 'Edit'+ select_option_value('Red', index: 2, create: true)+ click_on 'Done'+ end++ within("#option-#{color_option.id}") do+ expect(page).to have_content('Black')+ expect(page).to have_content('White')+ expect(page).to have_content('Red')+ end++ # Update product variants+ click_on 'Update', match: :first+ expect(page).to have_content('successfully updated')+ within('.alerts-container') { find('button').click }++ within('[data-test-id="product-variants-table"]') do+ expect(page).to have_content('Black')+ expect(page).to have_content('White')+ expect(page).to have_content('Red')+ end++ expect(product.reload.variants.map(&:options_text)).to contain_exactly(+ 'Color: Black',+ 'Color: White',+ 'Color: Red'+ )+ end++ it 'uses the parent stock level for new variants', js: true do+ # Add the first option - Color+ find('span', text: Spree.t('admin.variants_form.add_option.empty')).click+ tom_select('Color', from: Spree.t(:option_name), create: true)+ select_option_value('Black', index: 0, create: true)+ select_option_value('White', index: 1, create: true)+ click_on 'Done'++ within('#option-Color') do+ expect(page).to have_content('Black')+ expect(page).to have_content('White')+ end++ # Set stock levels for the color variants+ within('[data-test-id="product-variants-table"]') do+ within('[data-variant-name="Black"]') do+ within('.column-quantity') { find('input').set(100) }+ end++ within('[data-variant-name="White"]') do+ within('.column-quantity') { find('input').set(200) }+ end+ end++ # Update product variants+ click_on 'Update', match: :first+ expect(page).to have_content('successfully updated')+ within('.alerts-container') { find('button').click }++ # Add the second option - Size+ find('span', text: Spree.t('admin.variants_form.add_option.not_empty')).click+ tom_select('Size', from: Spree.t(:option_name), create: true)+ select_option_value('Small', index: 0, create: true)+ select_option_value('Medium', index: 1, create: true)+ select_option_value('Large', index: 2, create: true)+ click_on 'Done'++ within('#option-Size') do+ expect(page).to have_content('Small')+ expect(page).to have_content('Medium')+ expect(page).to have_content('Large')+ end++ # Check stock levels for the color/size variants+ within('[data-test-id="product-variants-table"]') do+ within('[data-variant-name="Black"]') do+ within('.column-quantity') { expect(page).to have_field(placeholder: 300) }+ end++ within('[data-variant-name="Black/Small"]') do+ within('.column-quantity') { expect(page).to have_field(with: 100) }+ end++ within('[data-variant-name="Black/Medium"]') do+ within('.column-quantity') { expect(page).to have_field(with: 100) }+ end++ within('[data-variant-name="Black/Large"]') do+ within('.column-quantity') { expect(page).to have_field(with: 100) }+ end++ within('[data-variant-name="White"]') do+ within('.column-quantity') { expect(page).to have_field(placeholder: 600) }+ end++ within('[data-variant-name="White/Small"]') do+ within('.column-quantity') { expect(page).to have_field(with: 200) }+ end++ within('[data-variant-name="White/Medium"]') do+ within('.column-quantity') { expect(page).to have_field(with: 200) }+ end++ within('[data-variant-name="White/Large"]') do+ within('.column-quantity') { expect(page).to have_field(with: 200) }+ end+ end++ # Update product variants+ click_on 'Update', match: :first+ expect(page).to have_content('successfully updated')+ within('.alerts-container') { find('button').click }++ # Make sure the stock levels are still correct after the update+ within('[data-test-id="product-variants-table"]') do+ within('[data-variant-name="Black"]') do+ within('.column-quantity') { expect(page).to have_field(placeholder: 300) }+ end++ within('[data-variant-name="Black/Small"]') do+ within('.column-quantity') { expect(page).to have_field(with: 100) }+ end++ within('[data-variant-name="Black/Medium"]') do+ within('.column-quantity') { expect(page).to have_field(with: 100) }+ end++ within('[data-variant-name="Black/Large"]') do+ within('.column-quantity') { expect(page).to have_field(with: 100) }+ end++ within('[data-variant-name="White"]') do+ within('.column-quantity') { expect(page).to have_field(placeholder: 600) }+ end++ within('[data-variant-name="White/Small"]') do+ within('.column-quantity') { expect(page).to have_field(with: 200) }+ end++ within('[data-variant-name="White/Medium"]') do+ within('.column-quantity') { expect(page).to have_field(with: 200) }+ end++ within('[data-variant-name="White/Large"]') do+ within('.column-quantity') { expect(page).to have_field(with: 200) }+ end+ end+ end++ context 'for a product with existing options', js: true do+ let!(:color_option) { create(:option_type, name: 'color', presentation: 'Color', products: [product]) }+ let!(:black_option_value) { create(:option_value, name: 'black', presentation: 'Black', option_type: color_option) }+ let!(:white_option_value) { create(:option_value, name: 'white', presentation: 'White', option_type: color_option) }++ let!(:size_option) { create(:option_type, name: 'size', presentation: 'Size', products: [product]) }+ let!(:small_option_value) { create(:option_value, name: 'small', presentation: 'Small', option_type: size_option) }+ let!(:medium_option_value) { create(:option_value, name: 'medium', presentation: 'Medium', option_type: size_option) }+ let!(:large_option_value) { create(:option_value, name: 'large', presentation: 'Large', option_type: size_option) }++ let!(:variant1) { create(:variant, product: product, option_values: [black_option_value, small_option_value], price: 10) }+ let!(:variant2) { create(:variant, product: product, option_values: [black_option_value, medium_option_value], price: 10) }+ let!(:variant3) { create(:variant, product: product, option_values: [black_option_value, large_option_value], price: 10) }++ let!(:variant4) { create(:variant, product: product, option_values: [white_option_value, small_option_value], price: 11) }+ let!(:variant5) { create(:variant, product: product, option_values: [white_option_value, medium_option_value], price: 11) }+ let!(:variant6) { create(:variant, product: product, option_values: [white_option_value, large_option_value], price: 11) }++ it 'allows adding another option' do+ visit spree.edit_admin_product_path(product.reload)++ find('span', text: Spree.t('admin.variants_form.add_option.not_empty')).click+ tom_select('Material', from: Spree.t(:option_name), create: true)+ select_option_value('Cotton', index: 0, create: true)+ select_option_value('Polyester', index: 1, create: true)+ click_on 'Done'++ within('#option-Material') do+ expect(page).to have_content('Cotton')+ expect(page).to have_content('Polyester')+ end++ within('[data-test-id="product-variants-table"]') do+ within('[data-variant-name="Black/Small/Cotton"]') do+ within('.column-price') { find('input').set(10) }+ within('.column-quantity') { find('input').set(100) }+ end++ within('[data-variant-name="Black/Small/Polyester"]') do+ within('.column-price') { find('input').set(11) }+ within('.column-quantity') { find('input').set(110) }+ end++ within('[data-variant-name="Black/Medium/Cotton"]') do+ within('.column-price') { find('input').set(12) }+ within('.column-quantity') { find('input').set(120) }+ end++ within('[data-variant-name="Black/Medium/Polyester"]') do+ within('.column-price') { find('input').set(13) }+ within('.column-quantity') { find('input').set(130) }+ end++ within('[data-variant-name="Black/Large/Cotton"]') do+ within('.column-price') { find('input').set(14) }+ within('.column-quantity') { find('input').set(140) }+ end++ within('[data-variant-name="Black/Large/Polyester"]') do+ within('.column-price') { find('input').set(15) }+ within('.column-quantity') { find('input').set(150) }+ end++ within('[data-variant-name="White"]') do+ within('.column-price') { find('input').set(20) }+ within('.column-quantity') { find('input').set(200) }+ end+ end++ # Update product variants+ click_on 'Update', match: :first+ expect(page).to have_content('successfully updated')++ material_option = Spree::OptionType.find_by(name: 'material')++ within("#option-#{material_option.id}") do+ expect(page).to have_content('Cotton')+ expect(page).to have_content('Polyester')+ end++ within('[data-test-id="product-variants-table"]') do+ within('[data-variant-name="Black/Small/Cotton"]') do+ within('.column-price') { find('input').set(10) }+ within('.column-quantity') { find('input').set(100) }+ end++ within('[data-variant-name="Black/Small/Polyester"]') do+ within('.column-price') { find('input').set(11) }+ within('.column-quantity') { find('input').set(110) }+ end++ within('[data-variant-name="Black/Medium/Cotton"]') do+ within('.column-price') { find('input').set(12) }+ within('.column-quantity') { find('input').set(120) }+ end++ within('[data-variant-name="Black/Medium/Polyester"]') do+ within('.column-price') { find('input').set(13) }+ within('.column-quantity') { find('input').set(130) }+ end++ within('[data-variant-name="Black/Large/Cotton"]') do+ within('.column-price') { find('input').set(14) }+ within('.column-quantity') { find('input').set(140) }+ end++ within('[data-variant-name="Black/Large/Polyester"]') do+ within('.column-price') { find('input').set(15) }+ within('.column-quantity') { find('input').set(150) }+ end++ within('[data-variant-name="White/Small/Cotton"]') do+ within('.column-price') { find('input').set(20) }+ within('.column-quantity') { find('input').set(200) }+ end++ within('[data-variant-name="White/Small/Polyester"]') do+ within('.column-price') { find('input').set(20) }+ within('.column-quantity') { find('input').set(200) }+ end++ within('[data-variant-name="White/Medium/Cotton"]') do+ within('.column-price') { find('input').set(20) }+ within('.column-quantity') { find('input').set(200) }+ end++ within('[data-variant-name="White/Medium/Polyester"]') do+ within('.column-price') { find('input').set(20) }+ within('.column-quantity') { find('input').set(200) }+ end++ within('[data-variant-name="White/Large/Cotton"]') do+ within('.column-price') { find('input').set(20) }+ within('.column-quantity') { find('input').set(200) }+ end++ within('[data-variant-name="White/Large/Polyester"]') do+ within('.column-price') { find('input').set(20) }+ within('.column-quantity') { find('input').set(200) }+ end+ end++ expect(product.reload.variants.map(&:options_text)).to contain_exactly(+ 'Color: Black, Size: Small, and Material: Cotton',+ 'Color: Black, Size: Small, and Material: Polyester',+ 'Color: Black, Size: Medium, and Material: Cotton',+ 'Color: Black, Size: Medium, and Material: Polyester',+ 'Color: Black, Size: Large, and Material: Cotton',+ 'Color: Black, Size: Large, and Material: Polyester',+ 'Color: White, Size: Small, and Material: Cotton',+ 'Color: White, Size: Small, and Material: Polyester',+ 'Color: White, Size: Medium, and Material: Cotton',+ 'Color: White, Size: Medium, and Material: Polyester',+ 'Color: White, Size: Large, and Material: Cotton',+ 'Color: White, Size: Large, and Material: Polyester'+ )+ end++ it 'allows removing the first option' do+ visit spree.edit_admin_product_path(product.reload)++ within("#option-#{color_option.id}") do+ click_on 'Edit'+ click_on 'Delete'+ end++ expect(page).not_to have_css("#option-#{color_option.id}")+ expect(page).to have_css("#option-#{size_option.id}")++ within('[data-test-id="product-variants-table"]') do+ expect(page).not_to have_content('Black')+ expect(page).not_to have_content('White')++ expect(page).to have_content('Small')+ expect(page).to have_content('Medium')+ expect(page).to have_content('Large')++ within('[data-variant-name="Small"]') do+ within('.column-price') { find('input').set(10) }+ within('.column-quantity') { find('input').set(100) }+ end++ within('[data-variant-name="Medium"]') do+ within('.column-price') { find('input').set(11) }+ within('.column-quantity') { find('input').set(110) }+ end++ within('[data-variant-name="Large"]') do+ within('.column-price') { find('input').set(12) }+ within('.column-quantity') { find('input').set(120) }+ end+ end++ # Update product variants+ click_on 'Update', match: :first+ expect(page).to have_content('successfully updated')+ within('.alerts-container') { find('button').click }++ within('[data-test-id="product-variants-table"]') do+ within('[data-variant-name="Small"]') do+ within('.column-price') { expect(page).to have_field(with: 10) }+ within('.column-quantity') { expect(page).to have_field(with: 100) }+ end++ within('[data-variant-name="Medium"]') do+ within('.column-price') { expect(page).to have_field(with: 11) }+ within('.column-quantity') { expect(page).to have_field(with: 110) }+ end++ within('[data-variant-name="Large"]') do+ within('.column-price') { expect(page).to have_field(with: 12) }+ within('.column-quantity') { expect(page).to have_field(with: 120) }+ end+ end++ expect(product.reload.variants.map(&:options_text)).to contain_exactly(+ 'Size: Small',+ 'Size: Medium',+ 'Size: Large'+ )++ within('[data-test-id="product-variants-table"]') do+ within('[data-variant-name="Small"]') do+ find('.column-checkbox').click+ end+ end++ click_on 'Delete selected'++ # Update product variants+ click_on 'Update', match: :first+ expect(page).to have_content('successfully updated')+ within('.alerts-container') { find('button').click }++ within('[data-test-id="product-variants-table"]') do+ within('[data-variant-name="Medium"]') do+ within('.column-price') { expect(page).to have_field(with: 11) }+ within('.column-quantity') { expect(page).to have_field(with: 110) }+ end++ within('[data-variant-name="Large"]') do+ within('.column-price') { expect(page).to have_field(with: 12) }+ within('.column-quantity') { expect(page).to have_field(with: 120) }+ end+ end++ expect(product.reload.variants.map(&:options_text)).to contain_exactly(+ 'Size: Medium',+ 'Size: Large'+ )+ end++ it 'allows removing the last option' do+ visit spree.edit_admin_product_path(product.reload)++ within("#option-#{size_option.id}") do+ click_on 'Edit'+ click_on 'Delete'+ end++ expect(page).to have_css("#option-#{color_option.id}")+ expect(page).not_to have_css("#option-#{size_option.id}")++ within('[data-test-id="product-variants-table"]') do+ expect(page).to have_content('Black')+ expect(page).to have_content('White')++ expect(page).not_to have_content('Small')+ expect(page).not_to have_content('Medium')+ expect(page).not_to have_content('Large')+ end++ # Update product variants+ click_on 'Update', match: :first+ expect(page).to have_content('successfully updated')++ within('[data-test-id="product-variants-table"]') do+ expect(page).to have_content('Black')+ expect(page).to have_content('White')++ expect(page).not_to have_content('Small')+ expect(page).not_to have_content('Medium')+ expect(page).not_to have_content('Large')+ end++ expect(product.reload.variants.map(&:options_text)).to contain_exactly(+ 'Color: Black',+ 'Color: White'+ )+ end++ it 'allows removing all options' do+ visit spree.edit_admin_product_path(product.reload)++ within("#option-#{color_option.id}") do+ click_on 'Edit'+ click_on 'Delete'+ end++ within("#option-#{size_option.id}") do+ click_on 'Edit'+ click_on 'Delete'+ end++ expect(page).not_to have_css("#option-#{color_option.id}")+ expect(page).not_to have_css("#option-#{size_option.id}")+ expect(page).not_to have_css('[data-test-id="product-variants-table"]')++ # Update product variants+ click_on 'Update', match: :first+ expect(page).to have_content('successfully updated')++ within('.variants-form') { expect(page).to have_content(Spree.t('admin.variants_form.add_option.empty')) }+ expect(product.reload.variants).to be_empty+ end++ it 'allows removing all options and adding one back and a new one' do+ visit spree.edit_admin_product_path(product.reload)++ within("#option-#{color_option.id}") do+ click_on 'Edit'+ click_on 'Delete'+ end++ within("#option-#{size_option.id}") do+ click_on 'Edit'+ click_on 'Delete'+ end++ # Add color back+ find('span', text: Spree.t('admin.variants_form.add_option.empty')).click+ tom_select('Color', from: Spree.t(:option_name))+ select_option_value('White', index: 0)+ click_on 'Done'++ within("#option-#{color_option.id}") do+ expect(page).to have_content('White')+ end++ # Add a new material option+ find('span', text: Spree.t('admin.variants_form.add_option.not_empty')).click+ tom_select('Material', from: Spree.t(:option_name), create: true)+ select_option_value('Cotton', index: 0, create: true)+ select_option_value('Polyester', index: 1, create: true)+ click_on 'Done'++ within('#option-Material') do+ expect(page).to have_content('Cotton')+ expect(page).to have_content('Polyester')+ end++ within('[data-test-id="product-variants-table"]') do+ expect(page).not_to have_content('Black')+ expect(page).not_to have_content('Small')+ expect(page).not_to have_content('Medium')+ expect(page).not_to have_content('Large')++ expect(page).to have_content('White')+ expect(page).to have_content('Cotton')+ expect(page).to have_content('Polyester')++ within('[data-variant-name="White/Cotton"]') do+ within('.column-price') { find('input').set(20) }+ within('.column-quantity') { find('input').set(200) }+ end++ within('[data-variant-name="White/Polyester"]') do+ within('.column-price') { find('input').set(21) }+ within('.column-quantity') { find('input').set(210) }+ end+ end++ # Update product variants+ click_on 'Update', match: :first+ expect(page).to have_content('successfully updated')++ within('[data-test-id="product-variants-table"]') do+ within('[data-variant-name="White/Cotton"]') do+ within('.column-price') { expect(page).to have_field(with: 20) }+ within('.column-quantity') { expect(page).to have_field(with: 200) }+ end++ within('[data-variant-name="White/Polyester"]') do+ within('.column-price') { expect(page).to have_field(with: 21) }+ within('.column-quantity') { expect(page).to have_field(with: 210) }+ end+ end++ expect(product.reload.variants.map(&:options_text)).to contain_exactly(+ 'Color: White, Material: Cotton',+ 'Color: White, Material: Polyester'+ )+ end++ it 'allows removing selected variants' do+ red_option_value = create(:option_value, name: 'red', presentation: 'Red', option_type: color_option)+ create(:variant, product: product, option_values: [red_option_value, small_option_value], price: 10)+ create(:variant, product: product, option_values: [red_option_value, medium_option_value], price: 10)+ create(:variant, product: product, option_values: [red_option_value, large_option_value], price: 10)++ visit spree.edit_admin_product_path(product.reload)++ # Remove Red color entirely and Black/Medium variant+ within('[data-test-id="product-variants-table"]') do+ within('[data-variant-name="Red"]') do+ find('.column-checkbox').click+ end+ end++ click_on 'Delete selected'++ within("#option-#{color_option.id}") do+ expect(page).not_to have_content('Red')+ expect(page).to have_content('Black')+ expect(page).to have_content('White')+ end++ within('[data-test-id="product-variants-table"]') do+ expect(page).not_to have_content('Red')+ expect(page).to have_content('Black')+ expect(page).to have_content('White')+ end++ within('[data-test-id="product-variants-table"]') do+ within('[data-variant-name="Black/Medium"]') do+ find('.column-checkbox').click+ end+ end++ click_on 'Delete selected'++ # Update product variants+ click_on 'Update', match: :first+ expect(page).to have_content('successfully updated')+ within('.alerts-container') { find('button').click }++ expect(product.reload.variants.map(&:options_text)).to contain_exactly(+ 'Color: Black, Size: Small',+ 'Color: Black, Size: Large',+ 'Color: White, Size: Small',+ 'Color: White, Size: Medium',+ 'Color: White, Size: Large'+ )++ # Remove White/Small and White/Large variants+ within('[data-test-id="product-variants-table"]') do+ within('[data-variant-name="White/Small"]') do+ find('.column-checkbox').click+ end++ within('[data-variant-name="White/Large"]') do+ find('.column-checkbox').click+ end+ end++ click_on 'Delete selected'++ # Update product variants+ click_on 'Update', match: :first+ expect(page).to have_content('successfully updated')+ within('.alerts-container') { find('button').click }++ expect(product.reload.variants.map(&:options_text)).to contain_exactly(+ 'Color: Black, Size: Small',+ 'Color: Black, Size: Large',+ 'Color: White, Size: Medium'+ )++ # Remove the last White/Medium variant and then Black/Medium and Black/Large variants+ within('[data-test-id="product-variants-table"]') do+ expect(page).to have_css('[data-variant-name="White"]')+ expect(page).to have_css('[data-variant-name="White/Medium"]')++ expect(page).not_to have_css('[data-variant-name="White/Small"]')+ expect(page).not_to have_css('[data-variant-name="White/Large"]')++ within('[data-variant-name="White/Medium"]') do+ find('.column-checkbox').click+ end+ end++ click_on 'Delete selected'++ within("#option-#{color_option.id}") do+ expect(page).not_to have_content('White')+ expect(page).to have_content('Black')+ end++ within('[data-test-id="product-variants-table"]') do+ within('[data-variant-name="Black/Medium"]') do+ find('.column-checkbox').click+ end++ within('[data-variant-name="Black/Large"]') do+ find('.column-checkbox').click+ end+ end++ click_on 'Delete selected'++ # Update product variants+ click_on 'Update', match: :first+ expect(page).to have_content('successfully updated')+ within('.alerts-container') { find('button').click }++ within("#option-#{color_option.id}") do+ expect(page).to have_content('Black')+ expect(page).not_to have_content('White')+ end++ within("#option-#{size_option.id}") do+ expect(page).to have_content('Small')+ expect(page).not_to have_content('Medium')+ expect(page).not_to have_content('Large')+ end++ expect(product.reload.variants.map(&:options_text)).to contain_exactly(+ 'Color: Black, Size: Small'+ )+ end++ it 'uses the parent price for new variants' do+ visit spree.edit_admin_product_path(product.reload)++ within('[data-test-id="product-variants-table"]') do+ within('[data-variant-name="Black"]') do+ within('.column-price') { expect(page).to have_field(with: 10) }+ end++ within('[data-variant-name="White"]') do+ within('.column-price') { expect(page).to have_field(with: 11) }+ end+ end++ within("#option-#{size_option.id}") do+ click_on 'Edit'+ select_option_value('Extra Large', index: 3, create: true)+ click_on 'Done'+ end++ within('[data-test-id="product-variants-table"]') do+ within('[data-variant-name="Black/Extra Large"]') do+ within('.column-price') { expect(page).to have_field(with: 10) }+ end++ within('[data-variant-name="White/Extra Large"]') do+ within('.column-price') { expect(page).to have_field(with: 11) }+ end+ end++ # Update product variants+ click_on 'Update', match: :first+ expect(page).to have_content('successfully updated')+ within('.alerts-container') { find('button').click }++ within('[data-test-id="product-variants-table"]') do+ within('[data-variant-name="Black/Extra Large"]') do+ within('.column-price') { expect(page).to have_field(with: 10) }+ end++ within('[data-variant-name="White/Extra Large"]') do+ within('.column-price') { expect(page).to have_field(with: 11) }+ end+ end++ expect(product.reload.variants.map(&:options_text)).to contain_exactly(+ 'Color: Black, Size: Small',+ 'Color: Black, Size: Medium',+ 'Color: Black, Size: Large',+ 'Color: Black, Size: Extra Large',+ 'Color: White, Size: Small',+ 'Color: White, Size: Medium',+ 'Color: White, Size: Large',+ 'Color: White, Size: Extra Large'+ )+ end+ endendcontext 'filtering products', js: true dodiff --git a/core/app/services/spree/products/prepare_nested_attributes.rb b/core/app/services/spree/products/prepare_nested_attributes.rbindex dc8cd7b08a..35c2e945ac 100644--- a/core/app/services/spree/products/prepare_nested_attributes.rb+++ b/core/app/services/spree/products/prepare_nested_attributes.rb@@ -45,8 +45,6 @@ module Spreeendend- params.delete(:variants_attributes) if params[:variants_attributes].blank?-# mark resource properties to be removed# when value is left blankif params[:product_properties_attributes].present?@@ -62,6 +60,18 @@ module Spree# ensure there is at least one storeparams[:store_ids] = [store.id] if params[:store_ids].blank?+ # Add empty list for option_type_ids and mark variants as removed if there are no variants and options+ if params[:variants_attributes].blank? && variants_to_remove.any? && !params.key?(:option_type_ids)+ params[:option_type_ids] = []+ params[:variants_attributes] = {}++ variants_to_remove.each_with_index do |variant_id, index|+ params[:variants_attributes][index.to_s] = { id: variant_id, _destroy: '1' }+ end++ params[:variants_attributes].permit!+ end+paramsend