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.

Discontinue variants with completed orders, instead of destroying them (#13760)

Frederik Kragh Nicolajsen10bd64d09c6e
diff --git a/spree/admin/app/controllers/spree/admin/products_controller.rb b/spree/admin/app/controllers/spree/admin/products_controller.rb
index 58550b0381..263dbe0643 100644
--- a/spree/admin/app/controllers/spree/admin/products_controller.rb
+++ b/spree/admin/app/controllers/spree/admin/products_controller.rb
@@ -57,7 +57,17 @@ module Spree
def update
invoke_callbacks(:update, :before)
- if @product.update(permitted_resource_params)
+ success = ActiveRecord::Base.transaction do
+ @prepare_params_service&.variants_to_discontinue&.each(&:discontinue!)
+
+ unless @product.update(permitted_resource_params)
+ raise ActiveRecord::Rollback
+ end
+
+ true
+ end
+
+ if success
set_current_store
invoke_callbacks(:update, :after)
flash[:success] = flash_message_for(@product, :successfully_updated)
@@ -202,8 +212,8 @@ module Spree
end
def prepare_product_params
- params_service = Spree::Products::PrepareNestedAttributes.new(@product, current_store, permitted_resource_params, current_ability)
- params[:product] = params_service.call
+ @prepare_params_service = Spree::Products::PrepareNestedAttributes.new(@product, current_store, permitted_resource_params, current_ability)
+ params[:product] = @prepare_params_service.call
end
# These includes are not picked automatically by ar_lazy_preload gem so we need to specify them manually.
diff --git a/spree/core/app/services/spree/products/prepare_nested_attributes.rb b/spree/core/app/services/spree/products/prepare_nested_attributes.rb
index 6f7f639988..bef751d0cc 100644
--- a/spree/core/app/services/spree/products/prepare_nested_attributes.rb
+++ b/spree/core/app/services/spree/products/prepare_nested_attributes.rb
@@ -17,11 +17,14 @@ module Spree
# prepared_params = service.call
#
class PrepareNestedAttributes
+ attr_reader :variants_to_discontinue
+
def initialize(product, store, params, ability)
@product = product
@store = store
@params = params
@ability = ability
+ @variants_to_discontinue = []
end
def call
@@ -75,7 +78,8 @@ module Spree
params[:option_type_ids] = []
params[:variants_attributes] = {}
- variants_to_remove.each_with_index do |variant_id, index|
+ populate_variants_to_discontinue
+ variant_ids_to_destroy.each_with_index do |variant_id, index|
params[:variants_attributes][index.to_s] = { id: variant_id, _destroy: '1' }
end
@@ -122,15 +126,37 @@ module Spree
def removed_variants_attributes
return {} unless can_remove_variants?
+ populate_variants_to_discontinue
+
attributes = {}
last_index = params[:variants_attributes].keys.map(&:to_i).max
- variants_to_remove.each_with_index do |variant_id, index|
- attributes[(index + last_index + 1).to_s] = { id: variant_id, _destroy: '1' }
+ variant_ids_to_destroy.each_with_index do |variant_id, index|
+ attributes[(last_index + 1 + index).to_s] = { id: variant_id, _destroy: '1' }
end
attributes
end
+ def populate_variants_to_discontinue
+ ids = variants_to_remove.select { |vid| variant_ids_with_completed_orders.include?(vid) }
+ @variants_to_discontinue = product.variants.where(id: ids).to_a if ids.any?
+ end
+
+ def variant_ids_to_destroy
+ variants_to_remove - variant_ids_with_completed_orders
+ end
+
+ def variant_ids_with_completed_orders
+ @variant_ids_with_completed_orders ||=
+ product.variants
+ .joins(:orders)
+ .merge(Spree::Order.complete)
+ .reorder(nil)
+ .distinct
+ .pluck(:id)
+ .map(&:to_s)
+ end
+
def removed_product_option_types_attributes
return {} unless can_manage_option_types?
diff --git a/spree/core/spec/services/spree/products/prepare_nested_attributes_spec.rb b/spree/core/spec/services/spree/products/prepare_nested_attributes_spec.rb
index d693afd122..f0d046c7f1 100644
--- a/spree/core/spec/services/spree/products/prepare_nested_attributes_spec.rb
+++ b/spree/core/spec/services/spree/products/prepare_nested_attributes_spec.rb
@@ -1,7 +1,8 @@
require 'spec_helper'
RSpec.describe Spree::Products::PrepareNestedAttributes do
- subject(:prepared_params) { described_class.new(product, store, params, ability).call }
+ let(:service) { described_class.new(product, store, params, ability) }
+ subject(:prepared_params) { service.call }
let(:ability) { Spree::Ability.new(nil) }
let(:store) { @default_store }
@@ -218,6 +219,138 @@ RSpec.describe Spree::Products::PrepareNestedAttributes do
end
end
+ describe 'variant removal handling' do
+ let(:option_type) { create(:option_type) }
+ let(:option_value) { create(:option_value, option_type: option_type) }
+ let(:variant) { create(:variant, product: product) }
+
+ context 'when variant has completed orders' do
+ before do
+ create(:completed_order_with_totals, variants: [variant])
+ end
+
+ context 'via variants_attributes' do
+ let(:new_option_value) { create(:option_value, option_type: option_type) }
+ let(:new_variant) { create(:variant, product: product) }
+ let(:raw_params) do
+ {
+ variants_attributes: {
+ '0' => {
+ id: new_variant.id.to_s,
+ options: [
+ {
+ id: option_type.id,
+ name: option_type.name,
+ position: '0',
+ option_value_name: new_option_value.name,
+ option_value_presentation: new_option_value.presentation
+ }
+ ]
+ }
+ }
+ }
+ end
+
+ it 'collects the variant for discontinuation' do
+ prepared_params
+ expect(service.variants_to_discontinue).to include(variant)
+ end
+
+ it 'does not include the variant in removal attributes' do
+ removed = prepared_params[:variants_attributes].values.find { |v| v[:id] == variant.id.to_s }
+ expect(removed).to be_nil
+ end
+ end
+
+ context 'via fallback removal (no variants_attributes)' do
+ let(:raw_params) { { name: 'Updated Product' } }
+
+ it 'collects the variant for discontinuation' do
+ prepared_params
+ expect(service.variants_to_discontinue).to include(variant)
+ end
+
+ it 'does not include the variant in destruction attributes' do
+ removed = prepared_params[:variants_attributes]&.values&.find { |v| v[:id] == variant.id.to_s }
+ expect(removed).to be_nil
+ end
+ end
+ end
+
+ context 'when variant has no completed orders' do
+ before { variant } # ensure variant exists before service runs
+
+ context 'via variants_attributes' do
+ let(:new_option_value) { create(:option_value, option_type: option_type) }
+ let(:new_variant) { create(:variant, product: product) }
+ let(:raw_params) do
+ {
+ variants_attributes: {
+ '0' => {
+ id: new_variant.id.to_s,
+ options: [
+ {
+ id: option_type.id,
+ name: option_type.name,
+ position: '0',
+ option_value_name: new_option_value.name,
+ option_value_presentation: new_option_value.presentation
+ }
+ ]
+ }
+ }
+ }
+ end
+
+ it 'marks the variant for destruction' do
+ removed = prepared_params[:variants_attributes].values.find { |v| v[:id] == variant.id.to_s }
+ expect(removed[:_destroy]).to eq('1')
+ end
+
+ it 'does not collect the variant for discontinuation' do
+ prepared_params
+ expect(service.variants_to_discontinue).not_to include(variant)
+ end
+ end
+
+ context 'via fallback removal (no variants_attributes)' do
+ let(:raw_params) { { name: 'Updated Product' } }
+
+ it 'marks the variant for destruction' do
+ removed = prepared_params[:variants_attributes].values.find { |v| v[:id] == variant.id.to_s }
+ expect(removed[:_destroy]).to eq('1')
+ end
+ end
+ end
+
+ context 'when product has mix of variants with and without completed orders' do
+ let(:variant_with_order) { create(:variant, product: product) }
+ let(:variant_without_order) { create(:variant, product: product) }
+
+ before do
+ variant_without_order # ensure variant exists before service runs
+ create(:completed_order_with_totals, variants: [variant_with_order])
+ end
+
+ let(:raw_params) { { name: 'Updated Product' } }
+
+ it 'collects the variant with completed orders for discontinuation' do
+ prepared_params
+ expect(service.variants_to_discontinue).to include(variant_with_order)
+ end
+
+ it 'does not include the collected variant in removal attributes' do
+ removed = prepared_params[:variants_attributes].values.find { |v| v[:id] == variant_with_order.id.to_s }
+ expect(removed).to be_nil
+ end
+
+ it 'marks the variant without completed orders for destruction' do
+ removed = prepared_params[:variants_attributes].values.find { |v| v[:id] == variant_without_order.id.to_s }
+ expect(removed[:_destroy]).to eq('1')
+ end
+ end
+ end
+
describe 'stock_items_attributes handling' do
let(:variant) { product.master }