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

flokli - September 26, 2009 - 14:59
Project:Ubercart
Version:6.x-2.0
Component:Code
Category:bug report
Priority:critical
Assigned:Unassigned
Status:active
Issue tags:uc_stock
Description

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.

#1

Simon Georges - November 8, 2009 - 14:18
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

Danny_Joris - November 27, 2009 - 18:43

subscribing

#3

Danny_Joris - November 27, 2009 - 18:55
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.

 
 

Drupal is a registered trademark of Dries Buytaert.