Jump to content
Sign in to follow this  
mdeloach

Change stock level when order status is shipped

Recommended Posts

Hello,

 

I have run into a bit of a problem with a client site, and I'm wondering if anyone knew of a fix or workaround for this....

 

Currently the way OScommerce works is that when a customer completes and order and submits it, the stock level for those particular products will change.

 

What I need to do is change the stock level when changing the status of the order in the Admin section of oscommerce (Customers/Orders) to "shipped"... Then, and then only, I would like for the stock level to subtract for those particular products.

 

Does anyone out there possibly know how to change this? I couldn't find a contribution much less a thread where this was addressed.

 

Would appreciate any help,

 

Matt

Share this post


Link to post
Share on other sites

That may be a mistake to do...That leaves you wide open to sell an item that is no longer in stock....

You may want to rethink what you ask for...

 

 

Mike

Share this post


Link to post
Share on other sites

You would need to take the stock update section out of catalog/checkout_process.php and put it in catalog/admin/orders.php

 

This adjust the quantity when changing the status from pending to processing.

 

Mainly I do it this way because all the items are quantity 1 and this eliminates fraud orders from depleting the stock.

 

case 'update_order':

...


if ($status == 2 && $check_status['orders_status'] == 1) {
           $order = new order($oID);

           for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
             if (STOCK_LIMITED == 'true') {
               if (DOWNLOAD_ENABLED == 'true') {
                 $stock_query_raw = "SELECT products_quantity, pad.products_attributes_filename
                           FROM " . TABLE_PRODUCTS . " p
                           LEFT JOIN " . TABLE_PRODUCTS_ATTRIBUTES . " pa
                            ON p.products_id=pa.products_id
                           LEFT JOIN " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " pad
                            ON pa.products_attributes_id=pad.products_attributes_id
                           WHERE p.products_id = '" . tep_get_prid($order->products[$i]['id']) . "'";
                 // Will work with only one option for downloadable products
                 // otherwise, we have to build the query dynamically with a loop
                 $products_attributes = $order->products[$i]['attributes'];
                 if (is_array($products_attributes)) {
                   $stock_query_raw .= " AND pa.options_id = '" . $products_attributes[0]['option_id'] . "' AND pa.options_values_id = '" . $products_attributes[0]['value_id'] . "'";
                 }
                 $stock_query = tep_db_query($stock_query_raw);
               } else {
                 $stock_query = tep_db_query("select products_quantity from " . TABLE_PRODUCTS . " where products_id = '" . tep_get_prid($order->products[$i]['id']) . "'");
               }
               if (tep_db_num_rows($stock_query) > 0) {
                 $stock_values = tep_db_fetch_array($stock_query);
                 // do not decrement quantities if products_attributes_filename exists
                 if ((DOWNLOAD_ENABLED != 'true') || (!$stock_values['products_attributes_filename'])) {
                   $stock_left = $stock_values['products_quantity'] - $order->products[$i]['qty'];
                 } else {
                   $stock_left = $stock_values['products_quantity'];
                 }
                 tep_db_query("update " . TABLE_PRODUCTS . " set products_quantity = '" . $stock_left . "' where products_id = '" . tep_get_prid($order->products[$i]['id']) . "'");
                 if ( ($stock_left < 1) && (STOCK_ALLOW_CHECKOUT == 'false') ) {
                   tep_db_query("update " . TABLE_PRODUCTS . " set products_status = '0' where products_id = '" . tep_get_prid($order->products[$i]['id']) . "'");
                 }
               }
             }
           }
         }

Share this post


Link to post
Share on other sites

This is exactly what I was looking for. Can't thank you enough. I'll try to implement it tomorrow and let you know if I have any trouble.....Appreciate it!

Share this post


Link to post
Share on other sites

The code in checkout_process.php differers from what you seem to have:

 

  for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
// Stock Update - Joao Correia
   if (STOCK_LIMITED == 'true') {
     if (DOWNLOAD_ENABLED == 'true') {
       $stock_query_raw = "SELECT products_quantity, pad.products_attributes_filename 
                           FROM " . TABLE_PRODUCTS . " p
                           LEFT JOIN " . TABLE_PRODUCTS_ATTRIBUTES . " pa
                            ON p.products_id=pa.products_id
                           LEFT JOIN " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " pad
                            ON pa.products_attributes_id=pad.products_attributes_id
                           WHERE p.products_id = '" . tep_get_prid($order->products[$i]['id']) . "'";
// Will work with only one option for downloadable products
// otherwise, we have to build the query dynamically with a loop
       $products_attributes = $order->products[$i]['attributes'];
       if (is_array($products_attributes)) {
         $stock_query_raw .= " AND pa.options_id = '" . $products_attributes[0]['option_id'] . "' AND pa.options_values_id = '" . $products_attributes[0]['value_id'] . "'";
       }
       $stock_query = tep_db_query($stock_query_raw);
     } else {
       $stock_query = tep_db_query("select products_quantity from " . TABLE_PRODUCTS . " where products_id = '" . tep_get_prid($order->products[$i]['id']) . "'");
     }
     if (tep_db_num_rows($stock_query) > 0) {
       $stock_values = tep_db_fetch_array($stock_query);
// do not decrement quantities if products_attributes_filename exists
       if ((DOWNLOAD_ENABLED != 'true') || (!$stock_values['products_attributes_filename'])) {
         $stock_left = $stock_values['products_quantity'] - $order->products[$i]['qty'];
       } else {
         $stock_left = $stock_values['products_quantity'];
       }
       tep_db_query("update " . TABLE_PRODUCTS . " set products_quantity = '" . $stock_left . "' where products_id = '" . tep_get_prid($order->products[$i]['id']) . "'");
       if ( ($stock_left < 1) && (STOCK_ALLOW_CHECKOUT == 'false') ) {
         tep_db_query("update " . TABLE_PRODUCTS . " set products_status = '0' where products_id = '" . tep_get_prid($order->products[$i]['id']) . "'");
       }
     }
   }

 

 

 

Also, in "orders.php" my code differs from yours as well

 

        if ( ($check_status['orders_status'] != $status) || tep_not_null($comments)) {
         tep_db_query("update " . TABLE_ORDERS . " set orders_status = '" . tep_db_input($status) . "', last_modified = now() where orders_id = '" . (int)$oID . "'");

 

 

I tried to remove the above in checkout_process.php and add it to orders.php to no avail. Hope I don't sound too ignorant, but I'm wondering if you or anyone might be able to help.

 

Thanks again,

 

Matt

Share this post


Link to post
Share on other sites

This is so it only updates when you change the status from 1 to 2.

 

if ($status == 2 && $check_status['orders_status'] == 1) {
 $order = new order($oID);

 Stock update code from checkout_process.php

}

 

This doesnt exist in orders.php so you have to add it to orders.php in the case 'update_order': section and then remove it from checkout_process.php

Share this post


Link to post
Share on other sites

I get this error when following ur instructions above :

 

Fatal error: Cannot instantiate non-existent class: order in /data/web/com.roystontong/dvd/catalog/admin/orders.php on line 71

 

line 71 says:

 

$order = new order($oID);

 

I tried require(DIR_WS_CLASSES . 'order.php'); but that doesn't work. It doesnt like the order.php file in the admin/classes folder.

 

So how did u guys fix this? Cause im sure u guys had the same problem.

 

Thanks


I'm an Idiot !

Share this post


Link to post
Share on other sites

This was annoying for me too. There is already a

require(DIR_WS_CLASSES . 'order.php');

later in the file: move it to the top of the file

Share this post


Link to post
Share on other sites

I like this idea .. but spent hours trying to get it to work with no effect. It will not adjust the product quantity .. no matter what I do with the code.

 

I hope someone with better php skills than me can release this as a mod.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×