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?

Files: 
CommentFileSizeAuthor
#23 increase_stock-1791350-23.patch2.39 KBguy_schneerson

Comments

guy_schneerson’s picture

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.

GeertWil’s picture

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.

guy_schneerson’s picture

good luck @GeertWil let me know how you get on

ponies’s picture

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" ] } }
          ]
        }
      }
    ]
  }
}
guy_schneerson’s picture

Thanks for sharing @ponies

pun_pun’s picture

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" ] } }
          ]
        }
      }
    ]
  }
}
guy_schneerson’s picture

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

ponies’s picture

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

guy_schneerson’s picture

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.

nodecode’s picture

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

guy_schneerson’s picture

Assigned:GeertWil» Unassigned
heyyo’s picture

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" ] } }
          ]
        }
      }
    ]
  }
svouthi’s picture

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.

guy_schneerson’s picture

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 :)

elsteff1385’s picture

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" ] } }
          ]
        }
      }
    ]
  }
}
MrPaulDriver’s picture

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?

MrPaulDriver’s picture

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

guy_schneerson’s picture

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 :)

vood002’s picture

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" ] } }
          ]
        }
      }
    ]
  }
}
?>
Lukas von Blarer’s picture

Issue summary:View changes

Can we include a this rule in the commerce stock module? Should I provide a patch?

guy_schneerson’s picture

Hi Lukas
Happy to include the rule as disabled with a note in the read me and on the project page. can look at this next time I work on stock or commit a patch if you post one.
It probably can be enabled by default but don't want to change the behavior at this point and its easy to enable a rule. we can change the default at a later stage, for now lets get it in as it obviously wanted and easy to do.

guy_schneerson’s picture

Just going over the issue again and I get the point @nodecode made in 10 and I think the approach of @ponies in the original rule in #4 is the correct one: only increase stock if state moves from one of the completed states to a canceled state.
Some implementations may need to update this but I think its safer.

guy_schneerson’s picture

Status:Active» Needs review
StatusFileSize
new2.39 KB

Included patch:

  • Adds a disabled rule - So no surprises for existing sites that upgrade
  • Rule to use old state of Pending, Processing or Completed - play it safe
  • Updated README.txt - please help with going over to make sure no typos and that is clear.

Thanks to all that helped.
This should have gone in a long time a go so sorry for the delay.
Please test and let me know if any issues or ideas.

prasannag’s picture

I tried this guy_schneerson, this patch is working fine. No issues found. Thanks

kteodor’s picture

Tested #23. Is working as it should. Thank you!

guy_schneerson’s picture

Status:Needs review» Reviewed & tested by the community

thanks @prasannag and @kteodor setting to reviewed and tested and will soon commit

  • guy_schneerson committed ff29c2a on 7.x-2.x
    Issue #1791350 by guy_schneerson, GeertWil, pun_pun  : [Increase stock...
guy_schneerson’s picture

Status:Reviewed & tested by the community» Fixed
discipolo’s picture

so can i use this in 2.x ? should i open a new issue?

guy_schneerson’s picture

Hi @discipolo this is now available in the 7.x-2.1 version and no plans to port to the version 1

Status:Fixed» Closed (fixed)

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

phponwebsites’s picture

I need to create new rule for "decrease stock when the order is pending from cancelled"
How to do this? Any help from your side.

ponies’s picture

@phponwebsites The special sauce is in the order status unchanged and the loop over line items. It'll look something kind of like this:

{ "rules_stock_control_test" : {
    "LABEL" : "Stock control test",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : true,
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "commerce_ss", "entity" ],
    "ON" : { "commerce_order_update" : [] },
    "IF" : [
      { "data_is" : { "data" : [ "commerce-order:status" ], "value" : "pending" } },
      { "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_decrease_by_line_item" : { "commerce_line_item" : [ "list-item" ] } }
          ]
        }
      }
    ]
  }
}
phponwebsites’s picture

Thank you @ponies .. I got solution for this problem.

JulienF’s picture

Thanks for the contribution and help I'm adding my grain of salt to make it (eventually) more solid, the following cases don't seem covered:

-> Deleting an order
-> Moving an order from Canceled to Completed/processing/pending etc... (in case the user puts the order as canceled by mistake) : I haven't checked other rules from the stock module so maybe other rules already included in the module cover this case

Is it planned to make a patch to cover those uses ?

guy_schneerson’s picture

Hi @JulienF and thanks for pointing this cases.
The issue is that this can never be complete as so many ways to use commerce and it is really easy to create custom rules for clients. However any nicely packaged rule can be added as disabled this allows users to enable if needed or modify / clone for their own use, so patches with new rules are welcome.