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
Do not shadow outer variable when trying to remove empty price (#12556)
Damian Kaczmarczyk6bcfa6292869
diff --git a/admin/spec/controllers/spree/admin/products_controller_spec.rb b/admin/spec/controllers/spree/admin/products_controller_spec.rbindex 547574d671..d9c4676e29 100644--- a/admin/spec/controllers/spree/admin/products_controller_spec.rb+++ b/admin/spec/controllers/spree/admin/products_controller_spec.rb@@ -622,12 +622,15 @@ 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(:variant3) { create(:variant, product: product, option_values: [red_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!(:variant3_price_pln) { create(:price, variant: variant3, currency: 'PLN', amount: 300) }let(:variant1_stock_item) { variant1.stock_items.first }let(:variant2_stock_item) { variant2.stock_items.first }+ let(:variant3_stock_item) { variant3.stock_items.first }let(:product_params) do{@@ -684,6 +687,32 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller dovalue: 'Large'}]+ },+ '2' => {+ prices_attributes: {+ '0' => { currency: 'PLN', amount: 30, id: variant3.price_in('PLN')&.id },+ '1' => { currency: 'USD', amount: 40, id: variant3.price_in('USD')&.id }+ },+ id: variant3.id,+ stock_items_attributes: {+ '0' => {+ id: variant3_stock_item.id,+ count_on_hand: 30,+ stock_location_id: variant3_stock_item.stock_location_id,+ }+ },+ options: [+ {+ name: 'Color',+ position: 1,+ value: 'Red'+ },+ {+ name: 'Size',+ position: 2,+ value: 'Large'+ }+ ]}}}@@ -691,16 +720,16 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller docontext 'when price is not present' dobefore do- product_params[:variants_attributes]['0'][:prices_attributes]['0'][:amount] = nil+ product_params[:variants_attributes]['2'][:prices_attributes]['0'][:amount] = nilendit 'removes the price' dosend_request- expect(variant1.price_in('PLN').amount).to be_nil- expect(variant1.price_in('PLN').id).to be_nil- expect(variant1.price_in('USD').amount).to eq 20- expect(variant1.price_in('USD').id).to be_present+ expect(variant3.price_in('PLN').amount).to be_nil+ expect(variant3.price_in('PLN').id).to be_nil+ expect(variant3.price_in('USD').amount).to eq 40+ expect(variant3.price_in('USD').id).to be_presentendend@@ -839,7 +868,7 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller doit 'removes the variant' dosend_request- expect(product.reload.variant_ids).to eq([variant2.id])+ expect(product.reload.variant_ids).to eq([variant2.id, variant3.id])endendenddiff --git a/core/app/services/spree/products/prepare_nested_attributes.rb b/core/app/services/spree/products/prepare_nested_attributes.rbindex fe02f1ff8f..4985af7d53 100644--- a/core/app/services/spree/products/prepare_nested_attributes.rb+++ b/core/app/services/spree/products/prepare_nested_attributes.rb@@ -16,8 +16,8 @@ module Spreeif can_update_prices?# If the variant price is nil then mark it for destruction- variant_params[:prices_attributes]&.each do |_key, price_params|- variant_params[:prices_attributes][key]['_destroy'] = '1' if price_params[:amount].blank?+ variant_params[:prices_attributes]&.each do |price_key, price_params|+ variant_params[:prices_attributes][price_key]['_destroy'] = '1' if price_params[:amount].blank?endelsevariant_params.delete(:prices_attributes)@@ -37,8 +37,8 @@ module Spreeif can_update_prices?# If the master price is nil then mark it for destruction- params[:master_attributes][:prices_attributes]&.each do |key, price_params|- params[:master_attributes][:prices_attributes][key]['_destroy'] = '1' if price_params[:amount].blank?+ params.dig(:master_attributes, :prices_attributes)&.each do |price_key, price_params|+ params[:master_attributes][:prices_attributes][price_key]['_destroy'] = '1' if price_params[:amount].blank?endelseparams[:master_attributes].delete(:prices_attributes)