- 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 β ?