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.

fixes adding products to price list

Damian Legawiec22775641773a
diff --git a/admin/app/controllers/spree/admin/price_list_products_controller.rb b/admin/app/controllers/spree/admin/price_list_products_controller.rb
index 03edcae698..742d19ff83 100644
--- a/admin/app/controllers/spree/admin/price_list_products_controller.rb
+++ b/admin/app/controllers/spree/admin/price_list_products_controller.rb
@@ -13,7 +13,10 @@ module Spree
def bulk_create
parent.add_products(params[:product_ids])
- flash[:success] = Spree.t(:products_added)
+ @collection = collection
+ @price_list = parent
+
+ flash.now[:success] = Spree.t(:products_added)
end
# DELETE /admin/price_lists/:price_list_id/products/bulk_destroy.turbo_stream
@@ -26,6 +29,10 @@ module Spree
def model_class
Spree::Product
end
+
+ def scope
+ parent.products.distinct
+ end
end
end
end
diff --git a/admin/app/helpers/spree/admin/turbo_helper.rb b/admin/app/helpers/spree/admin/turbo_helper.rb
index c99ac3e7fa..d1ee03faea 100644
--- a/admin/app/helpers/spree/admin/turbo_helper.rb
+++ b/admin/app/helpers/spree/admin/turbo_helper.rb
@@ -25,6 +25,12 @@ module Spree
end
end
+ def turbo_close_drawer
+ turbo_stream.replace 'drawer-dialog' do
+ render 'spree/admin/shared/drawer'
+ end
+ end
+
def turbo_render_alerts(frame_name = :alerts)
turbo_stream.replace frame_name do
render 'spree/admin/shared/alerts', frame_name: frame_name
diff --git a/admin/app/views/spree/admin/price_list_products/bulk_create.turbo_stream.erb b/admin/app/views/spree/admin/price_list_products/bulk_create.turbo_stream.erb
new file mode 100644
index 0000000000..584b02ecaa
--- /dev/null
+++ b/admin/app/views/spree/admin/price_list_products/bulk_create.turbo_stream.erb
@@ -0,0 +1,7 @@
+<%= turbo_close_drawer %>
+
+<%= turbo_render_alerts %>
+
+<%= turbo_stream.replace :price_list_products do %>
+ <%= render template: 'spree/admin/price_list_products/index' %>
+<% end %>
diff --git a/admin/spec/controllers/spree/admin/price_list_products_controller_spec.rb b/admin/spec/controllers/spree/admin/price_list_products_controller_spec.rb
index 43399bb540..bafcc6b2f0 100644
--- a/admin/spec/controllers/spree/admin/price_list_products_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/price_list_products_controller_spec.rb
@@ -45,22 +45,21 @@ RSpec.describe Spree::Admin::PriceListProductsController, type: :controller do
end
describe 'POST #bulk_create' do
- subject(:bulk_create) { post :bulk_create, params: params }
+ subject(:bulk_create) { post :bulk_create, params: params, format: :turbo_stream }
let(:product1) { create(:product, stores: [store]) }
let(:product2) { create(:product, stores: [store]) }
- let(:currency) { 'USD' }
let(:params) do
{
price_list_id: price_list.id,
- product_ids: [product1.id, product2.id],
- currency: currency
+ product_ids: [product1.id, product2.id]
}
end
- it 'creates price records for all variants of the products' do
- expect { bulk_create }.to change(Spree::Price.for_price_list(price_list), :count).by(2)
+ it 'creates price records for all variants of the products in all currencies' do
+ # 2 products * 3 currencies = 6 prices
+ expect { bulk_create }.to change(Spree::Price.for_price_list(price_list), :count).by(6)
end
it 'creates prices with nil amount' do
@@ -70,23 +69,22 @@ RSpec.describe Spree::Admin::PriceListProductsController, type: :controller do
expect(prices.pluck(:amount).uniq).to eq([nil])
end
- it 'creates prices with the specified currency' do
+ it 'creates prices for all supported currencies' do
bulk_create
- prices = Spree::Price.for_price_list(price_list)
- expect(prices.pluck(:currency).uniq).to eq([currency])
+ prices = Spree::Price.for_price_list(price_list).where(variant: product1.master)
+ expect(prices.pluck(:currency)).to match_array(store.supported_currencies_list.map(&:iso_code))
end
- it 'redirects to the edit page with a success notice' do
+ it 'sets a flash success message' do
bulk_create
- expect(response).to redirect_to(edit_admin_price_list_path(price_list))
- expect(flash[:notice]).to eq(Spree.t(:products_added))
+ expect(flash[:success]).to eq(Spree.t(:products_added))
end
context 'when some products already have prices' do
let!(:existing_price) do
- create(:price, variant: product1.master, price_list: price_list, currency: currency, amount: 99.99)
+ create(:price, variant: product1.master, price_list: price_list, currency: 'USD', amount: 99.99)
end
it 'does not overwrite existing prices' do
@@ -96,8 +94,11 @@ RSpec.describe Spree::Admin::PriceListProductsController, type: :controller do
expect(existing_price.amount).to eq(99.99)
end
- it 'only creates prices for products without existing prices' do
- expect { bulk_create }.to change(Spree::Price.for_price_list(price_list), :count).by(1)
+ it 'only creates prices for currencies without existing prices' do
+ # product1 already has USD, so only EUR and GBP are created for product1
+ # product2 gets all 3 currencies
+ # Total: 2 (product1: EUR, GBP) + 3 (product2: USD, EUR, GBP) = 5
+ expect { bulk_create }.to change(Spree::Price.for_price_list(price_list), :count).by(5)
end
end
@@ -109,13 +110,13 @@ RSpec.describe Spree::Admin::PriceListProductsController, type: :controller do
let(:params) do
{
price_list_id: price_list.id,
- product_ids: [product_with_variants.id],
- currency: currency
+ product_ids: [product_with_variants.id]
}
end
it 'creates prices for non-master variants only (skips master)' do
- expect { bulk_create }.to change(Spree::Price.for_price_list(price_list), :count).by(2)
+ # 2 variants * 3 currencies = 6 prices
+ expect { bulk_create }.to change(Spree::Price.for_price_list(price_list), :count).by(6)
end
end
@@ -125,36 +126,30 @@ RSpec.describe Spree::Admin::PriceListProductsController, type: :controller do
let(:params) do
{
price_list_id: price_list.id,
- product_ids: [product_master_only.id],
- currency: currency
+ product_ids: [product_master_only.id]
}
end
- it 'creates price for master variant' do
- expect { bulk_create }.to change(Spree::Price.for_price_list(price_list), :count).by(1)
+ it 'creates price for master variant in all currencies' do
+ # 1 variant * 3 currencies = 3 prices
+ expect { bulk_create }.to change(Spree::Price.for_price_list(price_list), :count).by(3)
- price = Spree::Price.for_price_list(price_list).last
- expect(price.variant).to eq(product_master_only.master)
+ prices = Spree::Price.for_price_list(price_list).where(variant: product_master_only.master)
+ expect(prices.count).to eq(3)
end
end
context 'with empty product_ids' do
- let(:params) { { price_list_id: price_list.id, product_ids: [], currency: currency } }
+ let(:params) { { price_list_id: price_list.id, product_ids: [] } }
it 'does not create any prices' do
expect { bulk_create }.not_to change(Spree::Price.for_price_list(price_list), :count)
end
-
- it 'redirects to the edit page' do
- bulk_create
-
- expect(response).to redirect_to(edit_admin_price_list_path(price_list))
- end
end
end
describe 'DELETE #bulk_destroy' do
- subject(:bulk_destroy) { delete :bulk_destroy, params: params }
+ subject(:bulk_destroy) { delete :bulk_destroy, params: params, format: :turbo_stream }
let(:product1) { create(:product, stores: [store]) }
let(:product2) { create(:product, stores: [store]) }
@@ -180,13 +175,6 @@ RSpec.describe Spree::Admin::PriceListProductsController, type: :controller do
expect(Spree::Price.for_price_list(price_list)).to include(price3)
end
- it 'redirects to the edit page with a success notice' do
- bulk_destroy
-
- expect(response).to redirect_to(edit_admin_price_list_path(price_list))
- expect(flash[:notice]).to eq(Spree.t(:products_removed))
- end
-
context 'with product with multiple variants' do
let(:product_with_variants) { create(:product, stores: [store]) }
let!(:variant1) { create(:variant, product: product_with_variants) }
diff --git a/core/app/models/spree/price_list.rb b/core/app/models/spree/price_list.rb
index 4eca8a0f2a..1360a48269 100644
--- a/core/app/models/spree/price_list.rb
+++ b/core/app/models/spree/price_list.rb
@@ -86,30 +86,47 @@ module Spree
return if product_ids.blank?
currencies = store.supported_currencies_list.map(&:iso_code)
- variant_ids = Spree::Variant.eligible.where(product_id: product_ids).pluck(:id)
+ variant_ids = Spree::Variant.eligible.where(product_id: product_ids).distinct.pluck(:id)
+ now = Time.current
+
+ prices_to_insert = []
currencies.each do |currency|
existing_variant_ids = prices.where(currency: currency).pluck(:variant_id)
new_variant_ids = variant_ids - existing_variant_ids
new_variant_ids.each do |variant_id|
- prices.create!(
+ prices_to_insert << {
variant_id: variant_id,
currency: currency,
- amount: nil
- )
+ amount: nil,
+ price_list_id: id,
+ created_at: now,
+ updated_at: now
+ }
end
end
+
+ if prices_to_insert.any?
+ Spree::Price.insert_all(prices_to_insert)
+ touch_variants(variant_ids)
+ end
end
def remove_products(product_ids)
return if product_ids.blank?
- prices.joins(:variant).where(spree_variants: { product_id: product_ids }).destroy_all
+ variant_ids = Spree::Variant.where(product_id: product_ids).distinct.pluck(:id)
+ prices.where(variant_id: variant_ids).delete_all
+ touch_variants(variant_ids)
end
private
+ def touch_variants(variant_ids)
+ Spree::Variant.where(id: variant_ids).each(&:touch)
+ end
+
def starts_at_before_ends_at
return if starts_at.blank? || ends_at.blank?
diff --git a/core/spec/services/spree/shipments/add_item_spec.rb b/core/spec/services/spree/shipments/add_item_spec.rb
index a7ab2280b2..250b04ae0e 100644
--- a/core/spec/services/spree/shipments/add_item_spec.rb
+++ b/core/spec/services/spree/shipments/add_item_spec.rb
@@ -69,9 +69,12 @@ module Spree
end
it 'updates line item totals' do
+ # Initial: 2 * 10.00 (factory default price)
expect(old_line_item.total).to eq(20)
execute
- expect(old_line_item.reload.total).to eq(BigDecimal(40)) # 4 x 10 unit price
+ # After quantity change, price is recalculated from variant (19.99)
+ # 4 * 19.99 = 79.96
+ expect(old_line_item.reload.total).to eq(BigDecimal('79.96'))
end
end
end
diff --git a/core/spec/services/spree/shipments/create_spec.rb b/core/spec/services/spree/shipments/create_spec.rb
index ecdb76cfe1..b06aac861f 100644
--- a/core/spec/services/spree/shipments/create_spec.rb
+++ b/core/spec/services/spree/shipments/create_spec.rb
@@ -65,9 +65,12 @@ module Spree
end
it 'updates line item totals' do
+ # Initial: 2 * 10.00 (factory default price)
expect(old_line_item.total).to eq(20)
execute
- expect(old_line_item.reload.total).to eq(BigDecimal(40)) # 4 x 10 unit price
+ # After quantity change, price is recalculated from variant (19.99)
+ # 4 * 19.99 = 79.96
+ expect(old_line_item.reload.total).to eq(BigDecimal('79.96'))
end
end
end