atsngr
atsngr

Reputation: 181

Prestashop SQL Query to display category names of just the active language

I have a Prestashop module developed some years ago. The module displays new products based on selected categories. I recently discovered that if I have more than one language setup for the store (e.g en, fr) and I edit a category names in English and French. The module will display both names in the frontend instead of just displaying one based on the active shop language.

I think the code below inside the edited blocknewproduct.php fetch the category list:

    public function hookdisplayHome($params)
{
    $list_id_category= Tools::jsonDecode(Configuration::get('PS_NB_NEW_PRODUCT_CATEGORY'));
    $resuilt_category=array(); 
    if($list_id_category)
    {
        $sql="select distinct id_category,name from "._DB_PREFIX_."category_lang where id_category in (".implode(',',$list_id_category).")";
        $categories=Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
    }
    if (!$this->isCached('blocknewproducts_home.tpl', $this->getCacheId('blocknewproducts-home')))
        BlockNewProducts1::$cache_new_products = $this->getNewProducts();
    {
        $this->smarty->assign(array(
            'new_products' => BlockNewProducts1::$cache_new_products,
            'categories' =>$categories,
            'mediumSize' => Image::getSize(ImageType::getFormatedName('medium')),
            'homeSize' => Image::getSize(ImageType::getFormatedName('home'))
        ));
    }

    if (BlockNewProducts1::$cache_new_products === false)
        return false;
    $this->pagination(count($this->getAllNewProducts($this->context->language->id))); // by ats_edited

    return $this->display(__FILE__, 'blocknewproducts_home.tpl', $this->getCacheId('blocknewproducts-home'));
}

Can anyone suggest what line of this code needs to be edited to get only the category name of the active language to be displayed.

In the blocknewproducts_home.tpl file, this code below is used to display the list.

<ul class="list-category-new-product plists">
                    <li class="plist {if !isset($id_category_select) || !$id_category_select}active{/if}"><a href="#" data-id="0" title="{l s='All' mod='blocknewproducts1'}">{l s='All' mod='blocknewproducts1'}</a></li>
                    {foreach from=$categories item='category'}
                        <li class="plist {if $category.id_category==$id_category_select}active{/if}"><a href="#" data-id="{$category.id_category}" title="{$category.name}">{$category.name}</a></li>
                    {/foreach}
                </ul>

Sorry I am not a programmer, just a web designer so I will need a clear assistance.

Upvotes: 1

Views: 2194

Answers (1)

sadlyblue
sadlyblue

Reputation: 2987

change the $sql to:

$sql="select distinct id_category,name from "._DB_PREFIX_."category_lang where id_category in (".implode(',',$list_id_category).") and id_lang = ".(int) $this->context->language->id;

Upvotes: 0

Related Questions