I'm a novice in Drupal Commerce, so perhaps I'm completely wrong here.
I installed Drupal Commerce Kickstart v2 RC1 and added the commerce stock module.

On my test site I see that the stock changes when someone bought a product. When an order is canceled, the stock does not change again. Is this as planned, or am I missing something here?

Comments

Hi @GeertWil

The problem is that each shop manages stock differently and such a rule would only work for some.
However you should be able to create a rule that is triggered by the update of the order with a condition checking the order state.
This will be smiler to the complete checkout rule only will use the action "Increase the product stock level, given a line item" instead of "Decrease the product stock level, given a line item".

If i get the time ill add this rule as disabled and people can enable it if needed.
Please post an export of the rule if you manage to create it.

Thanks for the reply.
I'm quite new to Drupal Commerce, but I already succesfully created some - rather simple - rules. I'l give it a try to add an "increase stock" rule.

good luck @GeertWil let me know how you get on

Here's an export of the rule I set up. It's for the 2.x branch though.

{ "rules_increment_stock_canceling_order" : {
    "LABEL" : "Stock: Increase when canceling the order process",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_ss", "entity" ],
    "ON" : [ "commerce_order_update" ],
    "IF" : [
      { "data_is" : { "data" : [ "commerce-order:status" ], "value" : "canceled" } },
      { "data_is" : {
          "data" : [ "commerce-order-unchanged:status" ],
          "op" : "IN",
          "value" : { "value" : { "processing" : "processing", "completed" : "completed" } }
        }
      }
    ],
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "commerce-order:commerce-line-items" ] },
          "ITEM" : { "list_item" : "Current list item" },
          "DO" : [
            { "commerce_ss_increase_by_line_item" : { "commerce_line_item" : [ "list-item" ] } }
          ]
        }
      }
    ]
  }
}

Thanks for sharing @ponies

Hi!
This rule doesn't work for me in 2.0 ver... In case of wrong condition... I've changed it a little bit:

{ "rules_increment_stock_canceling_order" : {
    "LABEL" : "Stock: Increase when canceling the order process",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_ss", "entity" ],
    "ON" : [ "commerce_order_update" ],
    "IF" : [
      { "data_is" : { "data" : [ "commerce-order:status" ], "value" : "canceled" } },
      { "AND" : [
          { "NOT data_is" : { "data" : [ "commerce-order-unchanged:status" ], "value" : "canceled" } }
        ]
      }
    ],
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "commerce-order:commerce-line-items" ] },
          "ITEM" : { "list_item" : "Current list item" },
          "DO" : [
            { "commerce_ss_increase_by_line_item" : { "commerce_line_item" : [ "list-item" ] } }
          ]
        }
      }
    ]
  }
}

Thanks @pun_pun
The unchanged status not in "canceled" makes more sense.

Is stock decremented at a certain order status? Would going from cart->cancelled increment the stock amount before it's decremented?

Hi @ponies
The default rule that comes with the module uses the "Completing the checkout process" event to decrement stock, this gets triggered when you move to the completed checkout page".
However you can replace this with rules triggered by the change of the order state, probably safer for what we are attempting to do.

@guy_schneerson
Could you explain the reason why you say altering the default stock decrementing rule would solve the question in #8? It seems to me that regardless of how the stock is decremented upon order completion, order cancellation could still increment incorrectly if it's done before the order is complete. That is unless the rule in #6 also checks that the unchanged order is not in a state that precedes the state at which stock is decremented (i.e. cart, checkout_checkout, checkout_shipping, checkout_review, checkout_payment).

Is that correct or am I misunderstanding something here?

Assigned:GeertWil» Unassigned

Same process shoud also happen if an order is deleted and with status different of Cancelled:

{ "rules_stock_increase_when_deleting_an_order" : {
    "LABEL" : "Stock: Increase when deleting an order",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "Stock" ],
    "REQUIRES" : [ "rules", "commerce_ss", "entity" ],
    "ON" : [ "commerce_order_delete" ],
    "IF" : [
      { "NOT data_is" : { "data" : [ "commerce-order:status" ], "value" : "canceled" } }
    ],
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "commerce-order:commerce-line-items" ] },
          "ITEM" : { "list_item" : "Current list item" },
          "DO" : [
            { "commerce_ss_increase_by_line_item" : { "commerce_line_item" : [ "list-item" ] } }
          ]
        }
      }
    ]
  }

Thank you, pun_pun - I added your "Stock: Increase when canceling the order process" rule to good effect. I appreciate you sharing it.

I also tried adding heyyo's "Stock: Increase when deleting an order" rule, however it didn't work for me. If someone else has also used it effectively, I would appreciate hearing about it. Thanks.

Thanks to all for contributing rules and reviewing them.
All input greatly appreciate as I am planning to include "Increase stock" rules as disabled in the full release of version 2.
Keep up the good work :)

pun_pun's rule in #6 is perfectly working :)
heyyo's rule in #12 is unfortunately not working, like svouthi confirms too. The reason for this is obvious: after deleting the order, heyyo's rule tries to do actions on line items, but those have been deleted...

heyyo's idea would be wonderfull though, but to make it work, we would need an event "before deleting a commerce order" like described in https://drupal.org/node/1926154

I enhanced pun-pun's rule a little so it won't act on a return order, made by the commerce_rma module. It should not apply stock changes if a order is of type "commercer_return".

{ "rules_increment_stock_canceling_order" : {
    "LABEL" : "Stock: Increase when canceling the order process",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "stock_control" ],
    "REQUIRES" : [ "rules", "php", "commerce_ss", "entity" ],
    "ON" : [ "commerce_order_update" ],
    "IF" : [
      { "data_is" : { "data" : [ "commerce-order:status" ], "value" : "canceled" } },
      { "NOT data_is" : { "data" : [ "commerce-order-unchanged:status" ], "value" : "canceled" } },
      { "NOT php_eval" : { "code" : "if ($commerce_order-\u003Etype == \u0022commerce_return\u0022) {\r\n  return true;\r\n}\r\n" } }
    ],
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "commerce-order:commerce-line-items" ] },
          "ITEM" : { "list_item" : "Current list item" },
          "DO" : [
            { "commerce_ss_increase_by_line_item" : { "commerce_line_item" : [ "list-item" ] } }
          ]
        }
      }
    ]
  }
}

Assigned:guy_schneerson» Unassigned

I'm loving the rule imports.

Is one of these beauties likely to find its way in to a release candidate?

Is there a business case for being able to reinstate a cancelled order and having the stock level re-set again?

Assigned:Unassigned» guy_schneerson

Hi @MrPaulDriver
I am planning on getting out a stable version as soon as possible so feedback is welcome.
This may not go to the first full version 2 but likely to be shortly after.
One of the main criteria for picking new features is interest and community involvement in the issue q and this is one of those.
assigning to myself so I remember to give it attention :)

Here's a rule for Commerce Stock 1.x if anyone is interested. Tested well...only increments if order is canceled.

<?php
{ "commerce_stock_increase_stock_when_order_is_canceled" : {
   
"LABEL" : "Increase stock when order is canceled",
   
"PLUGIN" : "reaction rule",
   
"OWNER" : "rules",
   
"REQUIRES" : [ "rules", "commerce_stock", "entity" ],
   
"ON" : { "commerce_order_update" : [] },
   
"IF" : [
      {
"NOT data_is" : {
         
"data" : [ "commerce-order-unchanged:status" ],
         
"value" : [ "commerce-order:status" ]
        }
      },
      {
"data_is" : { "data" : [ "commerce-order:status" ], "value" : "canceled" } },
      {
"AND" : [
          {
"OR" : [
              {
"data_is" : { "data" : [ "commerce-order-unchanged:state" ], "value" : "pending" } },
              {
"data_is" : { "data" : [ "commerce-order-unchanged:state" ], "value" : "completed" } }
            ]
          }
        ]
      }
    ],
   
"DO" : [
      {
"LOOP" : {
         
"USING" : { "list" : [ "commerce_order:commerce-line-items" ] },
         
"ITEM" : { "list_item" : "Current list item" },
         
"DO" : [
            {
"commerce_stock_increase_by_line_item" : { "commerce_line_item" : [ "list-item" ] } }
          ]
        }
      }
    ]
  }
}
?>