ECA action for setting initial stock level

Created on 11 January 2024, 12 months ago
Updated 13 January 2024, 11 months ago

Problem/Motivation

Hi,

I've configured commerce_stock to use the "Simple stock transaction" method. To enhance the user experience for creating products with multiple variations, I have created and ECK entity bundle called "Product Variation". It contains a few basic fields, like "Price" and "Stock" and an attribute. Using Inline Entity Form on Product edit forms, store managers can add multiple ECK Product Variations and on save, ECA loops through these ECK Product Variations and creates a Commerce Product Variation for each of them. ECA also constructs and sets the SKU, and does all the obvious stuff so that the store manager doesn't have to.

All this works great, but I'm running into an issue when trying to set the initial stock level. I was expecting that I could have ECA create something like a "Stock transaction entity" but that doesn't seem to be the case. It looks like Stock Transactions are not entities?

I have cloned the field_stock_level field to the ECK Product Variation, and tried to simply copy the value from that field to the field_stock_level field on the Commerce Product Variation. No luck there.

Is there a way to set initial stock levels for newly created variations using ECA? Or would the less desirable alternative work where I could configure commerce_stock to work like it did in D7 with absolute stock levels and then use ECA to update those values when a product is successfully checked out, and when a product is returned?

Thanks in advance for any insights!

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

Version

1.3

Component

Documentation

Created by

πŸ‡ΉπŸ‡­Thailand AlfTheCat

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

Comments & Activities

  • Issue created by @AlfTheCat
  • πŸ‡ΊπŸ‡ΈUnited States freelock Seattle

    Hi,

    I solved this problem a few years ago using an arbitrary field to hold the absolute stock level (synchronized from another system using an update migration we run every 15 minutes). I added a presave hook to add a stock level adjustment based on the difference between the stock level and my custom field:

    /**
     * Implements hook_ENTITY_TYPE_update().
     */
    function mymodule_commerce_product_variation_presave(EntityInterface $entity) {
      // Sync up stock level with last imported MAS stock level
      /** @var \Drupal\commerce_stock\StockServiceManagerInterface $stockManager */
      $stockManager = \Drupal::service('commerce_stock.service_manager');
      $currentLevel = $stockManager->getStockLevel($entity);
      if (round($currentLevel, 3) != round($entity->field_absolute_stock_level->value, 3)) {
        $diff = $entity->field_absolute_stock_level->value - $currentLevel;
        $entity->field_stock_level->setValue($diff);
      }
    }
    
    

    Not sure whether this makes sense to turn into an ECA action...

  • πŸ‡ΉπŸ‡­Thailand AlfTheCat

    Hi @freelock, would be awesome to have an ECA action to set the initial stock level. I think it would be useful in a number of scenarios.
    Turning this into a feature request.

  • πŸ‡ΊπŸ‡ΈUnited States freelock Seattle

    Maybe something to add to ECA Commerce β†’ ?

Production build 0.71.5 2024