143 lines
7.0 KiB
PHP
Raw Normal View History

2016-01-04 12:48:08 +01:00
<?php
class Product extends ProductCore
{
public static function getProductsProperties($id_lang, $query_result, $cached = true)
{
$resultsArray = array();
if (is_array($query_result))
foreach ($query_result AS $row)
if ($row2 = Product::getProductProperties($id_lang, $row, $cached))
$resultsArray[] = $row2;
return $resultsArray;
}
public static function getProductProperties($id_lang, $row, $cached = true)
{
if (!$row['id_product'])
return false;
// Product::getDefaultAttribute is only called if id_product_attribute is missing from the SQL query at the origin of it: consider adding it in order to avoid unnecessary queries
$row['allow_oosp'] = Product::isAvailableWhenOutOfStock($row['out_of_stock']);
if ((!isset($row['id_product_attribute']) OR !$row['id_product_attribute'])
AND ((isset($row['cache_default_attribute']) AND ($ipa_default = $row['cache_default_attribute']) !== NULL)
OR ($ipa_default = Product::getDefaultAttribute($row['id_product'], !$row['allow_oosp'])))
)
$row['id_product_attribute'] = $ipa_default;
if (!isset($row['id_product_attribute']))
$row['id_product_attribute'] = 0;
// Tax
$usetax = Tax::excludeTaxeOption();
if ($cached) {
$cacheKey = $row['id_product'].'-'.$row['id_product_attribute'].'-'.$id_lang.'-'.(int)($usetax);
if (array_key_exists($cacheKey, self::$producPropertiesCache))
2016-03-09 16:33:05 +01:00
return self::$producPropertiesCache[$cacheKey];
2016-01-04 12:48:08 +01:00
}
// Datas
$link = new Link();
$row['category'] = Category::getLinkRewrite((int)$row['id_category_default'], (int)($id_lang));
$row['link'] = $link->getProductLink((int)$row['id_product'], $row['link_rewrite'], $row['category'], $row['ean13']);
$row['attribute_price'] = (isset($row['id_product_attribute']) AND $row['id_product_attribute']) ? (float)(Product::getProductAttributePrice($row['id_product_attribute'])) : 0;
$row['price_tax_exc'] = Product::getPriceStatic((int)$row['id_product'], false, ((isset($row['id_product_attribute']) AND !empty($row['id_product_attribute'])) ? (int)($row['id_product_attribute']) : NULL), (self::$_taxCalculationMethod == PS_TAX_EXC ? 2 : 6));
if (self::$_taxCalculationMethod == PS_TAX_EXC)
{
$row['price_tax_exc'] = Tools::ps_round($row['price_tax_exc'], 2);
$row['price'] = Product::getPriceStatic((int)$row['id_product'], true, ((isset($row['id_product_attribute']) AND !empty($row['id_product_attribute'])) ? (int)($row['id_product_attribute']) : NULL), 6);
$row['price_without_reduction'] = Product::getPriceStatic((int)$row['id_product'], false, ((isset($row['id_product_attribute']) AND !empty($row['id_product_attribute'])) ? (int)($row['id_product_attribute']) : NULL), 2, NULL, false, false);
}
else
{
$row['price'] = Tools::ps_round(Product::getPriceStatic((int)$row['id_product'], true, ((isset($row['id_product_attribute']) AND !empty($row['id_product_attribute'])) ? (int)($row['id_product_attribute']) : NULL), 2), 2);
$row['price_without_reduction'] = Product::getPriceStatic((int)$row['id_product'], true, ((isset($row['id_product_attribute']) AND !empty($row['id_product_attribute'])) ? (int)($row['id_product_attribute']) : NULL), 6, NULL, false, false);
}
$row['reduction'] = Product::getPriceStatic((int)($row['id_product']), (bool)$usetax, (int)($row['id_product_attribute']), 6, NULL, true, true, 1, true, NULL, NULL, NULL, $specific_prices);
$row['specific_prices'] = $specific_prices;
if ($row['id_product_attribute'])
{
$row['quantity_all_versions'] = $row['quantity'];
$row['quantity'] = Product::getQuantity((int)$row['id_product'], $row['id_product_attribute'], isset($row['cache_is_pack']) ? $row['cache_is_pack'] : NULL);
}
$row['id_image'] = Product::defineProductImage($row, $id_lang);
$row['features'] = Product::getFrontFeaturesStatic((int)$id_lang, $row['id_product']);
$row['attachments'] = ((!isset($row['cache_has_attachments']) OR $row['cache_has_attachments']) ? Product::getAttachmentsStatic((int)($id_lang), $row['id_product']) : array());
// Pack management
$row['pack'] = (!isset($row['cache_is_pack']) ? Pack::isPack($row['id_product']) : (int)$row['cache_is_pack']);
$row['packItems'] = $row['pack'] ? Pack::getItemTable($row['id_product'], $id_lang) : array();
$row['nopackprice'] = $row['pack'] ? Pack::noPackPrice($row['id_product']) : 0;
if ($row['pack'] AND !Pack::isInStock($row['id_product']))
$row['quantity'] = 0;
if ($cached) {
self::$producPropertiesCache[$cacheKey] = $row;
2016-03-09 16:33:05 +01:00
return self::$producPropertiesCache[$cacheKey];
2016-01-04 12:48:08 +01:00
} else {
return $row;
}
}
public function toggleCoupCoeur(){
if($this->online_only == 1){
$this->online_only = 0;
$this->save();
}else{
$this->online_only = 1;
$this->save();
}
}
2016-03-09 16:33:05 +01:00
public static function getDetailsCombination($id_product_attribute, $id_lang) {
return Db::getInstance()->ExecuteS('
SELECT pa.*, ag.`id_attribute_group`, ag.`is_color_group`, agl.`name` AS group_name, al.`name` AS attribute_name, a.`id_attribute`, pa.`unit_price_impact`
FROM `'._DB_PREFIX_.'product_attribute` pa
LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON pac.`id_product_attribute` = pa.`id_product_attribute`
LEFT JOIN `'._DB_PREFIX_.'attribute` a ON a.`id_attribute` = pac.`id_attribute`
LEFT JOIN `'._DB_PREFIX_.'attribute_group` ag ON ag.`id_attribute_group` = a.`id_attribute_group`
LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al ON (a.`id_attribute` = al.`id_attribute` AND al.`id_lang` = '.(int)($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'attribute_group_lang` agl ON (ag.`id_attribute_group` = agl.`id_attribute_group` AND agl.`id_lang` = '.(int)($id_lang).')
WHERE pa.`id_product_attribute` = '.(int)($id_product_attribute).'
ORDER BY pa.`id_product_attribute`');
}
2016-03-21 10:07:35 +01:00
public static function duplicateOneAttribute($id_product_old, $id_product_new, $old_attribute_id)
{
$return = true;
$combinationImages = array();
$result = Db::getInstance()->ExecuteS('
SELECT *
FROM `'._DB_PREFIX_.'product_attribute`
WHERE `id_product` = '.(int)($id_product_old).' AND `id_product_attribute` = '.(int)($old_attribute_id));
foreach ($result as $row)
{
$id_product_attribute_old = (int)($row['id_product_attribute']);
$result2 = Db::getInstance()->ExecuteS('
SELECT *
FROM `'._DB_PREFIX_.'product_attribute_combination`
WHERE `id_product_attribute` = '.$id_product_attribute_old);
$row['id_product'] = $id_product_new;
unset($row['id_product_attribute']);
$return &= Db::getInstance()->AutoExecute(_DB_PREFIX_.'product_attribute', $row, 'INSERT');
$id_product_attribute_new = (int)(Db::getInstance()->Insert_ID());
if ($resultImages = self::_getAttributeImageAssociations($id_product_attribute_old))
{
$combinationImages['old'][$id_product_attribute_old] = $resultImages;
$combinationImages['new'][$id_product_attribute_new] = $resultImages;
}
foreach ($result2 AS $row2)
{
$row2['id_product_attribute'] = $id_product_attribute_new;
$return &= Db::getInstance()->AutoExecute(_DB_PREFIX_.'product_attribute_combination', $row2, 'INSERT');
}
}
return !$return ? false : $combinationImages;
}
2016-01-04 12:48:08 +01:00
}