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
Drop additional Store Credit <> Gift Card association, just use originator
Damian Legawiec62e26d295f21
diff --git a/admin/app/views/spree/admin/payments/_payment.html.erb b/admin/app/views/spree/admin/payments/_payment.html.erbindex 1e89e541b1..4e3829c13e 100644--- a/admin/app/views/spree/admin/payments/_payment.html.erb+++ b/admin/app/views/spree/admin/payments/_payment.html.erb@@ -24,8 +24,8 @@<% if payment.try(:gateway_dashboard_payment_url).present? %><%= link_to_with_icon 'eye', Spree.t(:details), payment.gateway_dashboard_payment_url, class: 'dropdown-item', target: '_blank' %><% end %>- <% if payment.source.is_a?(Spree::StoreCredit) && payment.source.respond_to?(:gift_card) && payment.source.gift_card.present? %>- <%= link_to_with_icon 'eye', Spree.t(:details), spree.admin_gift_card_path(payment.source.gift_card), class: 'dropdown-item' %>+ <% if payment.source.is_a?(Spree::StoreCredit) && payment.source.originator_type == 'Spree::GiftCard' %>+ <%= link_to_with_icon 'eye', Spree.t(:details), spree.admin_gift_card_path(payment.source.originator), class: 'dropdown-item' %><% end %><% if can?(:update, payment) && (payment.checkout? || payment.pending?) %>diff --git a/core/app/models/spree/gift_card.rb b/core/app/models/spree/gift_card.rbindex b01da1cc74..8819756b30 100644--- a/core/app/models/spree/gift_card.rb+++ b/core/app/models/spree/gift_card.rb@@ -47,7 +47,7 @@ module Spreebelongs_to :user, class_name: Spree.user_class.to_s, optional: truebelongs_to :batch, class_name: 'Spree::GiftCardBatch', optional: true, foreign_key: :gift_card_batch_id- has_many :store_credits, class_name: 'Spree::StoreCredit'+ has_many :store_credits, class_name: 'Spree::StoreCredit', as: :originatorhas_many :orders, inverse_of: :gift_card, class_name: 'Spree::Order'has_many :users, through: :orders, class_name: Spree.user_class.to_sdiff --git a/core/app/models/spree/order/gift_card.rb b/core/app/models/spree/order/gift_card.rbindex 9999fba8e3..4f8b749632 100644--- a/core/app/models/spree/order/gift_card.rb+++ b/core/app/models/spree/order/gift_card.rb@@ -14,7 +14,7 @@ module Spreereturn 0.to_d unless gift_card.present?store_credit_ids = payments.store_credits.valid.pluck(:source_id)- store_credits = Spree::StoreCredit.where(id: store_credit_ids, gift_card: gift_card)+ store_credits = Spree::StoreCredit.where(id: store_credit_ids, originator: gift_card)store_credits.sum(:amount)enddiff --git a/core/app/services/spree/gift_cards/apply.rb b/core/app/services/spree/gift_cards/apply.rbindex e0130e9b59..693895ea8a 100644--- a/core/app/services/spree/gift_cards/apply.rb+++ b/core/app/services/spree/gift_cards/apply.rb@@ -24,7 +24,8 @@ module Spreeuser: order.user,amount: amount,currency: order.currency,- expires_at: gift_card.expires_at+ originator: gift_card,+ action_originator: gift_card)gift_card.amount_remaining -= amountgift_card.save!diff --git a/core/app/views/spree/shared/_payment.html.erb b/core/app/views/spree/shared/_payment.html.erbindex f3e244e628..9b2667bbfe 100644--- a/core/app/views/spree/shared/_payment.html.erb+++ b/core/app/views/spree/shared/_payment.html.erb@@ -12,11 +12,11 @@<p class="mb-0"><%= Spree.t(:expiration) %> <%= source.month %>/<%= source.year %></p></div><% elsif source.is_a?(Spree::StoreCredit) %>- <% if source.respond_to?(:gift_card) && source.gift_card.present? %>- <div class="d-flex align-items-center text-decoration-none w-100 h-100">- <%= icon('gift', height: 30, class: 'mr-3 text-muted') %>+ <% if source.originator_type == 'Spree::GiftCard' %>+ <div class="d-flex align-items-center">+ <%= icon('gift', height: 30, class: 'mr-3 text-muted opacity-50') %><div>- <p class="mb-1"><%= Spree.t(:gift_card) %>: <strong><%= source.gift_card.code.upcase %></strong></p>+ <p class="mb-1"><%= Spree.t(:gift_card) %>: <strong><%= source.originator.code.upcase %></strong></p><%= @order.display_gift_card_total %></div></div>diff --git a/core/db/migrate/20250506073057_create_spree_gift_cards_and_spree_gift_card_batches.rb b/core/db/migrate/20250506073057_create_spree_gift_cards_and_spree_gift_card_batches.rbindex 7947f86a61..1a38b2ddaf 100644--- a/core/db/migrate/20250506073057_create_spree_gift_cards_and_spree_gift_card_batches.rb+++ b/core/db/migrate/20250506073057_create_spree_gift_cards_and_spree_gift_card_batches.rb@@ -28,11 +28,6 @@ class CreateSpreeGiftCardsAndSpreeGiftCardBatches < ActiveRecord::Migration[7.2]add_index :spree_gift_cards, :code, unique: { scope: :store_id }- add_column :spree_store_credits, :gift_card_id, :bigint, if_not_exists: true- add_column :spree_store_credits, :expires_at, :datetime, if_not_exists: true- add_column :spree_store_credits, :state, :integer, default: 0, if_not_exists: true- add_index :spree_store_credits, :gift_card_id, if_not_exists: true-add_column :spree_orders, :gift_card_id, :bigint, if_not_exists: trueadd_index :spree_orders, :gift_card_id, if_not_exists: trueenddiff --git a/core/spec/models/spree/order_merger_spec.rb b/core/spec/models/spree/order_merger_spec.rbindex e472386e3b..36aae2922e 100644--- a/core/spec/models/spree/order_merger_spec.rb+++ b/core/spec/models/spree/order_merger_spec.rb@@ -161,7 +161,7 @@ module Spreelet(:order_2) { create(:order, gift_card: gift_card, total: 20) }let(:gift_card) { create(:gift_card) }let(:store_credit_payment_method) { create(:store_credit_payment_method) }- let(:store_credit) { create(:store_credit, gift_card: gift_card, amount: 20) }+ let(:store_credit) { create(:store_credit, originator: gift_card, amount: 20) }let!(:payment) { create(:payment, order: order_2, payment_method: store_credit_payment_method, amount: 20, source: store_credit) }it 'merges the gift card' dodiff --git a/core/spec/services/spree/gift_cards/apply_spec.rb b/core/spec/services/spree/gift_cards/apply_spec.rbindex b449d41d49..577a618870 100644--- a/core/spec/services/spree/gift_cards/apply_spec.rb+++ b/core/spec/services/spree/gift_cards/apply_spec.rb@@ -32,7 +32,7 @@ RSpec.describe Spree::GiftCards::Apply doexpect(store_credit.amount).to eq(30)expect(store_credit.store).to eq(Spree::Store.default)- expect(store_credit.gift_card).to eq(gift_card)+ expect(store_credit.originator).to eq(gift_card)endcontext 'when the order has applied store credit' do@@ -51,7 +51,7 @@ RSpec.describe Spree::GiftCards::Apply doexpect(order.reload.gift_card).to eq(nil)expect(order.total_applied_store_credit).to eq(10)- expect(order.payments.store_credits.last.source.gift_card).to eq(nil)+ expect(order.payments.store_credits.last.source.originator).to eq(nil)endend