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.

add `currency` to gift cards

Damian Legawiecd3041898b466
diff --git a/admin/app/views/spree/admin/gift_cards/_gift_card.html.erb b/admin/app/views/spree/admin/gift_cards/_gift_card.html.erb
index 64ce6b1332..4316820e8f 100644
--- a/admin/app/views/spree/admin/gift_cards/_gift_card.html.erb
+++ b/admin/app/views/spree/admin/gift_cards/_gift_card.html.erb
@@ -6,6 +6,7 @@
</td>
<td class="w-10 cursor-pointer" data-action="click->row-link#openLink"><%= gift_card.display_amount %></td>
<td class="w-10 cursor-pointer" data-action="click->row-link#openLink"><%= gift_card.display_used_amount %></td>
+ <td class="w-10 cursor-pointer" data-action="click->row-link#openLink"><%= gift_card.currency.upcase %></td>
<td class="w-10 cursor-pointer" data-action="click->row-link#openLink"><%= active_badge(gift_card.active?, label: gift_card.display_state) %></td>
<td class="w-10 cursor-pointer" data-action="click->row-link#openLink">
<% if gift_card.expires_at.present? %>
diff --git a/admin/app/views/spree/admin/gift_cards/_list.html.erb b/admin/app/views/spree/admin/gift_cards/_list.html.erb
index 73655b4ae2..9e1a2ccf45 100644
--- a/admin/app/views/spree/admin/gift_cards/_list.html.erb
+++ b/admin/app/views/spree/admin/gift_cards/_list.html.erb
@@ -6,6 +6,7 @@
<th><%= Spree.t(:code) %></th>
<th><%= Spree.t(:amount) %></th>
<th><%= Spree.t(:used) %></th>
+ <th><%= Spree.t(:currency) %></th>
<th><%= Spree.t(:status) %></th>
<th><%= Spree.t(:expires_at) %></th>
<th><%= Spree.t('admin.gift_cards.issued_to') %></th>
diff --git a/core/app/models/spree/gift_card.rb b/core/app/models/spree/gift_card.rb
index d25aea3248..dd7efee9e8 100644
--- a/core/app/models/spree/gift_card.rb
+++ b/core/app/models/spree/gift_card.rb
@@ -26,6 +26,7 @@ module Spree
# Validations
#
validates :code, presence: true, uniqueness: { scope: :store_id }
+ validates :store, :currency, presence: true
validates :amount, presence: true, numericality: { greater_than: 0 }
validates :amount_remaining, presence: true, numericality: { greater_than_or_equal_to: 0 }
validates :expires_at, comparison: { greater_than: Date.current + 1.day }, allow_nil: true, unless: :skip_expires_at_validation
@@ -62,6 +63,7 @@ module Spree
before_validation :generate_code
before_validation :normalize_code
before_validation :set_amount_remaining
+ before_validation :set_currency
before_destroy :ensure_can_be_deleted
#
@@ -182,5 +184,9 @@ module Spree
errors.add(:base, :cannot_destroy_used_gift_card)
throw(:abort)
end
+
+ def set_currency
+ self.currency ||= store.default_currency
+ end
end
end
diff --git a/core/app/models/spree/gift_card_batch.rb b/core/app/models/spree/gift_card_batch.rb
index 488324e09e..8515e1d6dc 100644
--- a/core/app/models/spree/gift_card_batch.rb
+++ b/core/app/models/spree/gift_card_batch.rb
@@ -8,6 +8,7 @@ module Spree
#
validates :codes_count, :amount, :prefix, presence: true
validates :codes_count, numericality: { greater_than: 0, less_than_or_equal_to: Spree::Config[:gift_card_batch_limit].to_i }
+ validates :store, :currency, presence: true
validates :amount, numericality: { greater_than: 0 }
#
@@ -18,6 +19,7 @@ module Spree
#
# Callbacks
#
+ before_validation :set_currency
after_create :generate_gift_cards
auto_strip_attributes :prefix
@@ -60,10 +62,15 @@ module Spree
gift_card_batch_id: id,
amount: amount,
amount_remaining: amount,
+ currency: currency,
code: generate_code,
store_id: store_id,
expires_at: expires_at
}
end
+
+ def set_currency
+ self.currency ||= store.default_currency
+ end
end
end
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.rb
index e533be71e8..7947f86a61 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
@@ -3,7 +3,8 @@ class CreateSpreeGiftCardsAndSpreeGiftCardBatches < ActiveRecord::Migration[7.2]
create_table :spree_gift_card_batches, if_not_exists: true do |t|
t.references :store, null: false, index: true
t.integer :codes_count, default: 1, null: false
- t.decimal :amount, precision: 10, scale: 2, default: '10.0', null: false
+ t.decimal :amount, precision: 10, scale: 2, null: false
+ t.string :currency, null: false
t.string :prefix
t.date :expires_at
@@ -20,6 +21,7 @@ class CreateSpreeGiftCardsAndSpreeGiftCardBatches < ActiveRecord::Migration[7.2]
t.string :state, null: false, index: true
t.decimal :amount, precision: 10, scale: 2, null: false
t.decimal :amount_remaining, precision: 10, scale: 2, null: false
+ t.string :currency, null: false
t.timestamps
end