Class: Spree::Carts::CheckAvailability

Inherits:
Object
  • Object
show all
Includes:
ServiceModule::Base
Defined in:
app/services/spree/carts/check_availability.rb

Overview

Asks the store's inventory source whether these items can actually be supplied.

Runs outside any transaction, as an external_step: the provider may be an external warehouse system, and holding a transaction open across that call is what turns a slow warehouse into stuck locks.

The provider only supplies the rows; Stock::Quantifier still does the arithmetic over them, so backorder limits, preorders and local checkout holds behave identically whoever owns the stock figure.

Instance Method Summary collapse

Methods included from ServiceModule::Base

prepended

Instance Method Details

#call(cart:, items:) ⇒ Spree::ServiceModule::Result

Returns value is an array of [variant, requested_quantity] pairs that cannot be supplied.

Parameters:

Returns:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/spree/carts/check_availability.rb', line 20

def call(cart:, items:)
  entries = Array(items).filter_map do |item|
    item = item.to_h.symbolize_keys
    variant = item[:variant]
    next if variant.blank?

    [variant, item[:quantity].to_i]
  end
  return success([]) if entries.empty?

  @cart = cart
  @provider = cart&.store&.inventory_provider_instance
  return success([]) if @provider.blank?

  @internal = @provider.is_a?(Spree::InventoryProvider::Internal)

  unsupplyable = entries.reject { |variant, quantity| supplyable?(variant, quantity) }

  success(unsupplyable)
rescue StandardError => e
  handle_failure(e, cart: cart)
end