prestashop associated categories in product.tpl

pikilon picture pikilon · Nov 3, 2012 · Viewed 10.7k times · Source

I have a product that belongs to two categories "rings" and "collections->waves" (category->subcategory)

I can show the default category and it link like this

<a href="{$link->getCategoryLink($product->id_category_default,$product->category)}" title="{$product->category}">{$product->category}</a>

But I can't show the (non default) associated categories anyhow. Is there any array with the associated categories in the object $product?

Because I know that all the categories are in the variable $categories (not the subcategories, it could be a problem, cause waves is a subcategory)

Thanks for everything

Answer

Alexander Simonchik picture Alexander Simonchik · Nov 3, 2012

Look to the Product class, it has nice function:

/**
 * getProductCategories return an array of categories which this product belongs to
 *
 * @return array of categories
 */
public static function getProductCategories($id_product = '')
{
    $ret = array();
    if ($row = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
    SELECT `id_category` FROM `'._DB_PREFIX_.'category_product`
    WHERE `id_product` = '.(int)$id_product)
    )
        foreach ($row as $val)
            $ret[] = $val['id_category'];
    return $ret;
}

Regards