This article explains the purpose, interface and correct usage of a custom stock locations sorter.
Your app's stock locations sorter defines the order in which stock locations are used to allocate
inventory when creating packages for an order. The sorter is called by Spree::Stock::SimpleCoordinator
when allocating inventory for an order.
Currently, we only provide two sorters, which you should use unless you need custom logic:
A custom sorter should inherit from Spree::Stock::LocationSorter::Base
and implement a sort
method
which accepts a Spree::StockLocation::ActiveRecord_Relation
and returns an enumerable of stock
locations. Note that the return value does not have to be an AR relation.
Here's an example that sorts stock locations by a custom priority
attribute:
class Spree::Stock::LocationSorter::Priority < Spree::Stock::LocationSorter::Base
def sort
stock_locations.order(priority: :asc)
end
end
Once you have created the logic for the new sorter, you need to register it so that it's used by
Spree::Stock::SimpleCoordinator
.
For example, you can register it in your /config/initializers/spree.rb
initializer:
Spree.config do |config|
# ...
config.stock.location_sorter_class = 'Spree::Stock::LocationSorter::Priority'
# ...
end
Solidus is an open source platform supported by the community. We encourage everyone using Solidus to contribute back to the documentation and the code.
If you’re interested in contributing to the docs, get started with the contributing guidelines. If you see something that needs fixing and can’t do it yourself, please send us an email.