Download & Extend

After deleting/cancelling an order, stock is not incremented again

Project:Ubercart
Version:7.x-3.x-dev
Component:Stock
Category:feature request
Priority:normal
Assigned:Unassigned
Status:patch (to be ported)
Issue tags:uc_stock

Issue Summary

When placing an order, the quantity of this article in stock is reduced correctly, but after deleting the order (or setting status to canceled), the quantity is not incremented again.

This will cause Drupal to show wrong quantities of an article in stock, e.g. when a customer skipped his order.

Comments

#1

Version:6.x-2.0-rc7» 6.x-2.0

The issue is still present in 6.x-2.0.

The trigger already exists when changing the status of the order, so I just had to add the following code (copying what already exists in the module), and it works.
But I can't figure out how to trigger something when the order is being deleted. Is-it possible ?

/**
* Increment a product's stock.
*
* @param $product
*   The product whose stock is decrementing.
*/
function uc_stock_increment_product_stock($product, $key, $order) {
  // Product has an active stock?
  if (!uc_stock_is_active($product->model)) {
    return;
  }

  // Decrement the product's stock.
  uc_stock_adjust($product->model, $product->qty);

  // Load the new stock record
  $stock = db_fetch_object(db_query("SELECT * FROM {uc_product_stock} WHERE sku = '%s'", $product->model));

  // Save a comment about the stock level.
  uc_order_comment_save($order->order_id, 0, t('The stock level for %model_name has been incremented to !qty.', array('%model_name' => $product->model, '!qty' => $stock->stock)));
}

/**
* Implementation of hook_ca_predicate().
*/
function uc_stock_ca_predicate() {
  $predicates['uc_stock_action_increment_stock'] = array(
    '#title' => t('Increment stock on cancelling order'),
    '#trigger' => 'uc_order_status_update',
    '#conditions' => array(
      '#operator' => 'AND',
      '#conditions' => array(
        array(
          '#name' => 'uc_order_status_condition',
          '#title' => t('If the order status is Cancelled.'),
          '#argument_map' => array(
            'order' => 'updated_order',
          ),
          '#settings' => array(
            'order_status' => 'canceled',
          ),
        ),
      ),
    ),
    '#class' => 'uc_stock',
    '#status' => 1,
    '#actions' => array(
      array(
        '#name' => 'uc_stock_action_increment_stock',
        '#title' => t('Increment stock of products in order'),
        '#argument_map' => array(
          'order' => 'order',
        ),
      ),
    ),
  );

  return $predicates;
}

/**
* Implementation of hook_action().
*/
function uc_stock_ca_action() {
  $actions['uc_stock_action_increment_stock'] = array(
    '#title' => t('Increment stock of products on the order with tracking activated.'),
    '#callback' => 'uc_stock_action_increment_stock',
    '#arguments' => array(
      'order' => array('#entity' => 'uc_order', '#title' => t('Order')),
    ),
    '#category' => t('Stock'),
  );

  return $actions;
}

/******************************************************************************
* Conditional Action Callbacks and Forms                                     *
******************************************************************************/

/**
* Increase the stock of ordered products.
*/
function uc_stock_action_increment_stock($order, $settings) {
  if (is_array($order->products)) {
    array_walk($order->products, 'uc_stock_increment_product_stock', $order);
  }
}

#2

subscribing

#3

Priority:normal» critical

I think it is safe to say that this is a critical bug, no? I don't think store administrators know it is not incremented again and they should not even have to think about it. If this happens a few times, the stock would become a complete chaos and would make it useless in the end.

#4

Where do you copy this code to implement this? I'd like to get it where I can set a conditional action that if a order is updated to a specific status that stock gets incremented. I'd really appreciate the help on this.

#5

Hi,

The code I used is in the 'uc_stock' module (I only replace decrement by increment).

I pasted the code in one of the costum modules I developped for the project I was working on, renaming every "uc_stock_function" into "project_name_function", and after refreshing the function (by visiting the module page), I could use the actions and conditions defined here to fix the bug.

I hope it helps, otherwise, I'll try to explain better.

Best Regards,
Simon

#6

I agree, the only major flaw of Ubercart is stock handling. Having stock incremented is a must.

#7

How about posting a patch so we can try it out and see if it has any unintended consequences?

#8

subscribing - agree this is critical.

#9

Hi, you'll find attached my first try to provide a patch taking #1 into account.

It's the first time I provide a patch, so my apologies if it's not perfect. Even if it works, I think the Ubercart would be able to provide a far better patch, because this one only suits my needs, and is not generic enough (I think).

Let me know if there is any problem with it anyway.

Best Regards,
Simon

AttachmentSizeStatusTest resultOperations
uc_stock.patch3.24 KBIdleFAILED: [[SimpleTest]]: [MySQL] Unable to apply patch uc_stock_0.patch. This may be a -p0 (old style) patch, which is no longer supported by the testbots.View details | Re-test

#10

Status:active» needs review

Please change the status to "needs review" when you post a patch...

#11

This is my first time trying to patch a module but it failed, I'm using ubercart 6.x-2.2 btw.. does it matter?

In need to increment stock on order cancellation/deletion! >.<

#12

If I remember correctly, I tested the patch on ubercart 6.x-2.2 as well. What command line did you use to patch your module ?

Please not that the patch only cover order cancellation (see #1). I don't know how to trigger the stock increment on order deletion, I'd have to go deeper in my ubercart understanding ;)

#13

I'm using http://gnuwin32.sourceforge.net/ to apply the patch :)

Hmm what I really need is to increment the stock on order deletion.. I don't understand why ubercart was around for so long but there's still no fix on this.. I thought this is the most basic function in any e-commerce system :(

Really need this function.. Help!

#14

I am subscribing, too

Stock incrementing after order cancellation and after order deletion are highly needed basic functions.

#15

I'm also in dire need of this!!!!

#16

Any reports on the progress of this? When will this be implemented into ubercart?

#17

Thx dude this patch worked just fine 4 me !!

#18

But what if a single product is removed from an order. It would be cool to handle that as well.

#19

Category:bug report» task

I've made a simple module based on code #1 and I've expanded it to return products during the order remove. My module gives You a new trigger [on order delete] and predicate which returns products.
It's pre-beta and I don't know if it's good-enough to be put on drupal repository but it's working.
Tell me if it's working fine on your applications. Maybe someone would make a uc_stock patch out of it ?

Sorry if my English is poor

AttachmentSizeStatusTest resultOperations
uc_stock_return.zip1.83 KBIgnored: Check issue status.NoneNone

#21

@barteque

There is no file?

#22

subscribing, not happy to read the above

#23

Ok I'm giving it once again.
It's on my ftp http://gogol.etrius.net/Public/uc_stock_return.zip and on drupal.org...
Have fun!

AttachmentSizeStatusTest resultOperations
uc_stock_return.zip1.8 KBIgnored: Check issue status.NoneNone

#24

Thank you barteque!

It's working just fine.

#25

I tested it with product attributes - no problems!

#26

Subscribing

#27

subscribe

#28

Thank you, Barteque.

I used it. Puts the stock back when an order is deleted.

Thank you for solving a major issue in UC ordering!

#29

Status:needs review» reviewed & tested by the community

There are three reviewer (#23). Marking this as RTBTC

#30

Status:reviewed & tested by the community» active

Can't be RBTC because there is nothing to commit yet! Still need a patch here people ... and the patch should address the concerns raised in #12 (works only for order cancellation, not for order deletion) and #18 (only works for entire orders, doesn't work when single products are removed from an order).

#31

@barteque
Thanks!
Installed the module, worked great.

There's still a problem:

- When an order is canceled the stock is incremented correctly
- But when a canceled order is updated to some other status, the quantity is not reduced again.

#32

So, what's the status of this thread? Do we need someone to roll this into a patch for uc_stock?

#33

"But when a canceled order is updated to some other status, the quantity is not reduced again."

This can be achieved by adding another CA predicate to check the existing status is cancelled and the new status is something other than cancelled, with an action to decrement the stock again. Perhaps this should in fact replace the current default predicate that decrements stock on checkout completion, to instead decrement stock if the current order status is either in checkout or cancelled and the new order status is neither of these.

#34

I think that sounds right.

Also feels like this should all be part of the shipped uc_stock module.

#35

Subscribing.

I've got to follow this one.
Thanks everybody!

#36

Subscribing.

#37

subscribe

#38

Version:6.x-2.0» 6.x-2.x-dev

I had to look at this, I was about to submit a patch but it has proven to be a bit more complex that I first thought.

One important note is that the stock is properly restored when an item is removed from an order, that's currently working, the only thing is that is not properly logged. I am submitting a patch to fix this and also stock decrement for product kits when added on admin created orders, please help review that one as well if you can #1014994: Product kit stock decrement not working and stock increment on product removal doesn't create order comment.

For this specific issue which I tried to address, I first added which was going to be a patch:

<?php
  
// Update stock on order deletion
   
if (module_exists('uc_stock')) {
      foreach (
$order->products as $product) {
       
// setting it negative so the stock goes back up
       
$product->qty = -$product->qty;
       
uc_stock_adjust_product_stock($product, 0, $order);
      }
     
// uc_stock_adjust_product_stockThe stock of the order's products has been restored.
     
drupal_set_message(t('The stock level of each product in this order has been restored.'));
    }
?>

to uc_order_delete() but then I realized that you might delete orders which stock were never decreased, like 'in checkout' orders never completed, so I think that the idea proposed by the module on #23 to create a trigger for order deletion and then an action to restore stock is the proper way to go. It might be good to have that included in ubercart rather than in an external module. If that patch will be consider I might step in and work it out. Don't have the time right now but if @TR or any other ubercart dev might be willing to consider/include it for the next release, just give me a shout and I'll try to chip this in.

What I said above about restoring stock on order deletion which might have not been decreased applies as well for removing individual products. If you remove a product from an in_checkout order, it will increase stock.

The main problem here is that stock decrement is both mixed as conditional actions and hardcoded in the code, depending on what workflow or action you take on an order.

Ideally stock decrement/increment should be either controlled entirely by conditional actions and different triggers, or properly handled within the code when it's really necessary, which is probably more tricky. CA seems the best way but I don't think that will happen on 2.x.

#39

This causes problems for me too. Please keep us posted on a resolution to this bug. Thanks.

#40

Subscribing.

#41

This module (found in post #23) works great. Simply download and enable it as normal. Go to /admin/store/ca and add a new predicate. Give it a title of 'Increment stock on canceling an order', and choose 'An order is being canceled'. Then give it the increment stock action.

You should now find that your stock is incremented when you cancel an order.

If you would rather have this work on canceling an order, open the uc_stock_return.module file in your text editor and string replace all instances of 'delete', 'deleted', and 'deleting' (case insensitive) with 'Cancel', 'Canceled', and 'Canceling'. Save and/or reupload.

#42

Can we get this up as a project on drupal.org? Esp #23 or the latest working?

#43

Thank you very much Bartosz.

#44

Subscribing

#45

I've been using my own, custom stock implementation for YEARS because of things like this.
sub.

#46

same in 7.x I think a rule action should be made 'increment stock on order canceling'

#47

Amazing that this is still a lingering issue. Any progress here? Wish I could help...have a site needing this and maybe able to throw some money at it.

#48

@millenniumtree Could you share your implementation?

Does it contain a stock log, so it knows why the stock is currently at its level?

#49

Component:Code» Stock
Category:task» bug report
Status:active» needs work

I improved the module posted by barteque. See attached.

It now also works when switching back from 'canceled' to another order status.
And it always excludes the 'in_checkout' status.
But that can be adjusted through the conditional actions interface, if you'd like.

In reply to #30:
+ it already acted on order deletion
+ the Ubercart core Order module module already handles individual product removal from an order, updating stock level

Ideally this module would be merged with the Ubercart core Stock module.

Have fun

AttachmentSizeStatusTest resultOperations
uc_stock_return2.zip1.72 KBIgnored: Check issue status.NoneNone

#50

subscribing

#51

@PieterDC: Can you please post a patch? A .zip file is not very useful.

#52

Status:needs work» needs review

A .zip file containing a standalone module is super useful for people wanting this fix, without having to patch the original Ubercart module.

Nevertheless; you'll find a patch attached.
Didn't test it yet, just moved the code into the Ubercart Stock module and did some renaming ('uc_stock_return' to 'uc_stock').

AttachmentSizeStatusTest resultOperations
restore_stock_level_on_order_cancel_or_delete-129292-52.patch7.26 KBIdlePASSED: [[SimpleTest]]: [MySQL] 1,306 pass(es).View details | Re-test

#53

Category:bug report» feature request
Priority:critical» normal

Improved version of #52 attached, moving the order delete trigger to uc_order and rearranging other code. This also disables the new predicates by default; you can easily enable them, and existing stores may already have similar CAs in place. In 7.x we can probably enable them by default if this lands before 7.x-3.0.

AttachmentSizeStatusTest resultOperations
588746-stock-reincrement.patch7.03 KBIdlePASSED: [[SimpleTest]]: [MySQL] 1,302 pass(es).View details | Re-test

#54

Just a quick question, these patches are only for D6 and not D7 right? If that's the case, should I open a new issue for D7 or use this thread?

#55

We may as well use the same thread, if you can port this patch to D7 and Rules then please do so.

#56

will this problem be solved in the near future?

#57

Please test the patches and post your results here, that will help get this feature included in Ubercart.

#58

Status:needs review» reviewed & tested by the community

Patch #53 provides three new conditional actions:
- Increment stock on deleting an order
- Increment stock on cancelling order (setting its status to cancel)
- Decrement stock when order cancellation is being undone

I've tried them one by one and they work like a charm !

#59

Version:6.x-2.x-dev» 7.x-3.x-dev
Status:reviewed & tested by the community» patch (to be ported)

Committed #53 to 6.x, needs porting to 7.x.

#60

I don't see the changes on latest 6.x-2.x-dev.

#61

If you're using the snapshot you may need to wait another 12-24 hours for it to update. It is in the git repo, committed in two parts:

http://drupalcode.org/project/ubercart.git/commitdiff/271cd86
http://drupalcode.org/project/ubercart.git/commitdiff/d613c06

#62

Ported and committed the order deletion trigger to Rules in 7.x-3.x. The actual stock handling rules still need porting.

#63

OK, I'll wait... sorry to bother.

nobody click here