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
Ability to clear metafield when passed empty value (#13242)
Damian Legawiec70942415f22a
diff --git a/core/.claude/settings.local.json b/core/.claude/settings.local.jsonnew file mode 100644index 0000000000..9e558154a7--- /dev/null+++ b/core/.claude/settings.local.json@@ -0,0 +1,9 @@+{+ "permissions": {+ "allow": [+ "Bash(bundle exec rspec:*)"+ ],+ "deny": [],+ "ask": []+ }+}diff --git a/core/app/models/concerns/spree/metafields.rb b/core/app/models/concerns/spree/metafields.rbindex 58d0d8e1ec..d895d9ddf7 100644--- a/core/app/models/concerns/spree/metafields.rb+++ b/core/app/models/concerns/spree/metafields.rb@@ -24,6 +24,22 @@ module Spreemf[:metafield_definition_id].blank? || (mf[:id].blank? && mf[:value].blank?)}+ # Override metafields_attributes= to automatically mark existing metafields+ # with empty values for destruction+ def metafields_attributes=(attributes)+ attributes = attributes.values if attributes.is_a?(Hash)++ attributes.each do |attrs|+ # If this is an existing metafield (has an id) and value is blank,+ # mark it for destruction+ if attrs[:id].present? && value_blank?(attrs[:value])+ attrs[:_destroy] = true+ end+ end++ super(attributes)+ end+scope :with_metafield_key, ->(key_with_namespace) {namespace, key = extract_namespace_and_key(key_with_namespace)joins(metafields: :metafield_definition).where(spree_metafield_definitions: { namespace: namespace, key: key })@@ -67,6 +83,12 @@ module Spreemetafields.with_key(namespace, key).exists?end++ private++ def value_blank?(value)+ value.blank?+ endendendenddiff --git a/core/app/services/spree/imports/row_processors/product_variant.rb b/core/app/services/spree/imports/row_processors/product_variant.rbindex 5d684d8813..b42ab9fba1 100644--- a/core/app/services/spree/imports/row_processors/product_variant.rb+++ b/core/app/services/spree/imports/row_processors/product_variant.rb@@ -168,14 +168,46 @@ module Spreereturn unless product.class.included_modules.include?(Spree::Metafields)metafield_attributes = attributes.select { |key, _value| key.to_s.start_with?('metafield.') }-+ return if metafield_attributes.empty?++ # Build nested attributes for metafields+ nested_attrs = []+metafield_attributes.each do |attribute_key, value|- next if value.blank?-# Extract namespace.key from "metafield.namespace.key"full_key = attribute_key.to_s.sub(/^metafield\./, '')- product.set_metafield(full_key, value.to_s.strip)+ namespace, key = product.extract_namespace_and_key(full_key)++ # Find or initialize metafield definition+ metafield_definition = Spree::MetafieldDefinition.find_by(+ namespace: namespace,+ key: key,+ resource_type: product.class.name+ )++ next unless metafield_definition++ # Find existing metafield if product is persisted+ existing_metafield = product.persisted? ? product.metafields.find_by(metafield_definition: metafield_definition) : nil++ # Skip blank values for new metafields+ next if value.blank? && existing_metafield.nil?++ # For existing metafields with blank values, we'll mark them for destruction+ # For new metafields, we skip them (handled above)+ # For existing or new metafields with values, we create/update them+ metafield_attrs = {+ metafield_definition_id: metafield_definition.id,+ value: value.to_s.strip,+ type: metafield_definition.metafield_type+ }++ metafield_attrs[:id] = existing_metafield.id if existing_metafield++ nested_attrs << metafield_attrsend++ product.update(metafields_attributes: nested_attrs) unless nested_attrs.empty?enddef to_spree_status(status)diff --git a/core/spec/models/concerns/spree/metafields_spec.rb b/core/spec/models/concerns/spree/metafields_spec.rbindex 779d325d02..8b473ed690 100644--- a/core/spec/models/concerns/spree/metafields_spec.rb+++ b/core/spec/models/concerns/spree/metafields_spec.rb@@ -143,5 +143,95 @@ RSpec.describe Spree::Metafields, type: :concern doproduct.update(attrs)}.not_to change { product.metafields.count }end++ context 'auto-destroy metafields with empty values' do+ let!(:metafield) do+ product.metafields.create!(+ metafield_definition: definition,+ value: 'initial value',+ type: definition.metafield_type+ )+ end++ it 'destroys existing metafield when value is set to empty string' do+ attrs = {+ metafields_attributes: [+ {+ id: metafield.id,+ metafield_definition_id: definition.id,+ value: '',+ type: definition.metafield_type+ }+ ]+ }+ expect {+ product.update(attrs)+ }.to change { product.metafields.count }.by(-1)+ end++ it 'destroys existing metafield when value is set to nil' do+ attrs = {+ metafields_attributes: [+ {+ id: metafield.id,+ metafield_definition_id: definition.id,+ value: nil,+ type: definition.metafield_type+ }+ ]+ }+ expect {+ product.update(attrs)+ }.to change { product.metafields.count }.by(-1)+ end++ it 'updates existing metafield when value is not empty' do+ attrs = {+ metafields_attributes: [+ {+ id: metafield.id,+ metafield_definition_id: definition.id,+ value: 'updated value',+ type: definition.metafield_type+ }+ ]+ }+ expect {+ product.update(attrs)+ }.not_to change { product.metafields.count }+ expect(metafield.reload.value).to eq('updated value')+ end++ it 'handles multiple metafields correctly' do+ other_definition = create(:metafield_definition, namespace: 'custom', key: 'bar', resource_type: 'Spree::Product')+ other_metafield = product.metafields.create!(+ metafield_definition: other_definition,+ value: 'other value',+ type: other_definition.metafield_type+ )++ attrs = {+ metafields_attributes: [+ {+ id: metafield.id,+ metafield_definition_id: definition.id,+ value: '',+ type: definition.metafield_type+ },+ {+ id: other_metafield.id,+ metafield_definition_id: other_definition.id,+ value: 'updated other value',+ type: other_definition.metafield_type+ }+ ]+ }+ expect {+ product.update(attrs)+ }.to change { product.metafields.count }.by(-1)+ expect(product.metafields.pluck(:id)).not_to include(metafield.id)+ expect(other_metafield.reload.value).to eq('updated other value')+ end+ endendenddiff --git a/core/spec/services/spree/imports/row_processors/product_variant_spec.rb b/core/spec/services/spree/imports/row_processors/product_variant_spec.rbindex 9c1956a1a9..a13240b182 100644--- a/core/spec/services/spree/imports/row_processors/product_variant_spec.rb+++ b/core/spec/services/spree/imports/row_processors/product_variant_spec.rb@@ -441,5 +441,67 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service doexpect(product.has_metafield?('custom.material')).to be falseendend++ context 'when updating existing product metafields with blank values' do+ let!(:existing_product) do+ p = create(:product, slug: 'denim-shirt', name: 'Denim Shirt', stores: [store])+ p.set_metafield('custom.brand', 'Old Brand')+ p.set_metafield('custom.material', 'Old Material')+ p+ end++ let(:row_data) do+ base_data = csv_row_hash(+ 'slug' => 'denim-shirt',+ 'name' => 'Denim Shirt',+ 'status' => 'active',+ 'price' => '62.99',+ 'currency' => 'USD'+ )+ base_data.merge(+ 'metafield.custom.brand' => 'New Brand',+ 'metafield.custom.material' => ''+ )+ end++ it 'removes existing metafield when empty value is uploaded' do+ expect(existing_product.metafields.count).to eq 2++ product = variant.product++ expect(product.id).to eq existing_product.id+ expect(product.has_metafield?('custom.brand')).to be true+ expect(product.get_metafield('custom.brand').value).to eq 'New Brand'+ expect(product.has_metafield?('custom.material')).to be false+ expect(product.metafields.count).to eq 1+ end++ context 'when all metafields have blank values' do+ let(:row_data) do+ base_data = csv_row_hash(+ 'slug' => 'denim-shirt',+ 'name' => 'Denim Shirt',+ 'status' => 'active',+ 'price' => '62.99',+ 'currency' => 'USD'+ )+ base_data.merge(+ 'metafield.custom.brand' => '',+ 'metafield.custom.material' => ''+ )+ end++ it 'removes all existing metafields' do+ expect(existing_product.metafields.count).to eq 2++ product = variant.product++ expect(product.id).to eq existing_product.id+ expect(product.has_metafield?('custom.brand')).to be false+ expect(product.has_metafield?('custom.material')).to be false+ expect(product.metafields.count).to eq 0+ end+ end+ endendenddiff --git a/storefront/.claude/settings.local.json b/storefront/.claude/settings.local.jsonnew file mode 100644index 0000000000..c632575e5c--- /dev/null+++ b/storefront/.claude/settings.local.json@@ -0,0 +1,9 @@+{+ "permissions": {+ "allow": [+ "WebSearch"+ ],+ "deny": [],+ "ask": []+ }+}