<modification>

	<id>Single Temp Order Id 2.0 - Dynamic Update Query String</id>
	<version>2.0.0</version>
	<vqmver>2.4.1</vqmver>
	<author>Qphoria</author>

	
	<file name="catalog/model/checkout/order.php">
			
		<operation info="wrap the original with the else" error="log">
            <search position="before"><![CDATA[
			$this->db->query("INSERT INTO `" . DB_PREFIX . "order`
            ]]></search>
            <add trim="false"><![CDATA[
		//Q: Prevent generating new temp order each time confirmation page loads.
		if (!isset($this->session->data['order_id'])) {
            ]]></add>
        </operation>
		
		<operation info="wrap the original with the else" error="log">
            <search position="after"><![CDATA[
			$order_id = $this->db->getLastId();
            ]]></search>
            <add trim="false"><![CDATA[
			} else {
			
				$query = $this->db->query("SELECT order_id FROM `" . DB_PREFIX . "order` WHERE order_status_id = 0 AND order_id = '" . (int)$this->session->data['order_id'] . "'");
				if ($query->num_rows) {
					$order_id = $this->session->data['order_id'];

					// Build Query string from $data. Assumes all "key" names are the db column names
					$ignoredCols = array(
						'products',
						'vouchers',
						'totals',
					);

					$query_str = "";
					foreach ($data  as $k => $v) {
						if (in_array($k, $ignoredCols)) { continue; }
						if (is_array($v)) { continue; }						
						// Check if column exists first, then add to the query string.
						$col_exists_query = $this->db->query("DESC `".DB_PREFIX."order` $k");
						if (!$col_exists_query->num_rows) { continue; }
						$query_str .= "$k = '".$this->db->escape($v)."', ";
					}

					$this->db->query("UPDATE `" . DB_PREFIX . "order` SET $query_str date_added = NOW(), date_modified = NOW() 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 . "'");
					$tbl_exists_query = $this->db->query("SHOW TABLES LIKE '".DB_PREFIX."order_voucher'");
					if ($tbl_exists_query->num_rows) {
						$this->db->query("DELETE FROM " . DB_PREFIX . "order_voucher WHERE order_id = '" . (int)$order_id . "'");
					}
					$this->db->query("DELETE FROM " . DB_PREFIX . "order_total WHERE order_id = '" . (int)$order_id . "'");
				}
			}//
			
            ]]></add>
        </operation>

		
	</file>

</modification>