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.

Fix order exports without spree admin

Damian Legawiec0aa7fc1e28f6
diff --git a/admin/app/helpers/spree/admin/payments_helper.rb b/admin/app/helpers/spree/admin/payments_helper.rb
index 60bb9e4ec9..65e5013bca 100644
--- a/admin/app/helpers/spree/admin/payments_helper.rb
+++ b/admin/app/helpers/spree/admin/payments_helper.rb
@@ -15,17 +15,6 @@ module Spree
image_tag "payment_icons/#{payment_method}.svg", opts
rescue Sprockets::Rails::Helper::AssetNotFound
end
-
- def payment_source_name(payment)
- return if payment.source.blank?
-
- source_class = payment.source.class
- if source_class.respond_to?(:display_name)
- source_class.display_name
- else
- source_class.name.demodulize.split(/(?=[A-Z])/).join(' ')
- end
- end
end
end
end
diff --git a/admin/spec/helpers/spree/admin/payment_helper_spec.rb b/admin/spec/helpers/spree/admin/payment_helper_spec.rb
deleted file mode 100644
index 1860556c1b..0000000000
--- a/admin/spec/helpers/spree/admin/payment_helper_spec.rb
+++ /dev/null
@@ -1,34 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Admin::PaymentsHelper, type: :helper do
- let!(:store) { create(:store) }
-
- before do
- allow(helper).to receive(:current_store).and_return(store)
- end
-
- describe '#payment_source_name' do
- let(:payment_method) { create(:credit_card_payment_method, stores: [store]) }
- let(:payment) { build(:payment, payment_method: payment_method) }
-
- before do
- allow(payment).to receive(:source).and_return(source)
- end
-
- context 'for source with display_name' do
- let(:source) { double('Payment Source', class: double('Payment Source Class', display_name: 'Display 554')) }
-
- it 'returns the display name of the source class' do
- expect(helper.payment_source_name(payment)).to eq('Display 554')
- end
- end
-
- context 'for source without display_name' do
- let(:source) { double('Payment Source', class: double('Payment Source Class', name: 'MyPaymentMethod')) }
-
- it 'returns the display name of the source class' do
- expect(helper.payment_source_name(payment)).to eq('My Payment Method')
- end
- end
- end
-end
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index 19074d3433..cb0309a9ff 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -581,6 +581,15 @@ module Spree
persist_totals
end
+ def shipping_method
+ # This query will select the first available shipping method from the shipments.
+ # It will use subquery to first select the shipping method id from the shipments' selected_shipping_rate.
+ Spree::ShippingMethod.
+ where(id: shipments.with_selected_shipping_method.limit(1)).
+ limit(1).
+ first
+ end
+
def is_risky?
!payments.risky.empty?
end
diff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rb
index 57ca0e1944..a40a4c0b1e 100644
--- a/core/app/models/spree/payment.rb
+++ b/core/app/models/spree/payment.rb
@@ -206,6 +206,13 @@ module Spree
checkout? || pending?
end
+ def display_source_name
+ return if source.blank?
+
+ source_class = source.class
+ source_class.respond_to?(:display_name) ? source_class.display_name : source_class.name.demodulize.split(/(?=[A-Z])/).join(' ')
+ end
+
private
def after_void
diff --git a/core/app/presenters/spree/csv/order_line_item_presenter.rb b/core/app/presenters/spree/csv/order_line_item_presenter.rb
index 3a73abcf07..4850d247ef 100644
--- a/core/app/presenters/spree/csv/order_line_item_presenter.rb
+++ b/core/app/presenters/spree/csv/order_line_item_presenter.rb
@@ -1,8 +1,6 @@
module Spree
module CSV
class OrderLineItemPresenter
- include Spree::Admin::PaymentsHelper
-
HEADERS = [
'Number',
'Email',
@@ -87,7 +85,7 @@ module Spree
index.zero? ? order.total.to_f : nil,
index.zero? ? order.shipping_method&.name : nil,
index.zero? ? order.total_weight.to_f : nil,
- index.zero? && order.payments.valid.any? ? payment_source_name(order.payments.valid.first) : nil,
+ index.zero? && order.payments.valid.any? ? order.payments.valid.first.display_source_name : nil,
line_item.product_id,
line_item.quantity,
line_item.sku,
diff --git a/core/spec/models/spree/payment_spec.rb b/core/spec/models/spree/payment_spec.rb
index 9dcf0e2470..96ef539732 100644
--- a/core/spec/models/spree/payment_spec.rb
+++ b/core/spec/models/spree/payment_spec.rb
@@ -1021,4 +1021,31 @@ describe Spree::Payment, type: :model do
it { expect(payment.source).to be_nil }
end
end
+
+ describe '#display_source_name' do
+ let(:payment_method) { create(:credit_card_payment_method, stores: [store]) }
+ let(:payment) { build(:payment, payment_method: payment_method) }
+
+ before do
+ allow(payment).to receive(:source).and_return(source)
+ end
+
+ subject { payment.display_source_name }
+
+ context 'for source with display_name' do
+ let(:source) { double('Payment Source', class: double('Payment Source Class', display_name: 'Display 554')) }
+
+ it 'returns the display name of the source class' do
+ expect(subject).to eq('Display 554')
+ end
+ end
+
+ context 'for source without display_name' do
+ let(:source) { double('Payment Source', class: double('Payment Source Class', name: 'MyPaymentMethod')) }
+
+ it 'returns the display name of the source class' do
+ expect(subject).to eq('My Payment Method')
+ end
+ end
+ end
end