Follow up:
This is the function I currently have to get product quantity:
Note: I used a function I have in my class to turn the result from db query into string, but if you want to make use of the function below you can easily use a loop to do so.
@Kuroi: can you have a look and tell me if there is anything wrong?
I used the option text name and value instead of the ids because I want to make sure that we either the stock of the exact product and attributes or nothing.
PHP Code:
<?php
function retrieve_attribute_stock_from_order($attribute_array,$products_id){
global $db;
// Lets assume that if table TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK is defined, then we are using the stock by attribute mod
// we can also query database to check if we want to
if(defined('TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK')){
$sql_query = 'SELECT products_attributes_id FROM '.TABLE_PRODUCTS_ATTRIBUTES.' pa,'.TABLE_PRODUCTS_OPTIONS_VALUES.' ov,'.TABLE_PRODUCTS_OPTIONS.' o
WHERE ';
$condition_array = array();
if(count($attribute_array) == 0) return;
foreach($attribute_array as $attribute){
$condition_array[] = '(ov.products_options_values_name = \''.$attribute['value'].'\' AND o.products_options_name =\''.$attribute['option'].'\' AND pa.products_id = '.$products_id.' AND pa.options_id = o.products_options_id AND pa.options_values_id = ov.products_options_values_id)';
}
$sql_query .= implode(' OR ', $condition_array).' ORDER BY products_attributes_id';
$result = $db->Execute($sql_query);
$yobj = new yclass();
$attribute_string = $yobj->db_result_to_string(',',$result);
$get_quantity_query = 'SELECT quantity FROM ' . TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK . ' where products_id="' . $products_id . '" and stock_attributes="' . $attribute_string . '" LIMIT 1';
$quantity = $db->Execute($get_quantity_query);
if($quantity->RecordCount() == 1)
return $quantity->fields['quantity'];
}
// get products_quantity directly from products table
$get_quantity_query = 'SELECT products_quantity FROM ' . TABLE_PRODUCTS . ' where products_id="' . $products_id . '" LIMIT 1';
$quantity = $db->Execute($get_quantity_query);
if($quantity->RecordCount() == 1)
return $quantity->fields['products_quantity'];
else
return 0;
}
?>
Bookmarks