Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Better way of saving options in the product's variants form (#12804)

Damian Kaczmarczyk1d61e54542e2
diff --git a/admin/app/controllers/spree/admin/products_controller.rb b/admin/app/controllers/spree/admin/products_controller.rb
index 41bb4b3201..5a48fecf3b 100644
--- a/admin/app/controllers/spree/admin/products_controller.rb
+++ b/admin/app/controllers/spree/admin/products_controller.rb
@@ -180,10 +180,10 @@ module Spree
@product_options[option_type.id.to_s] = {
name: option_type.presentation,
position: index + 1,
- values: option_values.pluck(:presentation).uniq
+ values: option_values.pluck(:name, :presentation).uniq.map { |name, presentation| { value: name, text: presentation } }
}
- @product_available_options[option_type.id.to_s] = option_type.option_values.to_tom_select_json
+ @product_available_options[option_type.id.to_s] = option_type.option_values.pluck(:name, :presentation).uniq.map { |name, presentation| { id: name, name: presentation } }
end
@product_stock = {}
diff --git a/admin/app/javascript/spree/admin/controllers/multi_tom_select_controller.js b/admin/app/javascript/spree/admin/controllers/multi_tom_select_controller.js
index 5f4688a771..d58172a3c6 100644
--- a/admin/app/javascript/spree/admin/controllers/multi_tom_select_controller.js
+++ b/admin/app/javascript/spree/admin/controllers/multi_tom_select_controller.js
@@ -26,8 +26,9 @@ export default class extends Controller {
values() {
return this.selectTargets
.map((selectTarget) => selectTarget.querySelector('select'))
- .map((select) => select.options[select.selectedIndex].text)
- .filter((text) => text?.length > 0)
+ .map((select) => select.options[select.selectedIndex])
+ .filter((option) => option.value.length > 0)
+ .map((option) => ({ text: option.text, value: option.value}))
}
setValues() {
diff --git a/admin/app/javascript/spree/admin/controllers/variants_form_controller.js b/admin/app/javascript/spree/admin/controllers/variants_form_controller.js
index 47cc5db20b..12ffe9f88c 100644
--- a/admin/app/javascript/spree/admin/controllers/variants_form_controller.js
+++ b/admin/app/javascript/spree/admin/controllers/variants_form_controller.js
@@ -129,7 +129,7 @@ export default class extends CheckboxSelectAll {
const nestingLevel = internalName.split('/').length
if (nestingLevel === 1) {
const firstOptionKey = Object.keys(this.optionsValue)[0]
- const newOptionValues = this.optionsValue[firstOptionKey].values.filter((value) => value !== internalName)
+ const newOptionValues = this.optionsValue[firstOptionKey].values.filter((value) => value.text !== internalName)
if (newOptionValues.length === 0) {
const newOptionsValue = this.optionsValue
delete newOptionsValue[firstOptionKey]
@@ -265,12 +265,12 @@ export default class extends CheckboxSelectAll {
let name = ''
let internalName = name
if (i === 0) {
- name = variant[keys[i]]
+ name = variant[keys[i]].text
internalName = name
} else {
- const namesPath = keys.slice(1, keys.length).map((key) => variant[key])
+ const namesPath = keys.slice(1, keys.length).map((key) => variant[key].text)
name = namesPath.join(' / ')
- internalName = `${variant[keys[0]]}/${namesPath.join('/')}`
+ internalName = `${variant[keys[0]].text}/${namesPath.join('/')}`
}
return { name, internalName }
@@ -572,12 +572,12 @@ export default class extends CheckboxSelectAll {
refreshParentInputs() {
const firstOption = Object.values(this.optionsValue)[0]
if (firstOption) {
- firstOption.values.forEach((name) => {
+ firstOption.values.forEach((option) => {
this.currenciesValue.forEach((currency) => {
- this.updateParentPriceRange(name, currency)
+ this.updateParentPriceRange(option.text, currency)
})
this.stockLocationsValue.forEach((stockLocationId) => {
- this.updateParentStockSum(name, stockLocationId)
+ this.updateParentStockSum(option.text, stockLocationId)
})
this.updateShopLocationCountOnHand()
})
@@ -615,11 +615,17 @@ export default class extends CheckboxSelectAll {
.find((option) => option.name === key).position
inputs.push(positionInput)
- const valueInput = document.createElement('input')
- valueInput.type = 'hidden'
- valueInput.name = `product[variants_attributes][${i}][options][][value]`
- valueInput.value = variant[key]
- inputs.push(valueInput)
+ const optionNameInput = document.createElement('input')
+ optionNameInput.type = 'hidden'
+ optionNameInput.name = `product[variants_attributes][${i}][options][][option_value_name]`
+ optionNameInput.value = variant[key].value
+ inputs.push(optionNameInput)
+
+ const optionIdInput = document.createElement('input')
+ optionIdInput.type = 'hidden'
+ optionIdInput.name = `product[variants_attributes][${i}][options][][option_value_presentation]`
+ optionIdInput.value = variant[key].text
+ inputs.push(optionIdInput)
})
return inputs
@@ -924,8 +930,8 @@ export default class extends CheckboxSelectAll {
const optionValuesSelectContainer = template.querySelector('[data-slot="optionValuesSelectContainer"]')
const tomSelectOptionValues = optionValues.map((optionValue) => {
return {
- id: availableOptions.find((availableOption) => availableOption.name === optionValue)?.id,
- name: optionValue
+ id: optionValue.value,
+ name: optionValue.text,
}
})
@@ -948,8 +954,8 @@ export default class extends CheckboxSelectAll {
values.forEach((value) => {
const template = this.optionValueTemplateTarget.content.cloneNode(true)
const optionValueNameEl = template.querySelector('[data-slot="optionValueName"]')
- optionValueNameEl.textContent = value
- optionValueNameEl.dataset.name = value
+ optionValueNameEl.textContent = value.text
+ optionValueNameEl.dataset.name = value.text
templates.push(template)
})
diff --git a/admin/spec/controllers/spree/admin/products_controller_spec.rb b/admin/spec/controllers/spree/admin/products_controller_spec.rb
index d0f12f226f..9241faf495 100644
--- a/admin/spec/controllers/spree/admin/products_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/products_controller_spec.rb
@@ -237,13 +237,15 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
id: nil,
name: 'Color',
position: 1,
- value: 'Red'
+ option_value_presentation: 'Red',
+ option_value_name: nil
},
{
id: nil,
name: 'Not existing option',
position: 2,
- value: 'Not existing value'
+ option_value_presentation: 'Not existing value',
+ option_value_name: nil
}
]
},
@@ -263,13 +265,15 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
id: nil,
name: 'Color',
position: 1,
- value: 'Blue'
+ option_value_presentation: 'Blue',
+ option_value_name: nil
},
{
id: nil,
name: 'Not existing option',
position: 2,
- value: 'Not existing value2'
+ option_value_presentation: 'Not existing value2',
+ option_value_name: nil
}
]
}
@@ -572,6 +576,7 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
let(:stock_location) { create(:stock_location) }
let(:other_stock_location) { create(:stock_location) }
let(:new_option_type) { create(:option_type, name: 'Material', presentation: 'Fabric') }
+ let(:silk_option_value) { create(:option_value, name: 'Silk', option_type: new_option_type, presentation: 'Silk') }
let(:product_params) do
{
@@ -593,19 +598,22 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
id: nil,
name: 'Color',
position: 1,
- value: 'Red'
+ option_value_presentation: 'Red',
+ option_value_name: nil
},
{
id: nil,
name: 'Not existing option',
position: 2,
- value: 'Not existing value'
+ option_value_presentation: 'Not existing value',
+ option_value_name: nil
},
{
id: new_option_type.id,
name: 'Fabric',
position: 3,
- value: 'Silk'
+ option_value_presentation: silk_option_value.presentation,
+ option_value_name: silk_option_value.name
}
]
},
@@ -625,19 +633,22 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
id: nil,
name: 'Color',
position: 1,
- value: 'Blue'
+ option_value_presentation: 'Blue',
+ option_value_name: nil
},
{
id: nil,
name: 'Not existing option',
position: 2,
- value: 'Not existing value2'
+ option_value_presentation: 'Not existing value2',
+ option_value_name: nil
},
{
id: new_option_type.id,
name: 'Fabric',
position: 3,
- value: 'Cotton'
+ option_value_presentation: 'Cotton',
+ option_value_name: nil
}
]
}
@@ -748,13 +759,15 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
id: nil,
name: 'Color',
position: 1,
- value: 'Red'
+ option_value_presentation: red_option_value.presentation,
+ option_value_name: red_option_value.name
},
{
id: nil,
name: 'Size',
position: 2,
- value: 'Small'
+ option_value_presentation: small_option_value.presentation,
+ option_value_name: small_option_value.name
}
]
},
@@ -776,13 +789,15 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
id: nil,
name: 'Color',
position: 1,
- value: 'Blue'
+ option_value_presentation: blue_option_value.presentation,
+ option_value_name: blue_option_value.name
},
{
id: nil,
name: 'Size',
position: 2,
- value: 'Large'
+ option_value_presentation: large_option_value.presentation,
+ option_value_name: large_option_value.name
}
]
},
@@ -804,13 +819,15 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
id: nil,
name: 'Color',
position: 1,
- value: 'Red'
+ option_value_presentation: red_option_value.presentation,
+ option_value_name: red_option_value.name
},
{
id: nil,
name: 'Size',
position: 2,
- value: 'Large'
+ option_value_presentation: large_option_value.presentation,
+ option_value_name: large_option_value.name
}
]
}
@@ -860,13 +877,15 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
id: nil,
name: 'Size',
position: 1,
- value: 'S'
+ option_value_presentation: small_option_value.presentation,
+ option_value_name: small_option_value.name
},
{
id: nil,
name: 'Color',
position: 2,
- value: 'Red'
+ option_value_presentation: red_option_value.presentation,
+ option_value_name: red_option_value.name
}
]
},
@@ -881,13 +900,15 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
id: nil,
name: 'Size',
position: 1,
- value: 'M'
+ option_value_presentation: large_option_value.presentation,
+ option_value_name: large_option_value.name
},
{
id: nil,
name: 'Color',
position: 2,
- value: 'Blue'
+ option_value_presentation: blue_option_value.presentation,
+ option_value_name: blue_option_value.name
}
]
}
@@ -925,7 +946,8 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
id: nil,
name: 'Size',
position: 1,
- value: 'S'
+ option_value_presentation: small_option_value.presentation,
+ option_value_name: small_option_value.name
}
],
stock_items_attributes: {
@@ -951,7 +973,8 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
id: nil,
name: 'Size',
position: 1,
- value: 'M'
+ option_value_presentation: large_option_value.presentation,
+ option_value_name: large_option_value.name
}
]
}
diff --git a/core/app/services/spree/products/prepare_nested_attributes.rb b/core/app/services/spree/products/prepare_nested_attributes.rb
index e6f341b503..03a8d4abee 100644
--- a/core/app/services/spree/products/prepare_nested_attributes.rb
+++ b/core/app/services/spree/products/prepare_nested_attributes.rb
@@ -136,8 +136,15 @@ module Spree
o.position = opt[:position]
o.save!
end
- option_value = option_type.option_values.where(name: opt[:value].parameterize).first_or_initialize do |o|
- o.name = o.presentation = opt[:value]
+
+ option_value_identificator = if opt[:option_value_name].present?
+ opt[:option_value_name]
+ else
+ opt[:option_value_presentation].parameterize
+ end
+
+ option_value = option_type.option_values.where(name: option_value_identificator).first_or_initialize do |o|
+ o.name = o.presentation = opt[:option_value_presentation]
o.save!
end
diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb
index e4ab7c6fb3..3aa857a6a1 100644
--- a/core/lib/spree/permitted_attributes.rb
+++ b/core/lib/spree/permitted_attributes.rb
@@ -271,7 +271,7 @@ module Spree
:weight, :height, :width, :depth, :sku, :barcode, :cost_currency,
:weight_unit, :dimensions_unit,
{
- options: [:id, :name, :value, :position, :_destroy],
+ options: [:id, :name, :option_value_presentation, :option_value_name, :position, :_destroy],
stock_items_attributes: [:id, :count_on_hand, :stock_location_id, :backorderable, :_destroy],
prices_attributes: [:id, :amount, :compare_at_amount, :currency, :_destroy],
price: {},