Page 1 of 1

Return Stock when Order Canceled

Posted: Sun Aug 14, 2011 2:40 am
by OpenM
Return Stock when Order Canceled ?


I have set a product to 3 pieces and made an order from the shop for 3 i recieved the order through admin then i cancelled the order from there checked the product quantiy its zero so the order for the whole quantity was 3 cancelled order no stock back doesn't the default should be restoring the quantity back as it was if orders cancelled?
i even deleted the whole order after cancellation still quantity zero and at the shop it shows out of stock.....

any suggestions to solve this??

Re: Return Stock when Order Canceled

Posted: Sun Aug 14, 2011 2:56 am
by i2Paq
OpenM wrote:any suggestions to solve this??
Used our FREE search?

Re: Return Stock when Order Canceled

Posted: Sun Aug 14, 2011 2:58 am
by opencartisalright
This is normal behavior of OpenCart (although I wish it wasn't).

I agree that when you cancel an order then it should replace the stock that was taken out. It should also subtract the amount from your total sales figures as well. As of now it doesn't do either. Not sure if it ever will. I don't think there is an extension for it yet.

Re: Return Stock when Order Canceled

Posted: Mon Aug 15, 2011 12:17 am
by Julio Cesar C G
I do not know if this is a bug or some implementation that Daniel has not yet activated, but the solution is (But it only works if the order is deleted):

in admin/model/sale/order.php find

Code: Select all

	public function deleteOrder($order_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 ($order_query->num_rows) {
				$product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");

				foreach($product_query->rows as $product) {
					$this->db->query("UPDATE `" . DB_PREFIX . "product` SET quantity = (quantity + " . (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 + " . (int)$product['quantity'] . ") WHERE product_option_value_id = '" . (int)$option['product_option_value_id'] . "' AND subtract = '1'");
					}
				}
			}
		}

		$this->db->query("DELETE FROM `" . DB_PREFIX . "order` WHERE order_id = '" . (int)$order_id . "'");
      	$this->db->query("DELETE FROM " . DB_PREFIX . "order_history WHERE order_id = '" . (int)$order_id . "'");
      	$this->db->query("DELETE FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
      	$this->db->query("DELETE FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "'");
	  	$this->db->query("DELETE FROM " . DB_PREFIX . "order_download WHERE order_id = '" . (int)$order_id . "'");
      	$this->db->query("DELETE FROM " . DB_PREFIX . "order_total WHERE order_id = '" . (int)$order_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "customer_transaction WHERE order_id = '" . (int)$order_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "customer_reward WHERE order_id = '" . (int)$order_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "affiliate_transaction WHERE order_id = '" . (int)$order_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "coupon_history WHERE order_id = '" . (int)$order_id . "'");
	}
replace with:

Code: Select all

	public function deleteOrder($order_id) {
		$order_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order` WHERE order_status_id > '0' AND order_id = '" . (int)$order_id . "'");
		
		if ($order_query->num_rows) {
			$product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");

			foreach($product_query->rows as $product) {
				$result = $this->db->query("SELECT subtract FROM " . DB_PREFIX . "product WHERE product_id = '" . (int)$product['product_id'] . "'");
				
				if($result->row['subtract']){
					$this->db->query("UPDATE `" . DB_PREFIX . "product` SET quantity = (quantity + " . (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 + " . (int)$product['quantity'] . ") WHERE product_option_value_id = '" . (int)$option['product_option_value_id'] . "' AND subtract = '1'");
					}
				}
			}
		}
		
		$this->db->query("DELETE FROM `" . DB_PREFIX . "order` WHERE order_id = '" . (int)$order_id . "'");
      	$this->db->query("DELETE FROM " . DB_PREFIX . "order_history WHERE order_id = '" . (int)$order_id . "'");
      	$this->db->query("DELETE FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
      	$this->db->query("DELETE FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "'");
	  	$this->db->query("DELETE FROM " . DB_PREFIX . "order_download WHERE order_id = '" . (int)$order_id . "'");
      	$this->db->query("DELETE FROM " . DB_PREFIX . "order_total WHERE order_id = '" . (int)$order_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "customer_transaction WHERE order_id = '" . (int)$order_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "customer_reward WHERE order_id = '" . (int)$order_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "affiliate_transaction WHERE order_id = '" . (int)$order_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "coupon_history WHERE order_id = '" . (int)$order_id . "'");
	}

Re: Return Stock when Order Canceled

Posted: Tue Apr 24, 2012 6:12 pm
by Alsaru
Thank you !

This code is working for me (1.5.1.3).

Re: Return Stock when Order Canceled

Posted: Tue Jan 29, 2013 11:08 pm
by bytekultur
Has there been any attempt to fix this? I think stock should be updated on order status change as well, and as there are different product options with a stock number, it might get complicated to do so...

Re: Return Stock when Order Canceled

Posted: Sun Apr 14, 2013 3:10 am
by Brite Light LEDs
mine does not have this code to replace ? please help

V15.2.1

Re: Return Stock when Order Canceled

Posted: Tue Oct 01, 2013 6:29 am
by Klimskady
Has this been rectified in 1.5.5.1? If not can someone post the updated code for this please, as its an important oversight that would be beneficial to most if not all store owners.

Re: Return Stock when Order Canceled

Posted: Tue Oct 01, 2013 2:12 pm
by i2Paq
Only when an order is deleted items will return to stock.

This is by design.

Re: Return Stock when Order Canceled

Posted: Tue Nov 19, 2013 11:14 pm
by garywinner
i2Paq wrote:Only when an order is deleted items will return to stock.

This is by design.
I am using 1.5.6 OC.
I don't find there is an status of "Delete".
Please advice.

Re: Return Stock when Order Canceled

Posted: Tue Nov 19, 2013 11:16 pm
by i2Paq
garywinner wrote:
i2Paq wrote:Only when an order is deleted items will return to stock.

This is by design.
I am using 1.5.6 OC.
I don't find there is an status of "Delete".
Please advice.
It is not a status: you must delete the order.

Re: Return Stock when Order Canceled

Posted: Tue Nov 19, 2013 11:23 pm
by garywinner
You are right O0
Deleting the order makes things work. Tho it is not the best way.
Thankyou so much
i2Paq wrote:
garywinner wrote:
i2Paq wrote:Only when an order is deleted items will return to stock.

This is by design.
I am using 1.5.6 OC.
I don't find there is an status of "Delete".
Please advice.
It is not a status: you must delete the order.

Re: Return Stock when Order Canceled

Posted: Wed May 14, 2014 5:08 am
by 308Sound
By design or no, I for one (several it would seem) do find it helpful to have records of customers who order, then cancel....during processing. It's a way for me to easily see if a customer has ordered and canceled in the past. IMO, stock should be allowed to be zeroed out and leave an empty order.