Location: PHPKode > projects > HikaShop > back/classes/order_product.php
<?php
/**
 * @package		HikaShop for Joomla!
 * @version		1.4.3
 * @author		hikashop.com
 * @copyright	(C) 2010 HIKARI SOFTWARE. All rights reserved.
 * @license		GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?>
<?php
class hikashopOrder_productClass extends hikashopClass{
	var $tables = array('order_product');
	var $pkeys = array('order_product_id');
	function save(&$products){
		if(!empty($products)){
			$items = array();
			$updates = array();
			$discounts = array();
			foreach($products as $product){
				$items[] = '('.(int)$product->order_id.','.(int)$product->product_id.','.(int)$product->order_product_quantity.','.$this->database->Quote($product->order_product_name).','.$this->database->Quote($product->order_product_code).','.$this->database->Quote($product->order_product_price).','.(int)$product->order_product_tax.','.$this->database->Quote($product->order_product_options).')';
				$updates[$product->order_product_quantity][] = (int)$product->product_id;
				if(!empty($product->discount)){
					if(empty($discounts[$product->discount->discount_code])){
						$discounts[$product->discount->discount_code] = 0;
					}
					$discounts[$product->discount->discount_code] += (int)$product->order_product_quantity;
				}
			}
			foreach($updates as $k => $update){
				$query = 'UPDATE '.hikashop::table('product').' SET product_quantity = product_quantity - '.(int)$k.' WHERE product_id IN ('.implode(',',$update).') AND product_quantity > -1';
				$this->database->setQuery($query);
				$this->database->query();
			}
			$query='INSERT IGNORE INTO '.hikashop::table('order_product').' (order_id,product_id,order_product_quantity,order_product_name,order_product_code,order_product_price,order_product_tax,order_product_options) VALUES ';
			$query.=implode(',',$items);
			$this->database->setQuery($query);
			$this->database->query();
			if(!empty($discounts)){
				$discountUpdates = array();
				foreach($discounts as $code => $qty){
					$discountUpdates[$qty][]=$this->database->Quote($code);
				}
				foreach($discountUpdates as $k => $update){
					$query = 'UPDATE '.hikashop::table('discount').' SET discount_used_times = discount_used_times + '.(int)$k.' WHERE discount_code IN ('.implode(',',$update).')';
					$this->database->setQuery($query);
					$this->database->query();
				}
			}
		}
		return true;
	}
	function cancelProductReservation($order_id){
		$query='SELECT * FROM '.hikashop::table('order_product').' WHERE order_id='.(int)$order_id;
		$this->database->setQuery($query);
		$items = $this->database->loadObjectList();
		if(!empty($items)){
			$updates = array();
			foreach($items as $item){
				$updates[$item->order_product_quantity][] = (int)$item->product_id;
			}
			foreach($updates as $k => $update){
				$query = 'UPDATE '.hikashop::table('product').' SET product_quantity = product_quantity + '.(int)$k.' WHERE product_id IN ('.implode(',',$update).') AND product_quantity > -1';
				$this->database->setQuery($query);
				$this->database->query();
			}
		}
	}
	function update(&$product){
		$old = $this->get($product->order_product_id);
		if(!empty($old->product_id) && $old->order_product_quantity!=$product->order_product_quantity){
			$k = $product->order_product_quantity-$old->order_product_quantity;
			$query = 'UPDATE '.hikashop::table('product').' SET product_quantity = product_quantity - '.$k.' WHERE product_id='.$old->product_id.' AND product_quantity > -1';
			$this->database->setQuery($query);
			$this->database->query();
		}
		if(empty($product->order_product_quantity)){
			return $this->delete($product->order_product_id);
		}
		$product->order_product_id = parent::save($product);
		return $product->order_product_id;
	}
}
Return current item: HikaShop