Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sorting store credits with different algorithms #4677

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,9 @@ def add_store_credit_payments

if matching_store_credits.any?
payment_method = Spree::PaymentMethod::StoreCredit.first
sorter = Spree::Config.store_credit_prioritizer_class.new(matching_store_credits, self)

matching_store_credits.order_by_priority.each do |credit|
sorter.call.each do |credit|
break if remaining_total.zero?
next if credit.amount_remaining.zero?

Expand Down
17 changes: 17 additions & 0 deletions core/app/models/spree/store_credit_prioritizer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Spree
class StoreCreditPrioritizer
def initialize(credits, _order)
@credits = credits
end

def call
credits.order_by_priority
end

private

attr_reader :credits
end
end
9 changes: 9 additions & 0 deletions core/lib/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,15 @@ def payment_canceller
small: '400x400>',
product: '680x680>',
large: '1200x1200>' }

# Allows providing your own class for prioritizing store credit application
# to an order.
#
# @!attribute [rw] store_credit_prioritizer_class
# @return [Class] a class with the same public interfaces as
# Spree::StoreCreditPrioritizer.
class_name_attribute :store_credit_prioritizer_class, default: 'Spree::StoreCreditPrioritizer'

# @!attribute [rw] taxon_image_style_default
#
# Defines which style to default to when style is not provided
Expand Down
24 changes: 24 additions & 0 deletions core/spec/models/spree/store_credit_prioritizer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Spree::StoreCreditPrioritizer, type: :model do
let(:order) { create(:order) }
let(:credits) { Spree::StoreCredit.all }
let(:sorter) { described_class.new(credits, order) }

describe '#call' do
subject { sorter.call }

let(:credit_type_1) { create(:primary_credit_type, priority: '30') }
let!(:credit_1) { create(:store_credit, credit_type: credit_type_1) }
let(:credit_type_2) { create(:primary_credit_type, priority: '20') }
let!(:credit_2) { create(:store_credit, credit_type: credit_type_2) }
let(:credit_type_3) { create(:primary_credit_type, priority: '10') }
let!(:credit_3) { create(:store_credit, credit_type: credit_type_3) }

it 'returns the credits ordered by their priority' do
expect(subject.to_a).to eq([credit_3, credit_2, credit_1])
end
end
end