Use calculated stock level for all stock locations

Created on 14 October 2021, about 3 years ago
Updated 13 February 2023, almost 2 years ago

Problem/Motivation

Module calculates stock level for each stock location each time, without saving & reusing calculated values.
For example, if you use commerce_stock_enforcement module, which disables add to cart submit button if variation is out of stock, stock checker will load all stock locations and calulate stock levels for each of them in runtime. It can be ok if the store has only one stock location, but causes overhead when it has multiple stock location.

Steps to reproduce

Enable commerce_stock_enforcement module and create multiple (e.g. 10) stock locations.
You will see that speed of page loading is slowing down due to mentioned above calculations while add to cart placeholders are working.

Proposed resolution

Use separate storage for calculated variation stock level results for all stock locations.
I've made the patch, where i use @keyvalue storage to store calculated stock level results and return them if exists.
So now we have one request per product variation, no matter how many stock locations has the store.

Remaining tasks

Since we store calculated results for stock levels, we do have correctly invalidate them.
And at this point i'm not sure if i do it in a right way.

The proposed patch implementing this:

  1. Save calculated stock level for all stock location in StockServiceManager::getStockLevel()
  2. Delete calculated value for variation when creating new transaction in LocalStockUpdater::createTransaction()
  3. Also delete calculated value while StockLevelUpdater queue worker processing in StockLevelUpdater::processItem()

So, i thing the invalidation of calculated data could be simplified, any suggestions would be grate.

I'm suggesting this patch to see if this approcah actually usefull and if it is, i could improve some of the weaknesses like using Dependency Injection or improving invalidation logic.

Feature request
Status

Closed: outdated

Component

Code

Created by

🇷🇺Russia a.kovrigin Penza

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024