Check on Review pane, and payment pane for stock availabilty


I'm working on the stock module, and I'd like to add a check after proceeding to the review and payment pages, that all the item are in stock.

Is there a hook for when an order changes status?



Posted: Mar 11, 2011


Ryan Ryan Szrama on March 19, 2011

Hmm... looks like there's a TODO item for this in the code. :-/

Guess that needs to get resolved before we roll another beta! Ideally the hook will be accompanied by a Rules event so you can setup the default logic for stock checks using default rule configurations.

arbel on March 20, 2011

Sounds great.
Is there anything I can do to help, I"m not much of a coder - I'm a designer but I'm looking to rollout a DC website in the next couple weeks.

Is there a different hook somewhere in the code I can look at and maybe figure out how to create a hook for the review pane?


Ryan Ryan Szrama on March 22, 2011

Ok, actually, I'm a doofus and I overlooked the obvious solution. There is in fact already a Rules event and hook that gets invoked when an existing order is saved, commerce_order_updated. To determine if an order status change occurred, you simply have to compare the updated order's status against the unchanged order's status. This actually was failing before because we weren't properly accommodating the Rules callback we were using for managing "unchanged entity" parameters in our events. I've updated this for order and ran a test using the following rule that displays a message every time an order's status is updated:

{ "rules_display_order_status_change" : {
    "LABEL" : "Display an order status change",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_order" ],
    "ON" : [ "commerce_order_update" ],
    "IF" : [
      { "NOT data_is" : { "data" : [ "order:status" ], "value" : [ "order-unchanged:status" ] } }
    "DO" : [
      { "drupal_message" : { "message" : "Changing the status of Order [order:order-id] from \u003cem\u003e[order_unchanged:status-title]\u003c\/em\u003e to \u003cem\u003e[order:status-title]\u003c\/em\u003e." } }

You'll need the latest dev version of Commerce for this to work, but you should be able to use it as is without any special hook or event for order status updates.

arbel on March 23, 2011

thanks, managed to create a rule to display the message.

Now I"m looking to create another condition via the UI and I haven't been able to understand how I do this.

what I'd like to do is run through all the line items, and check their refrenced product for the stock field I created.
compare that to the quantity on the line item, if quantity is more than stock then I would display a message and prevent the checkout from moving forward.

But it seems like I would need to create a condition that would loop through all the line items, and I see loops are only for the actions, as well as I can't seem to get access to the "stock" field I created on the product type.

Is this a condition I have to write manually, if so is there an example of something similar I can see and modify.



arbel on March 23, 2011

I've created a condition, that checks to see if the order has any products that are not in stock. This works great.

And i've managed to display a message for each line item that isn't in stock, but is there a way to prevent the user from moving forward in the checkout proccess and highlighting the quantity field with an error similar to what you would do in a validate hook:

form_error($element, t('Product is out of Stock'));


Ryan Ryan Szrama on March 23, 2011

For something of that nature you'd need to hook directly into the form validation process using hook_form_alter() and adding a validate handler to the submit button of the checkout form. Rules operates separate from the form build / validate / submit process. By the time the Rules event is invoked, the form validation step has already passed - but it's ok, because there's nothing to highlight anyways. You can just display a message and redirect back to the first checkout page.