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.

[SD-1004] Changes links in mails to contain current store url (#10586)

Robert Nowakowski129ba3b0415c
diff --git a/core/app/mailers/spree/base_mailer.rb b/core/app/mailers/spree/base_mailer.rb
index db77a306f2..8de01c43df 100644
--- a/core/app/mailers/spree/base_mailer.rb
+++ b/core/app/mailers/spree/base_mailer.rb
@@ -23,7 +23,7 @@ module Spree
helper_method :frontend_available?
def mail(headers = {}, &block)
- ensure_default_action_mailer_url_host
+ ensure_default_action_mailer_url_host(headers[:store_url])
set_email_locale
super if Spree::Config[:send_core_emails]
end
@@ -33,9 +33,9 @@ module Spree
# this ensures that ActionMailer::Base.default_url_options[:host] is always set
# this is only a fail-safe solution if developer didn't set this in environment files
# http://guides.rubyonrails.org/action_mailer_basics.html#generating-urls-in-action-mailer-views
- def ensure_default_action_mailer_url_host
+ def ensure_default_action_mailer_url_host(store_url = nil)
ActionMailer::Base.default_url_options ||= {}
- ActionMailer::Base.default_url_options[:host] ||= current_store.url
+ ActionMailer::Base.default_url_options[:host] ||= store_url || current_store.url
end
def set_email_locale
diff --git a/core/app/mailers/spree/order_mailer.rb b/core/app/mailers/spree/order_mailer.rb
index e40dbcf973..7d503eb0cb 100644
--- a/core/app/mailers/spree/order_mailer.rb
+++ b/core/app/mailers/spree/order_mailer.rb
@@ -5,14 +5,14 @@ module Spree
current_store = @order.store
subject = (resend ? "[#{Spree.t(:resend).upcase}] " : '')
subject += "#{current_store.name} #{Spree.t('order_mailer.confirm_email.subject')} ##{@order.number}"
- mail(to: @order.email, from: from_address, subject: subject)
+ mail(to: @order.email, from: from_address, subject: subject, store_url: current_store.url)
end
def store_owner_notification_email(order)
@order = order.respond_to?(:id) ? order : Spree::Order.find(order)
current_store = @order.store
subject = Spree.t('order_mailer.store_owner_notification_email.subject', store_name: current_store.name)
- mail(to: current_store.new_order_notifications_email, from: from_address, subject: subject)
+ mail(to: current_store.new_order_notifications_email, from: from_address, subject: subject, store_url: current_store.url)
end
def cancel_email(order, resend = false)
@@ -20,7 +20,7 @@ module Spree
current_store = @order.store
subject = (resend ? "[#{Spree.t(:resend).upcase}] " : '')
subject += "#{current_store.name} #{Spree.t('order_mailer.cancel_email.subject')} ##{@order.number}"
- mail(to: @order.email, from: from_address, subject: subject)
+ mail(to: @order.email, from: from_address, subject: subject, store_url: current_store.url)
end
end
end
diff --git a/core/app/mailers/spree/reimbursement_mailer.rb b/core/app/mailers/spree/reimbursement_mailer.rb
index 6de4c3d773..feca26d5f7 100644
--- a/core/app/mailers/spree/reimbursement_mailer.rb
+++ b/core/app/mailers/spree/reimbursement_mailer.rb
@@ -6,7 +6,7 @@ module Spree
current_store = @reimbursement.store || Spree::Store.current
subject = (resend ? "[#{Spree.t(:resend).upcase}] " : '')
subject += "#{current_store.name} #{Spree.t('reimbursement_mailer.reimbursement_email.subject')} ##{@order.number}"
- mail(to: @order.email, from: current_store.mail_from_address, subject: subject)
+ mail(to: @order.email, from: current_store.mail_from_address, subject: subject, store_url: current_store.url)
end
end
end
diff --git a/core/app/mailers/spree/shipment_mailer.rb b/core/app/mailers/spree/shipment_mailer.rb
index b03609c054..cb1a76734e 100644
--- a/core/app/mailers/spree/shipment_mailer.rb
+++ b/core/app/mailers/spree/shipment_mailer.rb
@@ -6,7 +6,7 @@ module Spree
current_store = @shipment.store
subject = (resend ? "[#{Spree.t(:resend).upcase}] " : '')
subject += "#{current_store.name} #{Spree.t('shipment_mailer.shipped_email.subject')} ##{@order.number}"
- mail(to: @order.email, from: from_address, subject: subject)
+ mail(to: @order.email, from: from_address, subject: subject, store_url: current_store.url)
end
end
end
diff --git a/core/spec/mailers/order_mailer_spec.rb b/core/spec/mailers/order_mailer_spec.rb
index 9f6aa9d568..eb1a2c4905 100644
--- a/core/spec/mailers/order_mailer_spec.rb
+++ b/core/spec/mailers/order_mailer_spec.rb
@@ -8,10 +8,10 @@ describe Spree::OrderMailer, type: :mailer do
before { create(:store) }
let(:first_store) { create(:store, name: 'First Store') }
- let(:second_store) { create(:store, name: 'Second Store') }
+ let(:second_store) { create(:store, name: 'Second Store', url: 'other.example.com') }
let(:order) do
- order = stub_model(Spree::Order)
+ order = stub_model(Spree::Order, email: '[email protected]')
product = stub_model(Spree::Product, name: %{The "BEST" product})
variant = stub_model(Spree::Variant, product: product)
price = stub_model(Spree::Price, variant: variant, amount: 5.00)
@@ -25,7 +25,7 @@ describe Spree::OrderMailer, type: :mailer do
end
let(:second_order) do
- order = stub_model(Spree::Order)
+ order = stub_model(Spree::Order, email: '[email protected]')
product = stub_model(Spree::Product, name: %{The "BESTEST" product})
variant = stub_model(Spree::Variant, product: product)
price = stub_model(Spree::Price, variant: variant, amount: 15.00)
@@ -204,4 +204,18 @@ describe Spree::OrderMailer, type: :mailer do
expect(confirmation_email.subject).to include(second_store.name)
end
end
+
+ context 'emails contain only urls of the store where the order was made' do
+ it 'shows proper host url in email content' do
+ ActionMailer::Base.default_url_options[:host] = order.store.url
+ described_class.confirm_email(order).deliver_now
+ expect(ActionMailer::Base.default_url_options[:host]).to eq(order.store.url)
+ end
+
+ it 'shows proper host url in email content #2' do
+ ActionMailer::Base.default_url_options[:host] = second_order.store.url
+ described_class.confirm_email(second_order).deliver_now
+ expect(ActionMailer::Base.default_url_options[:host]).to eq(second_order.store.url)
+ end
+ end
end
diff --git a/core/spec/mailers/reimbursement_mailer_spec.rb b/core/spec/mailers/reimbursement_mailer_spec.rb
index 3d6bafda75..7e1eb438b3 100644
--- a/core/spec/mailers/reimbursement_mailer_spec.rb
+++ b/core/spec/mailers/reimbursement_mailer_spec.rb
@@ -67,4 +67,12 @@ describe Spree::ReimbursementMailer, type: :mailer do
end
end
end
+
+ context 'emails contain only urls of the store where the order was made' do
+ it 'shows proper host url in email content' do
+ ActionMailer::Base.default_url_options[:host] = reimbursement.order.store.url
+ described_class.reimbursement_email(reimbursement).deliver_now
+ expect(ActionMailer::Base.default_url_options[:host]).to eq(reimbursement.order.store.url)
+ end
+ end
end
diff --git a/core/spec/mailers/shipment_mailer_spec.rb b/core/spec/mailers/shipment_mailer_spec.rb
index 5863112c3c..6117b1d9a1 100644
--- a/core/spec/mailers/shipment_mailer_spec.rb
+++ b/core/spec/mailers/shipment_mailer_spec.rb
@@ -7,7 +7,7 @@ describe Spree::ShipmentMailer, type: :mailer do
let!(:store) { create(:store) }
- let(:order) { stub_model(Spree::Order, number: 'R12345', store: store) }
+ let(:order) { stub_model(Spree::Order, number: 'R12345', store: store, email: '[email protected]') }
let(:shipping_method) { stub_model(Spree::ShippingMethod, name: 'USPS') }
let(:product) { stub_model(Spree::Product, name: %{The "BEST" product}, sku: 'SKU0001') }
let(:variant) { stub_model(Spree::Variant, product: product) }
@@ -90,4 +90,12 @@ describe Spree::ShipmentMailer, type: :mailer do
end
end
end
+
+ context 'emails contain only urls of the store where the order was made' do
+ it 'shows proper host url in email content' do
+ ActionMailer::Base.default_url_options[:host] = store.url
+ described_class.shipped_email(shipment).deliver_now
+ expect(ActionMailer::Base.default_url_options[:host]).to eq(shipment.order.store.url)
+ end
+ end
end