Page 1 of 1

Added support to reintegrate product on cancelled orders

Posted: Tue Nov 17, 2009 6:11 pm
by lenna
if you have config_stock_subtract enable, you may need to reintegrate/charge back your stock when orders reach some statuses (as cancelled, chargeback, denied and so);
i tweak a little admin/model/customer/order.php to chargeback the stock of products once the status is changed to one of my choice (and of course the previous status didnt already made the chargeback)

to enable it
open admin/model/customer/order.php
search for this function: public function editOrder
and paste this code at the beginning of function, jsut before $this->db->query("UPDATE `" . DB_PREFIX . "order`....

at the moment the status that chargeback are hardcoded into $backarr, i will look forward to put a flag in the order_status edit (even it this means adding a db column)
for the moment just fill the $backarr with the order_status_id that you choose

hope this could be useful

Code: Select all

		/*CHARGE BACK*/
		$backarr=array(7,8,11,12,13); // these are the status that requires a chargeback (change as you need, you can grab these code from order_status table (order_status_id)
		if (in_array((int)$data['order_status_id'], $backarr)) {
			$prev_st=$this->db->query("SELECT order_status_id from " . DB_PREFIX . "order where order_id= '" .  (int)$order_id . "'");
			echo $prev_st->row['order_status_id'];
			if ($this->config->get('config_stock_subtract')) {
				if (!in_array($prev_st->row['order_status_id'], $backarr)) {
					$order_product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
					print_r($order_product_query);
					
					foreach ($order_product_query->rows as $result) {
					   $this->db->query("UPDATE " . DB_PREFIX . "product SET quantity = (quantity + " . (int)$result['quantity'] . ") WHERE product_id = '" . (int)$result['product_id'] . "'");
					}
				}
			}
		}

Re: Added support to reintegrate product on cancelled orders

Posted: Tue Nov 17, 2009 6:13 pm
by i2Paq
This is verry usefull and should be default in OC.
This function is missing in many carts, osCommerce does have this function.

I have added this to the Feature Requests topic.

Re: Added support to reintegrate product on cancelled orders

Posted: Tue Nov 17, 2009 8:19 pm
by Qphoria
This already exists in the core for "deleted" orders. But I guess it could be good for canceled as well

Re: Added support to reintegrate product on cancelled orders

Posted: Tue Nov 17, 2009 9:05 pm
by i2Paq
Qphoria wrote:This already exists in the core for "deleted" orders. But I guess it could be good for canceled as well
How do I see if the canceled orders restock?

Re: Added support to reintegrate product on cancelled orders

Posted: Tue Nov 17, 2009 11:00 pm
by Qphoria
it doesn't.. thats why i said it could be good to add for canceled/chargeback/denied as demonstrated in this post. It currently only works for deleted orders.

Re: Added support to reintegrate product on cancelled orders

Posted: Tue Nov 17, 2009 11:12 pm
by i2Paq
Qphoria wrote:it doesn't.. thats why i said it could be good to add for canceled/chargeback/denied as demonstrated in this post. It currently only works for deleted orders.
So if I delete an order it restocks without showing a notification except for""you modified your orders succesfully"?

Edit: it does!

Re: Added support to reintegrate product on cancelled orders

Posted: Wed Jan 20, 2010 9:44 pm
by DEHiCKA
lenna tweak doesn't affect options quantity. Here is my version, that changes options Qty as well.
And it works in both directions. So you can change order satus from cancelled back to one of the active states and it subtract products and options from the stock again.
Just add to the beginning of the editOrder function in order.php:

Code: Select all

// DEHiCKA - add CHARGE BACK
    $backarr=array(7,8,11,12,13); // these are the status that requires a chargeback (change as you need, you can grab these code from order_status table (order_status_id)
	if ($this->config->get('config_stock_subtract')) {
		$order_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order` WHERE order_status_id > '0' AND order_id = '" . (int)$order_id . "'");

		if (in_array((int)$data['order_status_id'], $backarr) != in_array($order_query->row['order_status_id'], $backarr) && $order_query->num_rows) { // status chargeback flag has changed
			$product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
			if (in_array((int)$data['order_status_id'], $backarr)) $charge_dir = '+'; else $charge_dir = '-';
		   		
			foreach($product_query->rows as $product) {
					$this->db->query("UPDATE `" . DB_PREFIX . "product` SET quantity = (quantity ". $charge_dir. " " . (int)$product['quantity'] . ") WHERE product_id = '" . (int)$product['product_id'] . "'");
					
					$option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . (int)$product['order_product_id'] . "'");
				
					foreach ($option_query->rows as $option) {
						$this->db->query("UPDATE " . DB_PREFIX . "product_option_value SET quantity = (quantity ". $charge_dir. " " . (int)$product['quantity'] . ") WHERE product_option_value_id = '" . (int)$option['product_option_value_id'] . "' AND subtract = '1'");
					}				
				}
			}
		}
// DEHiCKA - end
There are still some bad logic in the restocking deleted orders: completed and cancelled orders must not restock when deleted.
It can be done easily by modifying deleteOrder function in order.php - add after 66th line ($order_query = $this->db->query(.... ):

Code: Select all

// DEHiCKA - mod CHARGE BACK
		    $backarr=array(7,8,11,12,13,5); // these are the status that DOES NOT requires a chargeback on delete (change as you need, you can grab these code from order_status table (order_status_id)
			if (!in_array($order_query->row['order_status_id'], $backarr))
// DEHiCKA - end CHARGE BACK

Re: Added support to reintegrate product on cancelled orders

Posted: Sat Feb 04, 2012 6:28 pm
by Photospirit
Hi,
will this code also work for 1.5.3.1? And if yes, in which file do I have to change it?
Many thanks, chris

Re: Added support to reintegrate product on cancelled orders

Posted: Tue Jan 15, 2013 11:37 am
by topfuel75
I have made a vQmod for this. It is based on the code from DEHiCKA, but I havent include the deleteOrder function.
I have only tried it on 1.5.4.1, and it seems to work fine.

You will be able to return the stock tru the pages sale/order/update and sale/order/info when you change the order status.

Be sure to edit $backarr in the vQmod file so the order status id's will fit you need. $backarr is located in two places in the vQmod file.

Please let me know how it works out for you.

Cheers,
Fredrik

Re: Added support to reintegrate product on cancelled orders

Posted: Wed Dec 24, 2014 4:13 am
by BriteLightLEDs2014
can i talk to you about this code, love to get working with my store, it used option to products extension also , i will pay ;D