Problem/Motivation

Interface with the Commerce AvailabilityManager service that implements AvailabilityManagerInterface

<?php
  commerce.availability_manager:
    class: Drupal\commerce\AvailabilityManager
    tags:
      - { name: service_collector, tag: commerce.availability_checker, call: addChecker }
?>

This service allows adding instances of AvailabilityCheckerInterface using addChecker.
The service then provides a method to check availability check(PurchasableEntityInterface $entity, $quantity = 1)

Proposed resolution

StockAvailabilityChecker - implements AvailabilityCheckerInterface so can be added to the AvailabilityManager however we have the following points to consider:

1) The StockAvailabilityChecker itself has dependency injection for the EntityStockCheckInterface

2) Should we override the AvailabilityManager to make sure only stock can check if installed? - probably not.

3) The plan is to support more then one instance of EntityStockCheckInterface (probably configured by product type).
We have two options:
a) add one checker - StockAvailabilityChecker acts as a factory for EntityStockCheckInterface determined by the type of the PurchasableEntityInterface passed in.
b) create a AvailabilityCheckerInterface instance for each EntityStockCheckInterface and add those to the AvailabilityManager. Each of those will need to check the PurchasableEntityInterface to find out if it needs to run its own check.

Remaining tasks

  • implement StockAvailabilityChecker.
  • create stock services interface & implement AlwaysInStockService.
  • implement Commerce Stock Storage sub module.
  • implement the availability_manager in commerce
  • UI and code to determine the stock services tot use
CommentFileSizeAuthor
#5 intrface_with-2709939-5.patch924 bytesSut3kh
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

guy_schneerson created an issue. See original summary.

guy_schneerson’s picture

#2710763: Create StockManager service implanted the StockAvailabilityChecker & AvailabilityCheckerInterface
The StockAvailabilityChecker is a commerce.availability_checker service
It uses the StockManager is a service_collector that collects commerce.stock_service
A commerce.stock_service holds a Stock checker a stock updater & stock configuration.
A always_in_stock_service a commerce.stock_service - was added and will now be triggered by calling the commerce availability manager:

<?php
    $availabilityManager = \Drupal::service('commerce.availability_manager');
    $availabe = $availabilityManager->check($product_variation, $prod_qty);
?>

All that is left is to

  • implement Commerce Stock Storage sub module.
  • implement the availability_manager in commerce
  • UI and code to determine the stock services tot use
guy_schneerson’s picture

Issue summary: View changes

  • guy_schneerson committed e1c663d on 8.x-1.x
    Issue #2709939: Intrface with AvailabilityManager & StockManager
    
Sut3kh’s picture

FileSize
924 bytes

Not sure if you want this here or as a separate issue but with the latest Drupal Commerce this throws:

PHP Fatal error: Declaration of Drupal\\commerce_stock\\StockAvailabilityChecker::check() must be compatible with Drupal\\commerce\\AvailabilityCheckerInterface::check(Drupal\\commerce\\PurchasableEntityInterface $entity, $quantity, Drupal\\commerce\\Context $context)

It seems that #2803221: Create context value object for price resolvers and availability checkers has added '$context' to handle checks to retrospective order changes i.e. if the price was date based.

I can't see that time, customer or store are factors in the stock check so I have worked under the assumption they aren't, however this is literally my first foray into d8 commerce & commerce_stock (this patch is at least just to allow me to get it up and running!) so input is more than welcome :)

guy_schneerson’s picture

Thanks @Sut3kh that's a super useful patch and explanation.
Your patch does exactly what was needed. Not sure yet how we will use the $context and I suspect the AvailabilityManager will have to go through a few other changes. It currently stops a user from adding out of stock items but the add to cart function is not aware of that so you will get the "product added" msg. @steveoliver from the linked issue is also helping on the project so will probably be able to help.
Ideally this would have been a separate bug with a link to this issue but no big deal, main thing is you fixed it :)

steveoliver’s picture

Status: Active » Fixed

I believe the StockAvailabilityChecker works as expected now.

steveoliver’s picture

Title: Intrface with AvailabilityManager » Stock availability checker service implements Commerce AvailabilityCheckerInterface

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.