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
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.erbindex 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.rbindex 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 dolet(: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 doendcontext '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+ }+ }+ }+ }+ endbefore doproduct.update(track_inventory: true)diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex b77f285bab..a0857f608a 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -67,6 +67,8 @@ module Spreeafter_create :create_stock_itemsafter_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_cachescope :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 Spreedef clear_line_items_cacheline_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+ endendenddiff --git a/core/app/services/spree/products/prepare_nested_attributes.rb b/core/app/services/spree/products/prepare_nested_attributes.rbindex 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 Spreeenddef variants_to_remove- @variants_to_remove ||= product.variant_ids+ @variants_to_remove ||= product.variant_ids.map(&:to_s)enddef can_update_prices?