// Product: added a method to get the name of a given product, product_attribute, in a given language
This commit is contained in:
@@ -3886,5 +3886,51 @@ class ProductCore extends ObjectModel
|
||||
return $anchor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of a given product, in the given lang
|
||||
*
|
||||
* @since 1.5.0
|
||||
* @param int $id_product
|
||||
* @param int $id_product_attribute Optional
|
||||
* @param int $id_lang Optional
|
||||
* @return string
|
||||
*/
|
||||
public static function getProductName($id_product, $id_product_attribute = null, $id_lang = null)
|
||||
{
|
||||
// use the lang in the context if $id_lang is not defined
|
||||
if (!$id_lang)
|
||||
$id_lang = (int)Context::getContext()->language->id;
|
||||
|
||||
// creates the query object
|
||||
$query = new DbQuery();
|
||||
|
||||
// selects different names, if it is a combination
|
||||
if ($id_product_attribute)
|
||||
$query->select('IFNULL(CONCAT(pl.name, \' : \', GROUP_CONCAT(agl.`name`, \' - \', al.name SEPARATOR \', \')),pl.name) as name');
|
||||
else
|
||||
$query->select('DISTINCT pl.name as name');
|
||||
|
||||
// queries different tables if it is a combination
|
||||
if ($id_product_attribute)
|
||||
$query->from('product_attribute pa');
|
||||
else
|
||||
$query->from('product_lang pl');
|
||||
|
||||
// adds joins & where clauses for combinations
|
||||
if ($id_product_attribute)
|
||||
{
|
||||
$query->innerJoin('product_lang pl ON (pl.id_product = pa.id_product AND pl.id_lang = '.(int)$id_lang.')');
|
||||
$query->leftJoin('product_attribute_combination pac ON (pac.id_product_attribute = pa.id_product_attribute)');
|
||||
$query->leftJoin('attribute atr ON (atr.id_attribute = pac.id_attribute)');
|
||||
$query->leftJoin('attribute_lang al ON (al.id_attribute = atr.id_attribute AND al.id_lang = '.(int)$id_lang.')');
|
||||
$query->leftJoin('attribute_group_lang agl ON (agl.id_attribute_group = atr.id_attribute_group AND agl.id_lang = '.(int)$id_lang.')');
|
||||
$query->where('pa.id_product = '.(int)$id_product.' AND pa.id_product_attribute = '.(int)$id_product_attribute);
|
||||
}
|
||||
else // or just adds a 'where' clause for a simple product
|
||||
$query->where('pl.id_product = '.(int)$id_product);
|
||||
|
||||
return Db::getInstance()->getValue($query);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user