Hi,

I have a shop which will have a new product each day. So it means that hopefully the stock will decrease rapidly. Since a user may not be registered it can takes some time to complete the checkout process, and I bet that if the customer finish this long process and get a notice about the product being out of stock he will not be happy.

So I disabled the "Stock: Decrease when completing the order process" rule and created two new one based on it:

Stock: Decrease when adding to cart
- Event: After adding a product to cart
- Actions: loop / decrease the product stock for a given line item

Stock: Increase when prod removed from cart
- Event: After removing a product from cart
- Actions: loop / increase the product stock for a given line item

I believe there are some rules missing. For instance "After deleting a commerce order" and even "After updating an existing commerce order" in case the admin cancel the order. I'm still new to rules and I can't figure out how to make these work. Also I probably forgot about other rules to define.

Comments

idflood’s picture

So after searching for some time here is one more rule:

Stock: Increase when order canceled
- Event: After updating an existing commerce order
- Condition: Data comparison / Parameter: Data to compare: [commerce-order:state], Data value: Canceled
- Actions: loop / Increase the product stock level, given a line item (same as above)

The remaining rule to create would be on the event "After deleting a commerce order". But it doesn't work if I try to duplicate the "Stock: Increase when prod removed from cart" rule and change the event to "After deleting a commerce order". Maybe there needs to be a new event "Before deleting a commerce order", or I'm missing something.

guy_schneerson’s picture

also we need to consider abandoned orders. this can be handled using Commerce Cart Expiration.
I think we had a similar issue before may not have the answer for you but worth tracking it down.

idflood’s picture

I've added the commerce cart expiration module, and the rule with this one is easier since it adds an event:

Stock: Increase when cart removed
- Event: Before deleting an expired cart
- Actions: loop / Increase the product stock level, given a line item (same as above)

So there is still this "On order deleted" rule which is missing.

idflood’s picture

It looks like there is an issue if the stock is equal to 1. Adding an item to cart in this case will lead to this error:
"The maximum quantity for my_product_title that can be ordered is 0."

The solution is probably to change the weight and/or event of one of these rule: "Stock: validate add to cart" and "Stock: Decrease when adding to cart"

edit:
The issue with stock of 1 is probably fixed. There is a "Stock: validate checkout" rule which needs to be disabled. It's because the stock is decreased as soon as the product is added to the cart so this interfere with that rule. Also, there is already a similare rule "Stock: validate add to cart" making the checkout one not necessary here.

guy_schneerson’s picture

@idflood, how did you get on with this, did you manage to resolve your issue with the "After deleting a commerce order" rule?

idflood’s picture

@guy_schneerson: I haven't "fixed" that issue. I told my client to always cancel an order before deleting it. I'm not sure it's possible to fix it with the "after deleting a commerce order" event, we probably need a new "Before" event in this case.

patoshi’s picture

@idflood, would you happen to have any screenshots of how you set this up on your rules pages?

I'm creating an events ticket purchasing site and need the ability to have stock deducted once its added to the cart and have it added it back if the user doesnt process the order fully within 20 minutes.

roland.molnar’s picture

This method - described in the issue summary - has a big problem: it calculates wrong values when a user tries to add the same product to the cart twice.

This is because the only input parameter for "decrease the product stock for a given line item" action is a line item. When you have a x product with qty 2 in the cart and try to add another 1, the line item will have a qty of 3 when calling this action. This means it will decrease the stock by 3 not by 1.

I have solved this problem by manually calculating the stock amount:

{ "rules_commerce_ss_stock_decrease_when_adding_to_cart" : {
    "LABEL" : "Decrease stock when adding to cart",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "stock_control" ],
    "REQUIRES" : [ "rules", "commerce_cart" ],
    "ON" : { "commerce_cart_product_add" : [] },
    "DO" : [
      { "data_calc" : {
          "USING" : {
            "input_1" : [ "commerce-product:commerce-stock" ],
            "op" : "-",
            "input_2" : [ "quantity" ]
          },
          "PROVIDE" : { "result" : { "new_quantity" : "New Quantity" } }
        }
      },
      { "data_set" : {
          "data" : [ "commerce-product:commerce-stock" ],
          "value" : [ "new-quantity" ]
        }
      }
    ]
  }
}

This works well in conjunction with the increase method described in the issue summary:

{ "rules_increase_stock_when_item_removed_from_cart" : {
    "LABEL" : "Increase stock when item removed from cart",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "stock_control" ],
    "REQUIRES" : [ "commerce_ss", "commerce_cart" ],
    "ON" : { "commerce_cart_product_remove" : [] },
    "DO" : [
      { "commerce_ss_increase_by_line_item" : { "commerce_line_item" : [ "commerce-line-item" ] } }
    ]
  }
}

But this method has problems too:
- When you try to modify the amounts on the cart page, it won't affect the stock.
- When you modify the quantity of an item on the cart page and press the remove button (instead of update cart), it will increase the stock by the entered value, not the original qty.

This can be avoided by disabling the editable quantity field in cart view.

periksson’s picture

@roland.molnar

This seems to solve your first issue:

{ "rules_test" : {
    "LABEL" : "Test",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "entity" ],
    "ON" : { "commerce_line_item_update" : [] },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "commerce-line-item" ], "field" : "commerce_product" } }
    ],
    "DO" : [
      { "data_calc" : {
          "USING" : {
            "input_1" : [ "commerce-line-item:quantity" ],
            "op" : "-",
            "input_2" : [ "commerce-line-item-unchanged:quantity" ]
          },
          "PROVIDE" : { "result" : { "net_quantity" : "Net quantity" } }
        }
      },
      { "data_calc" : {
          "USING" : {
            "input_1" : [ "commerce-line-item:commerce-product:commerce-stock" ],
            "op" : "-",
            "input_2" : [ "net-quantity" ]
          },
          "PROVIDE" : { "result" : { "new_product_quantity" : "New product quantity" } }
        }
      },
      { "data_set" : {
          "data" : [ "commerce-line-item:commerce-product:commerce-stock" ],
          "value" : [ "new-product-quantity" ]
        }
      }
    ]
  }
}
pjcdawkins’s picture

Issue summary: View changes

I have started work on this problem in a module called Commerce Stock Reserve. Please have a look and see if it meets your needs.

guy_schneerson’s picture

Hi pjcdawkins
That's exiting news. it would good to give more details about the technical implementation and how it interacts with commerce stock. I created an issue on your project and looking forward to spend more time looking at the module.

Channel Islander’s picture

Status: Active » Fixed

Marking this fixed as Commerce Stock Reserve does what is requested.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

grenuy’s picture

Required functionality for commerce 2 (drupal 8) How to do it?