PHP Code:
function create_add_products($zf_insert_id, $zf_mode = false) {
global $db, $currencies, $order_total_modules, $order_totals, $zco_notifier;
// initialized for the email confirmation
$this->products_ordered = '';
$this->products_ordered_html = '';
$this->subtotal = 0;
$this->total_tax = 0;
// lowstock email report
$this->email_low_stock='';
for ($i=0, $n=sizeof($this->products); $i<$n; $i++) {
// Stock Update - Joao Correia
if (STOCK_LIMITED == 'true') {
if (DOWNLOAD_ENABLED == 'true') {
$stock_query_raw = "select p.products_quantity, pad.products_attributes_filename, p.product_is_always_free_shipping
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 = '" . zen_get_prid($this->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 = $this->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_values = $db->Execute($stock_query_raw);
} else {
$stock_values = $db->Execute("select * from " . TABLE_PRODUCTS . " where products_id = '" . zen_get_prid($this->products[$i]['id']) . "'");
}
$zco_notifier->notify('NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_BEGIN');
if ($stock_values->RecordCount() > 0) {
// do not decrement quantities if products_attributes_filename exists
if ((DOWNLOAD_ENABLED != 'true') || $stock_values->fields['product_is_always_free_shipping'] == 2 || (!$stock_values->fields['products_attributes_filename']) ) {
$stock_left = $stock_values->fields['products_quantity'] - $this->products[$i]['qty'];
$this->products[$i]['stock_reduce'] = $this->products[$i]['qty'];
} else {
$stock_left = $stock_values->fields['products_quantity'];
}
// $this->products[$i]['stock_value'] = $stock_values->fields['products_quantity'];
$db->Execute("update " . TABLE_PRODUCTS . " set products_quantity = '" . $stock_left . "' where products_id = '" . zen_get_prid($this->products[$i]['id']) . "'");
// if ( ($stock_left < 1) && (STOCK_ALLOW_CHECKOUT == 'false') ) {
if ($stock_left <= 0) {
// only set status to off when not displaying sold out
if (SHOW_PRODUCTS_SOLD_OUT == '0') {
$db->Execute("update " . TABLE_PRODUCTS . " set products_status = 0 where products_id = '" . zen_get_prid($this->products[$i]['id']) . "'");
}
}
// for low stock email
if ( $stock_left <= STOCK_REORDER_LEVEL ) {
// WebMakers.com Added: add to low stock email
$this->email_low_stock .= 'ID# ' . zen_get_prid($this->products[$i]['id']) . "\t\t" . $this->products[$i]['model'] . "\t\t" . $this->products[$i]['name'] . "\t\t" . ' Qty Left: ' . $stock_left . "\n";
}
}
}
// Update products_ordered (for bestsellers list)
// $db->Execute("update " . TABLE_PRODUCTS . " set products_ordered = products_ordered + " . sprintf('%d', $order->products[$i]['qty']) . " where products_id = '" . zen_get_prid($order->products[$i]['id']) . "'");
$db->Execute("update " . TABLE_PRODUCTS . " set products_ordered = products_ordered + " . sprintf('%f', $this->products[$i]['qty']) . " where products_id = '" . zen_get_prid($this->products[$i]['id']) . "'");
$zco_notifier->notify('NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_END');
$sql_data_array = array('orders_id' => $zf_insert_id,
'products_id' => zen_get_prid($this->products[$i]['id']),
'products_model' => $this->products[$i]['model'],
'products_name' => $this->products[$i]['name'],
'products_price' => $this->products[$i]['price'],
'final_price' => $this->products[$i]['final_price'],
'onetime_charges' => $this->products[$i]['onetime_charges'],
'products_tax' => $this->products[$i]['tax'],
'products_quantity' => $this->products[$i]['qty'],
'products_priced_by_attribute' => $this->products[$i]['products_priced_by_attribute'],
'product_is_free' => $this->products[$i]['product_is_free'],
'products_discount_type' => $this->products[$i]['products_discount_type'],
'products_discount_type_from' => $this->products[$i]['products_discount_type_from'],
'products_prid' => $this->products[$i]['id']);
zen_db_perform(TABLE_ORDERS_PRODUCTS, $sql_data_array);
$order_products_id = $db->Insert_ID();
$zco_notifier->notify('NOTIFY_ORDER_PROCESSING_CREDIT_ACCOUNT_UPDATE_BEGIN');
$order_total_modules->update_credit_account($i);//ICW ADDED FOR CREDIT CLASS SYSTEM
$zco_notifier->notify('NOTIFY_ORDER_PROCESSING_ATTRIBUTES_BEGIN');
//------ bof: insert customer-chosen options to order--------
$attributes_exist = '0';
$this->products_ordered_attributes = '';
if (isset($this->products[$i]['attributes'])) {
$attributes_exist = '1';
for ($j=0, $n2=sizeof($this->products[$i]['attributes']); $j<$n2; $j++) {
if (DOWNLOAD_ENABLED == 'true') {
$attributes_query = "select popt.products_options_name, poval.products_options_values_name,
pa.options_values_price, pa.price_prefix,
pa.product_attribute_is_free, pa.products_attributes_weight, pa.products_attributes_weight_prefix,
pa.attributes_discounted, pa.attributes_price_base_included, pa.attributes_price_onetime,
pa.attributes_price_factor, pa.attributes_price_factor_offset,
pa.attributes_price_factor_onetime, pa.attributes_price_factor_onetime_offset,
pa.attributes_qty_prices, pa.attributes_qty_prices_onetime,
pa.attributes_price_words, pa.attributes_price_words_free,
pa.attributes_price_letters, pa.attributes_price_letters_free,
pad.products_attributes_maxdays, pad.products_attributes_maxcount, pad.products_attributes_filename,
pa.product_attribute_is_free, pa.attributes_discounted
from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_OPTIONS_VALUES . " poval, " .
TABLE_PRODUCTS_ATTRIBUTES . " pa
left join " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " pad
on pa.products_attributes_id=pad.products_attributes_id
where pa.products_id = '" . zen_db_input($this->products[$i]['id']) . "'
and pa.options_id = '" . $this->products[$i]['attributes'][$j]['option_id'] . "'
and pa.options_id = popt.products_options_id
and pa.options_values_id = '" . $this->products[$i]['attributes'][$j]['value_id'] . "'
and pa.options_values_id = poval.products_options_values_id
and popt.language_id = '" . $_SESSION['languages_id'] . "'
and poval.language_id = '" . $_SESSION['languages_id'] . "'";
$attributes_values = $db->Execute($attributes_query);
} else {
$attributes_values = $db->Execute("select popt.products_options_name, poval.products_options_values_name,
pa.options_values_price, pa.price_prefix,
pa.product_attribute_is_free, pa.products_attributes_weight, pa.products_attributes_weight_prefix,
pa.attributes_discounted, pa.attributes_price_base_included, pa.attributes_price_onetime,
pa.attributes_price_factor, pa.attributes_price_factor_offset,
pa.attributes_price_factor_onetime, pa.attributes_price_factor_onetime_offset,
pa.attributes_qty_prices, pa.attributes_qty_prices_onetime,
pa.attributes_price_words, pa.attributes_price_words_free,
pa.attributes_price_letters, pa.attributes_price_letters_free
from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_OPTIONS_VALUES . " poval, " . TABLE_PRODUCTS_ATTRIBUTES . " pa
where pa.products_id = '" . (int)$this->products[$i]['id'] . "' and pa.options_id = '" . (int)$this->products[$i]['attributes'][$j]['option_id'] . "' and pa.options_id = popt.products_options_id and pa.options_values_id = '" . (int)$this->products[$i]['attributes'][$j]['value_id'] . "' and pa.options_values_id = poval.products_options_values_id and popt.language_id = '" . $_SESSION['languages_id'] . "' and poval.language_id = '" . $_SESSION['languages_id'] . "'");
}
//clr 030714 update insert query. changing to use values form $order->products for products_options_values.
$sql_data_array = array('orders_id' => $zf_insert_id,
'orders_products_id' => $order_products_id,
'products_options' => $attributes_values->fields['products_options_name'],
// 'products_options_values' => $attributes_values->fields['products_options_values_name'],
'products_options_values' => $this->products[$i]['attributes'][$j]['value'],
'options_values_price' => $attributes_values->fields['options_values_price'],
'price_prefix' => $attributes_values->fields['price_prefix'],
'product_attribute_is_free' => $attributes_values->fields['product_attribute_is_free'],
'products_attributes_weight' => $attributes_values->fields['products_attributes_weight'],
'products_attributes_weight_prefix' => $attributes_values->fields['products_attributes_weight_prefix'],
'attributes_discounted' => $attributes_values->fields['attributes_discounted'],
'attributes_price_base_included' => $attributes_values->fields['attributes_price_base_included'],
'attributes_price_onetime' => $attributes_values->fields['attributes_price_onetime'],
'attributes_price_factor' => $attributes_values->fields['attributes_price_factor'],
'attributes_price_factor_offset' => $attributes_values->fields['attributes_price_factor_offset'],
'attributes_price_factor_onetime' => $attributes_values->fields['attributes_price_factor_onetime'],
'attributes_price_factor_onetime_offset' => $attributes_values->fields['attributes_price_factor_onetime_offset'],
'attributes_qty_prices' => $attributes_values->fields['attributes_qty_prices'],
'attributes_qty_prices_onetime' => $attributes_values->fields['attributes_qty_prices_onetime'],
'attributes_price_words' => $attributes_values->fields['attributes_price_words'],
'attributes_price_words_free' => $attributes_values->fields['attributes_price_words_free'],
'attributes_price_letters' => $attributes_values->fields['attributes_price_letters'],
'attributes_price_letters_free' => $attributes_values->fields['attributes_price_letters_free'],
'products_options_id' => (int)$this->products[$i]['attributes'][$j]['option_id'],
'products_options_values_id' => (int)$this->products[$i]['attributes'][$j]['value_id'],
'products_prid' => $this->products[$i]['id']
);
zen_db_perform(TABLE_ORDERS_PRODUCTS_ATTRIBUTES, $sql_data_array);
if ((DOWNLOAD_ENABLED == 'true') && isset($attributes_values->fields['products_attributes_filename']) && zen_not_null($attributes_values->fields['products_attributes_filename'])) {
$sql_data_array = array('orders_id' => $zf_insert_id,
'orders_products_id' => $order_products_id,
'orders_products_filename' => $attributes_values->fields['products_attributes_filename'],
'download_maxdays' => $attributes_values->fields['products_attributes_maxdays'],
'download_count' => $attributes_values->fields['products_attributes_maxcount'],
'products_prid' => $this->products[$i]['id']
);
zen_db_perform(TABLE_ORDERS_PRODUCTS_DOWNLOAD, $sql_data_array);
}
//clr 030714 changing to use values from $orders->products and adding call to zen_decode_specialchars()
// $this->products_ordered_attributes .= "\n\t" . $attributes_values->fields['products_options_name'] . ' ' . $attributes_values->fields['products_options_values_name'];
$this->products_ordered_attributes .= "\n\t" . $attributes_values->fields['products_options_name'] . ' ' . zen_decode_specialchars($this->products[$i]['attributes'][$j]['value']);
//CUSTOM SERIAL SCRIPT CALL
$this->products_ordered_attributes .= "\n\tProduct Setial: " . getSetial($this->products[$i]['id']);
}
}
//------eof: insert customer-chosen options ----
// update totals counters
$this->total_weight += ($this->products[$i]['qty'] * $this->products[$i]['weight']);
$this->total_tax += zen_calculate_tax($total_products_price, $products_tax) * $this->products[$i]['qty'];
$this->total_cost += $total_products_price;
$zco_notifier->notify('NOTIFY_ORDER_PROCESSING_ONE_TIME_CHARGES_BEGIN');
// build output for email notification
$this->products_ordered .= $this->products[$i]['qty'] . ' x ' . $this->products[$i]['name'] . ($this->products[$i]['model'] != '' ? ' (' . $this->products[$i]['model'] . ') ' : '') . ' = ' .
$currencies->display_price($this->products[$i]['final_price'], $this->products[$i]['tax'], $this->products[$i]['qty']) .
($this->products[$i]['onetime_charges'] !=0 ? "\n" . TEXT_ONETIME_CHARGES_EMAIL . $currencies->display_price($this->products[$i]['onetime_charges'], $this->products[$i]['tax'], 1) : '') .
$this->products_ordered_attributes . "\n";
$this->products_ordered_html .=
'<tr>' .
'<td class="product-details" align="right" valign="top" width="30">' . $this->products[$i]['qty'] . ' x</td>' .
'<td class="product-details" valign="top">' . $this->products[$i]['name'] . ($this->products[$i]['model'] != '' ? ' (' . $this->products[$i]['model'] . ') ' : '') .
'<nobr><small><em> '. $this->products_ordered_attributes .'</em></small></nobr></td>' .
'<td class="product-details-num" valign="top" align="right">' .
$currencies->display_price($this->products[$i]['final_price'], $this->products[$i]['tax'], $this->products[$i]['qty']) .
($this->products[$i]['onetime_charges'] !=0 ?
'</td></tr><tr><td class="product-details">' . TEXT_ONETIME_CHARGES_EMAIL . '</td>' .
'<td>' . $currencies->display_price($this->products[$i]['onetime_charges'], $this->products[$i]['tax'], 1) : '') .
'</td></tr>';
}
$order_total_modules->apply_credit();//ICW ADDED FOR CREDIT CLASS SYSTEM
}
this is my code inside the function