Drupal Commerce 2 - Create Simple Stock with Custom Module and ECA

By BXTra |

About 2 years ago, I tried Drupal Commerce 2 and the module itself doesn't have comment system that a shop owner can use to make a note to let customer know and customer can also check that note in order information page.  Shop owner can decided to send notification to customer though an Email, if needed.  At that time, I decided to use Drupal Core Comment together with Rules module to create a comment form and use to communicate with customers.  Until now, Drupal Commerce 2 still doesn't have this feature built-in but well, at least I can make it work using Drupal Core and no programming at all which should be good enough. 

After waited several years for Drupal Commerce 2 to have a few features I need which should be comparable to my old Drupal 6 + Ubercart 2.x website, I think I should try it again.  This time, with ECA: Event - Condition – Action module, a lot of things can be done without programming.  (ECA module is similar to Rules module but can do a lot more.)

To be able to migrate data from my old Drupal 6 + Ubercart 2.x, I decided to use Commerce Feeds module.  What I need is only product data.  I can feeds import everything like Product Images, Price, Weight, Meta Tags but the only data I need and can't import is stock value in Drupal Commerce Stock.  Since Drupal Commerce Stock uses transaction, Feeds import is not support. 

Then, I found a good article Out of Stock feature in Drupal Commerce 2.x to create a custom module to check for out of stock item. This is only need so that the Add to cart button change to Out of stock when stock is 0 or less.

Here is what I do:

  1. Start by create an integer field name field_eca_stock in Product Variation
  2. Create a module from here.  in VariationAvailabilityChecker.php , Change this line:

        if ($entity->field_stock->value <= 0) {


       if ($entity->field_eca_stock->value <= 0) {
  3. Create a View in Views for ECA to retrieve Order item list with Contextual filters "Order item: Order"
  4. Use ECA to adjust stock value.  So, I create 3 ECA models below:
    1.    ECA - Stock - Adjust Stock when Insert, Update, Delete Items in Admin Order page.
    2.    ECA - Stock - Adjust Stock when customer complete an order.
    3.    ECA - Stock - Prevent customer to order if stock is not enough.

I don't know if the performance will be a problem with my solution or not, I will have to try it someday soon after I find all solutions I need.  Also, please note that I'm not a programmer, I understand that a lot of things can be done easily if you know how to program.  I, myself, can only read some code but can't create it especially with PHP OOP. So, anything with GUI is the best solution for me. 


ECA Process :


Views - Contextual Filter :



Software :

  1. Drupal 9.5.9
  2. ECA: Event - Condition – Action module 1.1.3


  1. Out of Stock feature in Drupal Commerce 2.x

Add new comment

  • No HTML tags allowed.
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.