-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Configurable Order Mergeable Scope #6384
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
Merged
tvdeyen
merged 1 commit into
solidusio:main
from
ThuktenSingye:feature/configurable-mergeable-orders-scope
Nov 27, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Spree | ||
| # Finds orders to merge when a user logs in. | ||
| # | ||
| # Configurable via {Spree::Config#mergeable_orders_finder_class}. | ||
| # Default behavior finds all incomplete orders from the same store. | ||
| # | ||
| # @example Custom finder for recent orders only | ||
| # class RecentOrdersFinder | ||
| # def initialize(context:) | ||
| # @user = context.spree_current_user | ||
| # @store = context.current_store | ||
| # @current_order = context.current_order | ||
| # end | ||
| # | ||
| # def call | ||
| # @user.orders.by_store(@store).incomplete | ||
| # .where.not(id: @current_order.id) | ||
| # .where('created_at > ?', 7.days.ago) | ||
| # end | ||
| # end | ||
| # | ||
| # Spree::Config.mergeable_orders_finder_class = RecentOrdersFinder | ||
| class MergeableOrdersFinder | ||
| # @param context [Object] an object that responds to spree_current_user, | ||
| # current_store, and current_order (typically a controller) | ||
| def initialize(context:) | ||
| @user = context.spree_current_user | ||
| @store = context.current_store | ||
| @current_order = context.current_order | ||
| end | ||
|
|
||
| # Returns orders that should be merged into the current order | ||
| # | ||
| # @return [ActiveRecord::Relation<Spree::Order>] incomplete orders from the | ||
| # same store | ||
| def call | ||
| return Spree::Order.none unless @user && @current_order | ||
|
|
||
| @user.orders.by_store(@store).incomplete.where(frontend_viewable: true).where.not(id: @current_order.id) | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| module Spree | ||
| RSpec.describe MergeableOrdersFinder do | ||
| let(:user) { create(:user) } | ||
| let(:store) { create(:store) } | ||
| let(:current_order) { create(:order, user: user, store: store) } | ||
| let(:context) { double('context', spree_current_user: user, current_store: store, current_order: current_order) } | ||
| let(:subject) { Spree::MergeableOrdersFinder.new(context: context) } | ||
|
|
||
| describe '#call' do | ||
| let!(:incomplete_order1) { create(:order, user: user, store: store, state: 'cart') } | ||
| let!(:incomplete_order2) { create(:order, user: user, store: store, state: 'address') } | ||
| let!(:complete_order) { create(:order, user: user, store: store, state: 'complete').touch(:completed_at) } | ||
| let!(:other_store_order) { create(:order, user: user, state: 'cart') } | ||
|
|
||
| it 'returns incomplete orders from the same store' do | ||
| orders = subject.call | ||
| expect(orders).to include(incomplete_order1, incomplete_order2) | ||
| expect(orders).not_to include(current_order, complete_order, other_store_order) | ||
| end | ||
|
|
||
| context 'when user is nil' do | ||
| let(:context) { double('context', spree_current_user: nil, current_store: store, current_order: current_order) } | ||
|
|
||
| it 'returns empty relation' do | ||
| orders = subject.call | ||
| expect(orders).to be_empty | ||
| expect(orders).to eq(Spree::Order.none) | ||
| end | ||
| end | ||
|
|
||
| context 'when current_order is nil' do | ||
| let(:context) { double('context', spree_current_user: user, current_store: store, current_order: nil) } | ||
|
|
||
| it 'returns empty relation' do | ||
| orders = subject.call | ||
| expect(orders).to be_empty | ||
| expect(orders).to eq(Spree::Order.none) | ||
| end | ||
| end | ||
|
|
||
| context 'when both user and current_order are nil' do | ||
| let(:context) { double('context', spree_current_user: nil, current_store: store, current_order: nil) } | ||
|
|
||
| it 'returns empty relation' do | ||
| orders = subject.call | ||
| expect(orders).to be_empty | ||
| expect(orders).to eq(Spree::Order.none) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.