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 `CreateLineItems` Promotion action

Damian Legawieccf642da9fc5e
diff --git a/admin/app/views/spree/admin/promotion_actions/_promotion_action.html.erb b/admin/app/views/spree/admin/promotion_actions/_promotion_action.html.erb
index 7fb82ceb03..8d8268c6fb 100644
--- a/admin/app/views/spree/admin/promotion_actions/_promotion_action.html.erb
+++ b/admin/app/views/spree/admin/promotion_actions/_promotion_action.html.erb
@@ -24,6 +24,20 @@
<h6><%= Spree.t(:calculator) %>: <%= promotion_action.calculator.description %></h6>
<%= render 'spree/admin/shared/preferences', object: promotion_action.calculator %>
<% end %>
+
+ <% if promotion_action.respond_to?(:promotion_action_line_items) %>
+ <h6><%= Spree.t(:line_items) %></h6>
+ <ul class="list-group">
+ <% promotion_action.promotion_action_line_items.each do |line_item| %>
+ <li class="list-group-item d-flex justify-content-between align-items-center">
+ <%= link_to line_item.variant.descriptive_name, spree.edit_admin_product_variant_path(line_item.variant.product, line_item.variant) %>
+ <span>
+ <%= Spree.t(:quantity) %>: <%= line_item.quantity %>
+ </span>
+ </li>
+ <% end %>
+ </ul>
+ <% end %>
</div>
</div>
<% end %>
diff --git a/admin/app/views/spree/admin/promotion_actions/forms/_create_line_items.html.erb b/admin/app/views/spree/admin/promotion_actions/forms/_create_line_items.html.erb
new file mode 100644
index 0000000000..4ebe678298
--- /dev/null
+++ b/admin/app/views/spree/admin/promotion_actions/forms/_create_line_items.html.erb
@@ -0,0 +1,35 @@
+<% line_items = f.object.promotion_action_line_items.includes(variant: :product) %>
+
+<div data-controller="nested-form">
+ <template data-nested-form-target="template">
+ <%= f.fields_for :promotion_action_line_items, line_items.new, child_index: 'NEW_RECORD' do |line_item_fields| %>
+ <%= render 'spree/admin/promotion_actions/forms/create_line_items_row', line_item_fields: line_item_fields %>
+ <% end %>
+ </template>
+
+ <div class="rounded-lg border">
+ <table class="table">
+ <thead>
+ <tr>
+ <th><%= Spree.t(:variant) %></th>
+ <th><%= Spree.t(:quantity) %></th>
+ <th></th>
+ </tr>
+ </thead>
+ <tbody>
+ <%= f.fields_for :promotion_action_line_items do |line_item_fields| %>
+ <%= render 'spree/admin/promotion_actions/forms/create_line_items_row', line_item_fields: line_item_fields %>
+ <% end %>
+ <!-- Inserted elements will be injected before that target. -->
+ <tr data-nested-form-target="target">
+ <td colspan="3">
+ <button type="button" class="btn btn-link" data-action="nested-form#add">
+ <%= icon('plus', class: 'mr-2') %>
+ <%= Spree.t(:add_one)%>
+ </button>
+ </td>
+ </tr>
+ </tbody>
+ </table>
+ </div>
+</div>
diff --git a/admin/app/views/spree/admin/promotion_actions/forms/_create_line_items_row.html.erb b/admin/app/views/spree/admin/promotion_actions/forms/_create_line_items_row.html.erb
new file mode 100644
index 0000000000..3bdf0d4474
--- /dev/null
+++ b/admin/app/views/spree/admin/promotion_actions/forms/_create_line_items_row.html.erb
@@ -0,0 +1,28 @@
+<tr class="nested-form-wrapper" data-new-record="<%= line_item_fields.object.new_record? %>">
+ <%= line_item_fields.hidden_field :id %>
+
+ <td class="w-70">
+ <% if line_item_fields.object.new_record? %>
+ <%= tom_select_tag "#{line_item_fields.object_name}[variant_id]",
+ url: spree.admin_variants_search_path,
+ remote_search: true,
+ class: 'w-100',
+ required: true %>
+ <% else %>
+ <%= link_to spree.edit_admin_product_variant_path(line_item_fields.object.variant.product, line_item_fields.object.variant), target: '_blank' do %>
+ <%= line_item_fields.object.variant.descriptive_name %>
+ <% end %>
+ <%= line_item_fields.hidden_field :variant_id %>
+ <%= line_item_fields.hidden_field :promotion_action_id %>
+ <% end %>
+ </td>
+ <td class="w-20">
+ <%= line_item_fields.number_field :quantity, min: 1, max: line_item_fields.object.variant&.total_on_hand, class: 'form-control', required: true %>
+ </td>
+ <td class="w-10 text-right">
+ <button type="button" class="btn btn-danger btn-sm" data-action="nested-form#remove">
+ <%= icon('trash', class: 'mr-0') %>
+ </button>
+ <%= line_item_fields.hidden_field :_destroy %>
+ </td>
+</tr>
\ No newline at end of file
diff --git a/admin/app/views/spree/admin/promotions/actions/_create_line_items.html.erb b/admin/app/views/spree/admin/promotions/actions/_create_line_items.html.erb
deleted file mode 100644
index ca85765b70..0000000000
--- a/admin/app/views/spree/admin/promotions/actions/_create_line_items.html.erb
+++ /dev/null
@@ -1,27 +0,0 @@
-<div class="card-body calculator-fields">
- <% promotion_action.promotion_action_line_items.each do |item| %>
- <% variant = item.variant %>
-
- <strong><%= item.quantity %> x <%= variant.name %></strong>
- <%= variant.options_text %>
- <% end %>
-
- <% if promotion_action.promotion_action_line_items.empty? %>
- <% line_items = promotion_action.promotion_action_line_items %>
- <% line_items.build %>
-
- <% line_items.each_with_index do |line_item, index| %>
- <div class="add-line-item">
- <% line_item_prefix = "#{param_prefix}[promotion_action_line_items_attributes][#{index}]" %>
- <div class="form-group">
- <%= label_tag "#{line_item_prefix}_variant_id", Spree.t(:variant) %>
- <%= select_tag "#{line_item_prefix}[variant_id]", line_item.variant_id, class: 'variant_autocomplete d-block w-100' %>
- </div>
- <div class="form-group mb-0">
- <%= label_tag "#{line_item_prefix}_quantity", Spree.t(:quantity) %>
- <%= number_field_tag "#{line_item_prefix}[quantity]", line_item.quantity, min: 1, class: 'form-control' %>
- </div>
- </div>
- <% end %>
- <% end %>
-</div>
diff --git a/core/app/models/spree/promotion/actions/create_line_items.rb b/core/app/models/spree/promotion/actions/create_line_items.rb
index 56dbcee6d9..ef7644cf40 100644
--- a/core/app/models/spree/promotion/actions/create_line_items.rb
+++ b/core/app/models/spree/promotion/actions/create_line_items.rb
@@ -2,8 +2,11 @@ module Spree
class Promotion
module Actions
class CreateLineItems < PromotionAction
- has_many :promotion_action_line_items, foreign_key: :promotion_action_id
- accepts_nested_attributes_for :promotion_action_line_items
+ has_many :promotion_action_line_items, foreign_key: :promotion_action_id, dependent: :destroy
+
+ attribute :promotion_action_line_items_attributes
+
+ after_save :handle_promotion_action_line_items
delegate :eligible?, to: :promotion
@@ -79,6 +82,35 @@ module Spree
quantifier = Spree::Stock::Quantifier.new(item.variant)
quantifier.can_supply? item.quantity
end
+
+ private
+
+ # Handles the creation and updating of promotion action line items
+ #
+ # This is a hacky replacement for accepts_nested_attributes_for
+ # that allows us to save the PromotionAction and PromotionActionLineItems
+ # at the same time.
+ def handle_promotion_action_line_items
+ return unless promotion_action_line_items_attributes
+
+ # remove the ones marked for destruction
+ ids_for_destruction = promotion_action_line_items_attributes.map { |key, params| params["_destroy"] == "1" ? params["id"] : nil }.compact
+ promotion_action_line_items.where(id: ids_for_destruction).delete_all if ids_for_destruction.present?
+
+ # upsert the rest
+ records_for_upsert = promotion_action_line_items_attributes.map { |key, params| params["_destroy"] != "1" ? params : nil }.compact
+
+ promotion_action_line_items.upsert_all(
+ records_for_upsert.map do |params|
+ {
+ variant_id: params["variant_id"],
+ quantity: params["quantity"],
+ promotion_action_id: id
+ }
+ end,
+ unique_by: %i[promotion_action_id variant_id]
+ )
+ end
end
end
end
diff --git a/core/db/migrate/20250313175830_add_unique_index_on_promotion_action_line_items.rb b/core/db/migrate/20250313175830_add_unique_index_on_promotion_action_line_items.rb
new file mode 100644
index 0000000000..126b70c5f4
--- /dev/null
+++ b/core/db/migrate/20250313175830_add_unique_index_on_promotion_action_line_items.rb
@@ -0,0 +1,15 @@
+class AddUniqueIndexOnPromotionActionLineItems < ActiveRecord::Migration[7.2]
+ def change
+ # Remove duplicate promotion action line items
+ execute <<-SQL
+ DELETE FROM spree_promotion_action_line_items
+ WHERE id NOT IN (
+ SELECT MIN(id)
+ FROM spree_promotion_action_line_items
+ GROUP BY promotion_action_id, variant_id
+ );
+ SQL
+
+ add_index :spree_promotion_action_line_items, [:promotion_action_id, :variant_id], unique: true
+ end
+end
diff --git a/core/spec/models/spree/promotion/actions/create_line_items_spec.rb b/core/spec/models/spree/promotion/actions/create_line_items_spec.rb
index 326bfc4042..4153475298 100644
--- a/core/spec/models/spree/promotion/actions/create_line_items_spec.rb
+++ b/core/spec/models/spree/promotion/actions/create_line_items_spec.rb
@@ -82,4 +82,55 @@ describe Spree::Promotion::Actions::CreateLineItems, type: :model do
expect(action.item_available?(item_in_stock)).to be true
end
end
+
+ describe '#handle_promotion_action_line_items' do
+ let(:promotion_action_line_items_attributes) do
+ {
+ '0' => { 'variant_id' => shirt.id, 'quantity' => 1 },
+ '1' => { 'variant_id' => mug.id, 'quantity' => 2 }
+ }
+ end
+
+ before do
+ action.promotion_action_line_items_attributes = promotion_action_line_items_attributes
+ end
+
+ it 'creates new promotion action line items' do
+ expect { action.save! }.to change(action.promotion_action_line_items, :count).by(2)
+
+ expect(action.promotion_action_line_items.find_by(variant_id: shirt.id).quantity).to eq(1)
+ expect(action.promotion_action_line_items.find_by(variant_id: mug.id).quantity).to eq(2)
+ end
+
+ context 'with existing promotion action line items' do
+ before do
+ action.save!
+ # Change quantity for existing item
+ action.promotion_action_line_items_attributes = {
+ '0' => { 'variant_id' => shirt.id, 'quantity' => 3 }
+ }
+ end
+
+ it 'updates existing promotion action line items' do
+ expect { action.save! }.not_to change(action.promotion_action_line_items, :count)
+ expect(action.promotion_action_line_items.find_by(variant_id: shirt.id).quantity).to eq(3)
+ expect(action.promotion_action_line_items.find_by(variant_id: mug.id).quantity).to eq(2)
+ end
+ end
+
+ context 'with items marked for destruction' do
+ before do
+ action.save!
+ action.promotion_action_line_items_attributes = {
+ '0' => { 'id' => action.promotion_action_line_items.find_by(variant_id: shirt.id).id, '_destroy' => '1' }
+ }
+ end
+
+ it 'removes items marked for destruction' do
+ expect { action.save! }.to change(action.promotion_action_line_items, :count).by(-1)
+ expect(action.promotion_action_line_items.find_by(variant_id: shirt.id)).to be_nil
+ expect(action.promotion_action_line_items.find_by(variant_id: mug.id)).to be_present
+ end
+ end
+ end
end