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.

fix issues with updating variants

Łukasz Adamczyk8aba70f72f8d
diff --git a/admin/app/views/spree/admin/products/form/variants/_variant_template.html.erb b/admin/app/views/spree/admin/products/form/variants/_variant_template.html.erb
index 6dd8a1e84d..2eae48acc0 100644
--- a/admin/app/views/spree/admin/products/form/variants/_variant_template.html.erb
+++ b/admin/app/views/spree/admin/products/form/variants/_variant_template.html.erb
@@ -27,7 +27,7 @@
</div>
<div class="variants-table__body__cell column-quantity">
<% available_stock_locations_for_product(@product).ids.each_with_index do |id, i| %>
- <%= number_field_tag "product[varaints_attributes][stock_items_attributes][#{i}][count_on_hand]", 0, class: class_names('form-control', default_stock_location.id == id ? 'd-block' : 'd-none'), data: {slot: "[stock_items_attributes][#{id}][count_on_hand]_input", stock_location_id: id, action: "variants-form#updateCountOnHand blur->variants-form#replaceBlankWithZero" }, readonly: !can_manage_stock %>
+ <%= number_field_tag "product[variants_attributes][stock_items_attributes][#{i}][count_on_hand]", 0, class: class_names('form-control', default_stock_location.id == id ? 'd-block' : 'd-none'), data: {slot: "[stock_items_attributes][#{id}][count_on_hand]_input", stock_location_id: id, action: "variants-form#updateCountOnHand blur->variants-form#replaceBlankWithZero" }, readonly: !can_manage_stock %>
<%= hidden_field_tag "product[variants_attributes][stock_items_attributes][#{i}][stock_location_id]", id, data: { slot: "[stock_items_attributes][#{id}][stock_location_id]_input" } %>
<%= hidden_field_tag "product[variants_attributes][stock_items_attributes][#{i}][id]", "", data: { slot: "[stock_items_attributes][#{id}][id]_input" } %>
<% end %>
diff --git a/admin/spec/controllers/spree/admin/products_controller_spec.rb b/admin/spec/controllers/spree/admin/products_controller_spec.rb
index 152ef6ae10..eab56513bb 100644
--- a/admin/spec/controllers/spree/admin/products_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/products_controller_spec.rb
@@ -582,6 +582,9 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
let(:variant1) { create(:variant, product: product, option_values: [red_option_value, small_option_value], price: 100) }
let(:variant2) { create(:variant, product: product, option_values: [blue_option_value, large_option_value], price: 100) }
+ let!(:variant1_price_pln) { create(:price, variant: variant1, currency: 'PLN', amount: 100) }
+ let!(:variant2_price_pln) { create(:price, variant: variant2, currency: 'PLN', amount: 200) }
+
let(:variant1_stock_item) { variant1.stock_items.first }
let(:variant2_stock_item) { variant2.stock_items.first }
@@ -786,7 +789,21 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
end
context 'setting track_inventory to false' do
- let(:product_params) { { track_inventory: false } }
+ let(:product_params) do
+ {
+ track_inventory: '0',
+ master_attributes: {
+ id: product.master.id,
+ stock_items_attributes: {
+ '0' => {
+ id: product.master.stock_items.first.id,
+ stock_location_id: product.master.stock_items.first.stock_location_id,
+ count_on_hand: 100
+ }
+ }
+ }
+ }
+ end
before do
product.update(track_inventory: true)
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index b77f285bab..a0857f608a 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -67,6 +67,8 @@ module Spree
after_create :create_stock_items
after_create :set_master_out_of_stock, unless: :is_master?
after_commit :clear_line_items_cache, on: :update
+ after_update_commit :handle_track_inventory_change
+
after_touch :clear_in_stock_cache
scope :in_stock, -> { joins(:stock_items).where("#{Spree::StockItem.table_name}.count_on_hand > ? OR #{Spree::Variant.table_name}.track_inventory = ?", 0, false) }
@@ -529,5 +531,12 @@ module Spree
def clear_line_items_cache
line_items.update_all(updated_at: Time.current)
end
+
+ def handle_track_inventory_change
+ return unless track_inventory_previously_changed?
+ return if track_inventory
+
+ stock_items.delete_all
+ end
end
end
diff --git a/core/app/services/spree/products/prepare_nested_attributes.rb b/core/app/services/spree/products/prepare_nested_attributes.rb
index cb884af43c..843feeab82 100644
--- a/core/app/services/spree/products/prepare_nested_attributes.rb
+++ b/core/app/services/spree/products/prepare_nested_attributes.rb
@@ -64,7 +64,7 @@ module Spree
end
def variants_to_remove
- @variants_to_remove ||= product.variant_ids
+ @variants_to_remove ||= product.variant_ids.map(&:to_s)
end
def can_update_prices?