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.

Fixed CSV importer stock update issue (#13258)

Damian Legawiecd4c003e311c3
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index 9e32a350d2..5dcfd03b09 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -403,11 +403,10 @@ module Spree
def set_stock(count_on_hand, backorderable = nil, stock_location = nil)
stock_location ||= Spree::Store.current.default_stock_location
- stock_items.find_or_initialize_by(stock_location: stock_location) do |stock_item|
- stock_item.count_on_hand = count_on_hand
- stock_item.backorderable = backorderable if backorderable.present?
- stock_item.save!
- end
+ stock_item = stock_items.find_or_initialize_by(stock_location: stock_location)
+ stock_item.count_on_hand = count_on_hand
+ stock_item.backorderable = backorderable if backorderable.present?
+ stock_item.save!
end
def price_modifier_amount_in(currency, options = {})
diff --git a/core/spec/services/spree/imports/row_processors/product_variant_spec.rb b/core/spec/services/spree/imports/row_processors/product_variant_spec.rb
index d3fb11e20d..35c6831eac 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
@@ -51,6 +51,30 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do
expect(variant.stock_items.first.count_on_hand).to eq 100
expect(variant.stock_items.first.backorderable).to eq true
end
+
+ context 'when updating an existing master variant' do
+ let!(:existing_product) { create(:product, slug: 'denim-shirt', name: 'Old Name', stores: [store]) }
+
+ before do
+ stock_item = existing_product.master.stock_items.find_or_initialize_by(stock_location: store.default_stock_location)
+ stock_item.count_on_hand = 50
+ stock_item.backorderable = false
+ stock_item.save!
+ end
+
+ it 'updates inventory_count and inventory_backorderable' do
+ stock_item = existing_product.master.stock_items.find_by(stock_location: store.default_stock_location)
+ expect(stock_item.count_on_hand).to eq 50
+ expect(stock_item.backorderable).to eq false
+
+ subject.process!
+
+ stock_item.reload
+ expect(stock_item.count_on_hand).to eq 100
+ expect(stock_item.backorderable).to eq true
+ expect(existing_product.reload.name).to eq 'Denim Shirt'
+ end
+ end
end
context 'when importing a variant row with options' do
@@ -108,6 +132,27 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do
expect { subject.process! }.to change { variant.reload.price }.from(50.99).to(62.99)
expect(variant.option_values.map(&:presentation).sort).to contain_exactly('Blue', 'XS')
end
+
+ context 'when updating inventory values' do
+ before do
+ stock_item = variant.stock_items.find_or_initialize_by(stock_location: store.default_stock_location)
+ stock_item.count_on_hand = 50
+ stock_item.backorderable = false
+ stock_item.save!
+ end
+
+ it 'updates inventory_count and inventory_backorderable' do
+ stock_item = variant.stock_items.find_by(stock_location: store.default_stock_location)
+ expect(stock_item.count_on_hand).to eq 50
+ expect(stock_item.backorderable).to eq false
+
+ subject.process!
+
+ stock_item.reload
+ expect(stock_item.count_on_hand).to eq 100
+ expect(stock_item.backorderable).to eq true
+ end
+ end
end
end