How to catch a Variation's Stock Change.

Created on 20 February 2024, 10 months ago
Updated 12 March 2024, 10 months ago

Problem/Motivation

Adjusting stock level on a Variation, does not save it.
That means, that we cannot apply business logic when the stocks changes by custom code or from an OrderEvent.

Some problems that I have encountered:

ex1 Unpublish a Product when Variation stock becomes 0.
ex2 We cannot trigger reindex on an Indexer for this Variation when Stock is adjusted.

Steps to reproduce

Use in custom code :

$stockServiceManager = \Drupal::service('commerce_stock.service_manager');
$stockServiceManager->receiveStock(PurchasableEntityInterface $entity, $location_id, $zone, $quantity, $unit_cost, $currency_code, $message = NULL)

Proposed resolution

Add hook to be triggered when variation's stock is Adjusted.
OR
Add an Event so we can subscribe to it.

If someone has a workaround or a solution, please inform.

Thank you all!

💬 Support request
Status

Fixed

Version

1.0

Component

Code

Created by

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

Comments & Activities

  • Issue created by @Spoting
  • 🇬🇷Greece geostam

    You could create an event subscriber like this:

    class YourSubscriber implements EventSubscriberInterface {
    
      public static function getSubscribedEvents() {
        return [
          LocalStockTransactionEvents::LOCAL_STOCK_TRANSACTION_INSERT => 'onTransactionInsert',
        ];
      }
    
        /**
       * Logging the insert event.
       *
       * @param \Drupal\commerce_stock_local\Event\LocalStockTransactionEvent $event
       *   The event.
       */
      public function onTransactionInsert(LocalStockTransactionEvent $event) {
    //your code
    }
  • 🇬🇷Greece geostam

    You could create an event subscriber like this:

    class YourSubscriber implements EventSubscriberInterface {
    
      public static function getSubscribedEvents() {
        return [
          LocalStockTransactionEvents::LOCAL_STOCK_TRANSACTION_INSERT => 'onTransactionInsert',
        ];
      }
    
        /**
       * Logging the insert event.
       *
       * @param \Drupal\commerce_stock_local\Event\LocalStockTransactionEvent $event
       *   The event.
       */
      public function onTransactionInsert(LocalStockTransactionEvent $event) {
    //your code
    }
  • 🇬🇷Greece geostam

    You could create an event subscriber like this:

    class YourSubscriber implements EventSubscriberInterface {
    
      public static function getSubscribedEvents() {
        return [
          LocalStockTransactionEvents::LOCAL_STOCK_TRANSACTION_INSERT => 'onTransactionInsert',
        ];
      }
    
        /**
       * Logging the insert event.
       *
       * @param \Drupal\commerce_stock_local\Event\LocalStockTransactionEvent $event
       *   The event.
       */
      public function onTransactionInsert(LocalStockTransactionEvent $event) {
    //your code
    }
  • Thank you for your feedback fellow colleague

  • Status changed to Fixed 10 months ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024