Stock locations sorter

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.

Pre-configured sorters

Currently, we only provide two sorters, which you should use unless you need custom logic:

  • Unsorted , which allocates inventory from stock locations as they are returned from the DB.
  • Default first , which allocates inventory from the default stock location first and then selects the next stock location based on the position column (can be edited in the Admin UI).

Custom sorter API

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:

Ruby
    
      class Spree::Stock::LocationSorter::Priority < Spree::Stock::LocationSorter::Base
  def sort
    stock_locations.order(priority: :asc)
  end
end

    
  

Switching the sorter

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:

/config/initializer/spree.rb
Ruby
    
      Spree.config do |config|
  # ...
  config.stock.location_sorter_class = 'Spree::Stock::LocationSorter::Priority'
  # ...
end

    
  

Feedback

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.