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.

Display prices in Products Admin UI for the selected currency

Damian Legawiece5b179ea0432
diff --git a/backend/app/controllers/spree/admin/resource_controller.rb b/backend/app/controllers/spree/admin/resource_controller.rb
index 8b0de21739..375e5da17b 100644
--- a/backend/app/controllers/spree/admin/resource_controller.rb
+++ b/backend/app/controllers/spree/admin/resource_controller.rb
@@ -196,11 +196,15 @@ class Spree::Admin::ResourceController < Spree::Admin::BaseController
end
def set_currency
- @object.currency = current_currency if model_class.method_defined?(:currency)
- @object.cost_currency = current_currency if model_class.method_defined?(:cost_currency)
+ return if @object.nil?
+
+ @object.currency = current_currency if model_class.method_defined?(:currency=)
+ @object.cost_currency = current_currency if model_class.method_defined?(:cost_currency=)
end
def set_store
+ return if @object.nil?
+
if @object.has_attribute?(:store_id)
@object.store = current_store
elsif model_class.method_defined?(:stores)
diff --git a/backend/app/views/spree/admin/products/_form.html.erb b/backend/app/views/spree/admin/products/_form.html.erb
index 963f43e26b..01d1d1ff0e 100644
--- a/backend/app/views/spree/admin/products/_form.html.erb
+++ b/backend/app/views/spree/admin/products/_form.html.erb
@@ -31,7 +31,12 @@
<div data-hook="admin_product_form_price">
<%= f.field_container :price, class: ['form-group'] do %>
<%= f.label :price, raw(Spree.t(:master_price) + required_span_tag) %>
- <%= f.text_field :price, value: number_to_currency(@product.price, unit: ''), class: 'form-control', disabled: (cannot? :update, @product.master.default_price) %>
+ <div class="input-group">
+ <div class="input-group-prepend">
+ <span class="input-group-text"><%= currency_symbol(current_currency) %></span>
+ </div>
+ <%= f.text_field :price, value: number_to_currency(@product.amount_in(current_currency), unit: ''), class: 'form-control', disabled: (cannot? :update, @product.master.default_price) %>
+ </div>
<%= f.error_message_on :price %>
<% end %>
</div>
@@ -39,7 +44,12 @@
<div>
<%= f.field_container :compare_at_price, class: ['form-group'] do %>
<%= f.label :compare_at_price, Spree.t(:compare_at_price) %>
- <%= f.text_field :compare_at_price, value: number_to_currency(@product.compare_at_price, unit: ''), class: 'form-control' %>
+ <div class="input-group">
+ <div class="input-group-prepend">
+ <span class="input-group-text"><%= currency_symbol(current_currency) %></span>
+ </div>
+ <%= f.text_field :compare_at_price, value: number_to_currency(@product.compare_at_amount_in(current_currency), unit: ''), class: 'form-control' %>
+ </div>
<%= f.error_message_on :compare_at_price %>
<% end %>
</div>
@@ -252,19 +262,7 @@
<p data-hook="admin_product_form_stores" class="omega two columns">
<%= f.field_container :stores, class: ['form-group'] do %>
<%= f.label :store_ids, Spree.t(:stores) %>
- <% if can? :modify, Spree::Store %>
- <%= f.select :store_ids, @stores.pluck(:name, :id), { include_blank: true }, { class: 'select2-clear', multiple: true, "data-placeholder" => Spree.t(:select_stores) } %>
- <% elsif @product.stores.any? %>
- <ul class="text_list">
- <% @product.stores.each do |store| %>
- <li><%= store.name %></li>
- <% end %>
- </ul>
- <% else %>
- <div class="alert alert-info">
- <%= Spree.t(:no_resource_found, resource: :stores) %>
- </div>
- <% end %>
+ <%= f.select :store_ids, @stores.pluck(:name, :id), { include_blank: true }, { class: 'select2', multiple: true, "data-placeholder" => Spree.t(:select_stores) } %>
<% end %>
</p>
diff --git a/backend/app/views/spree/admin/products/new.html.erb b/backend/app/views/spree/admin/products/new.html.erb
index 1b0d403273..97d40b273c 100644
--- a/backend/app/views/spree/admin/products/new.html.erb
+++ b/backend/app/views/spree/admin/products/new.html.erb
@@ -33,8 +33,13 @@
<div data-hook="new_product_price" class="col-12 col-md-4">
<%= f.field_container :price, class: ['form-group'] do %>
- <%= f.label :price, Spree.t(:master_price) %> <span class="required">*</span>
+ <%= f.label :price, Spree.t(:master_price) %> <span class="required">*</span>
+ <div class="input-group">
+ <div class="input-group-prepend">
+ <span class="input-group-text"><%= currency_symbol(current_currency) %></span>
+ </div>
<%= f.text_field :price, value: number_to_currency(@product.price, unit: ''), class: 'form-control', required: :required %>
+ </div>
<%= f.error_message_on :price %>
<% end %>
</div>
diff --git a/backend/spec/features/admin/configuration/payment_methods_spec.rb b/backend/spec/features/admin/configuration/payment_methods_spec.rb
index 2d3da51147..6f14c826ad 100644
--- a/backend/spec/features/admin/configuration/payment_methods_spec.rb
+++ b/backend/spec/features/admin/configuration/payment_methods_spec.rb
@@ -3,6 +3,7 @@ require 'spec_helper'
describe 'Payment Methods', type: :feature do
stub_authorization!
+ let(:store) { Spree::Store.default }
let!(:store_1) { create(:store) }
let!(:store_2) { create(:store) }
let!(:store_3) { create(:store) }
@@ -41,6 +42,8 @@ describe 'Payment Methods', type: :feature do
select 'PaymentMethod::Check', from: 'gtwy-type'
click_button 'Create'
expect(page).to have_content('successfully created!')
+
+ expect(page).to have_select('payment_method_store_ids', selected: store.unique_name)
end
it 'is able to create a new payment method with 2 stores' do
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index fdfb77e652..183c71f4b3 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -127,7 +127,7 @@ module Spree
[
:sku, :price, :currency, :weight, :height, :width, :depth, :is_master,
- :cost_currency, :price_in, :amount_in, :cost_price, :compare_at_price
+ :cost_currency, :price_in, :amount_in, :cost_price, :compare_at_price, :compare_at_amount_in
].each do |method_name|
delegate method_name, :"#{method_name}=", to: :find_or_build_master
end
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index a92c7631d6..d3bbe0cc97 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -199,6 +199,10 @@ module Spree
price_in(currency).try(:amount)
end
+ def compare_at_amount_in(currency)
+ price_in(currency).try(:compare_at_amount)
+ end
+
def price_modifier_amount_in(currency, options = {})
return 0 unless options.present?