Hi,
I've made some changes to uc_stocklevels_order to detect Order status changes and increment/decrement stock.
Now it works for me with paypal payements.
Know bugs:
- It can't detect if we remove a product from an Order product list. But it works well if we change qty or if we remove the complete order.
- I do some test with credit card test gateway. And wen a customer completes an order the order status changes from "Complete" to "pending" and the stock gets decremented in the first change but it not decrement in the second. With paypal the order never become complete if we don't do the payement so it works well.
<?php
function uc_stocklevels_order($op, &$order, $arg2 = null)
{
static $load_time_products = array();
static $load_status;
// Order status that not affect stock
$stat_pending = array("pending","paypal_pending","canceled", "in_checkout");
// Order status that increment/decrement Stock
$stat_completed = array("payment_received","processing","completed" );
switch($op)
{
case 'delete':
if (in_array($order->order_status, $stat_completed))
foreach($order->products as $product)
uc_stocklevels_adjust_stock($product->model, $product->qty);
break;
case 'load':
$load_time_products = $order->products; // remember to check qty when 'save'ed
// Status change
if ($load_status and $order->order_status and $load_status != $order->order_status){
// decrement stock
if (in_array($load_status, $stat_pending) and in_array($order->order_status, $stat_completed)){
foreach($order->products as $product)
uc_stocklevels_adjust_stock($product->model, 0 - $product->qty);
$load_status = $order->order_status;
} else
// Increment stock
if (in_array($load_status, $stat_completed) and in_array($order->order_status, $stat_pending)) {
foreach($order->products as $product)
uc_stocklevels_adjust_stock($product->model, $product->qty);
$load_status = $order->order_status;
}
}
break;
case 'save':
if (in_array($order->order_status, $stat_completed)) {
foreach($order->products as $new) {
$pmatch = FALSE;
foreach($load_time_products as $old) {
if($old->model == $new->model and $old->qty != $new->qty){
uc_stocklevels_adjust_stock($new->model, $old->qty - $new->qty);
$pmatch = TRUE;
}
}
// Decrement stock if we add a product to an existing order
if ( ! $pmatch) uc_stocklevels_adjust_stock($new->model, 0 - $new->qty);
}
}
break;
case 'update':
// Store status for status change detection
$load_status = $order->order_status;
}
}
?>Joan.



Joined: 06/04/2008