Jump to content

Transform id_category into name of that


2grosiek7

Recommended Posts

Hello,

I am looking for solutions, how to easily change id_parent, id_product or something else into name of that? For example, I have function:

{if $product}{$product->id_product}{/if}

And it returns for me just number, id of product. I want to get name of product instead id.

Link to comment
Share on other sites

So,

next example, if  I have parent category and I want to get id of that, I should use function and it works.

 

{if $category}{$category->id_parent}{/if}.

But, if I want to get name of that, did I should use something like this?

{if $category}{$category->id_parent->?}{/if}

 

Link to comment
Share on other sites

You could use the following:
{assign var='parent_category' value=Category::getCategoryInformations([$category->id])}
{$parent_category.{$category->id}.name}

But it would seem what you're doing is too complicated to be put in a TPL file and should be done in the PHP file.

Link to comment
Share on other sites

My above solution can be pasted into the TPL file. If you want to do it using PHP instead, you should override classes/Meta.php and add a function that creates an array with all the category IDs and names that you need, then assign it to the TPL file. That way, the name will already be there and you don't need to get the name in the TPL file using the ID.

Link to comment
Share on other sites

It would if you put it in a TPL file instead of a PHP file. You'll have to put it in the PHP file and assign the results like this:

$parents = $category->getAllParents();
​$this->context->smarty->assign('parents', $parents);
Link to comment
Share on other sites

Actually, I have title od product page like:

Name of product - Default Category

I made default category with vekia's tutorial. I want to get parent category of default category into title of product page like here:

Name of product - Default Category - Parent of default category

 

Link to comment
Share on other sites

Oh, I understand now. You can do it by creating override/classes/Meta.php with the following:
<?php

class Meta extends MetaCore
{
    public static function getProductMetas($id_product, $id_lang, $page_name)
    {
        $sql = 'SELECT `name`, `meta_title`, `meta_description`, `meta_keywords`, `description_short`, p.`id_category_default`
	       	FROM `'._DB_PREFIX_.'product` p
		LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = p.`id_product`'.Shop::addSqlRestrictionOnLang('pl').')
		'.Shop::addSqlAssociation('product', 'p').'
		WHERE pl.id_lang = '.(int)$id_lang.'
		AND pl.id_product = '.(int)$id_product.'
		AND product_shop.active = 1';
        if ($row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql)) {
            if (empty($row['meta_description'])) {
                $row['meta_description'] = strip_tags($row['description_short']);
            }
            
            $category = new Category((int)$row['id_category_default'], $id_lang);
            $row['name'] .= ' - '.$category->name;
            
            $parent = new Category((int)$category->id_parent, $id_lang);
            
            $row['name'] .= ' - '.$parent->name;
            
            return Meta::completeMetaTags($row, $row['name']);
        }

        return Meta::getHomeMetas($id_lang, $page_name);
    }   
}

Remember to go to Advanced Parameters > Performance and click the "Clear cache" button (or manually delete cache/class_index.php) so PrestaShop can find the override.

Link to comment
Share on other sites

I have followings Metas:

override/classes/meta.php:
 

<?php
class Meta extends MetaCore
{
    public static function getProductMetas($id_product, $id_lang, $page_name)
    {
        $sql = 'SELECT `name`, `meta_title`, `meta_description`, `meta_keywords`, `description_short`, p.`id_category_default`
	       	FROM `'._DB_PREFIX_.'product` p
		LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = p.`id_product`'.Shop::addSqlRestrictionOnLang('pl').')
		'.Shop::addSqlAssociation('product', 'p').'
		WHERE pl.id_lang = '.(int)$id_lang.'
		AND pl.id_product = '.(int)$id_product.'
		AND product_shop.active = 1';
        if ($row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql)) {
            if (empty($row['meta_description'])) {
                $row['meta_description'] = strip_tags($row['description_short']);
            }
            
            $category = new Category((int)$row['id_category_default'], $id_lang);
            $row['name'] .= ' - '.$category->name;
            
            $parent = new Category((int)$category->id_parent, $id_lang);
            
            $row['name'] .= ' - '.$parent->name;
            
            return Meta::completeMetaTags($row, $row['name']);
        }

        return Meta::getHomeMetas($id_lang, $page_name);
    }   

	public static function getMetaTags($id_lang, $page_name, $title = '')
        {
                global $maintenance;
                if (!(isset($maintenance) && (!in_array(Tools::getRemoteAddr(), explode(',', Configuration::get('PS_MAINTENANCE_IP'))))))
                {
                        if ($page_name == 'product' && ($id_product = Tools::getValue('id_product')))
                                return Meta::getProductMetas($id_product, $id_lang, $page_name);
                        elseif ($page_name == 'category' && ($id_category = Tools::getValue('id_category')))
                                return Meta::getCategoryMetas($id_category, $id_lang, $page_name, $title);
                        elseif ($page_name == 'manufacturer' && ($id_manufacturer = Tools::getValue('id_manufacturer')))
                                return Meta::getManufacturerMetas($id_manufacturer, $id_lang, $page_name);
                        elseif ($page_name == 'supplier' && ($id_supplier = Tools::getValue('id_supplier')))
                                return Meta::getSupplierMetas($id_supplier, $id_lang, $page_name);
                        elseif ($page_name == 'cms' && ($id_cms = Tools::getValue('id_cms')))
                                return Meta::getCmsMetas($id_cms, $id_lang, $page_name);
                        elseif ($page_name == 'cms' && ($id_cms_category = Tools::getValue('id_cms_category')))
                                return Meta::getCmsCategoryMetas($id_cms_category, $id_lang, $page_name);
                        elseif ($page_name == 'search')
                                return Meta::getSearchMetas($id_lang, $page_name);
                }

                return Meta::getHomeMetas($id_lang, $page_name);
        }
	public static function getSearchMetas($id_lang, $page_name){
		$sql = 'SELECT term as name, `title` as meta_title, `description` as meta_description, `keywords` as  `meta_keywords`
                        	FROM '._DB_PREFIX_.'searchterm s
                                WHERE term = \''.pSQL($_GET['search_query']).'\'';
                if ($row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql))
                {
                        if (empty($row['meta_description']))
                                $row['meta_description'] = strip_tags($row['meta_description']);
                        if (!empty($row['meta_title']))
                                $row['meta_title'] = $row['meta_title'].' - '.Configuration::get('PS_SHOP_NAME');
                        return Meta::completeMetaTags($row, $row['name']);
                }

                return Meta::getHomeMetas($id_lang, $page_name);
        }
}

And:

classes/meta.php

<?php
...

    public static function getProductMetas($id_product, $id_lang, $page_name)
    {
        $sql = 'SELECT `name`, `meta_title`, `meta_description`, `meta_keywords`, `description_short`
				FROM `'._DB_PREFIX_.'product` p
				LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = p.`id_product`'.Shop::addSqlRestrictionOnLang('pl').')
				'.Shop::addSqlAssociation('product', 'p').'
				WHERE pl.id_lang = '.(int)$id_lang.'
					AND pl.id_product = '.(int)$id_product.'
					AND product_shop.active = 1';
        if ($row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql)) {
	//	$sql = 'SELECT id_category_default FROM `'._DB_PREFIX_.'product` WHERE id_product = '.(int)$id_product.'';
	//	$row_cat = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
	//	$cat=new Category($row_cat['id_category_default'],(int)$id_lang);
	//	$row['meta_title']=$row['meta_title']." << ".$cat->name;
		
            if (empty($row['meta_description'])) {
                $row['meta_description'] = strip_tags($row['description_short']);
            }
            return Meta::completeMetaTags($row, $row['name']);
        }

        return Meta::getHomeMetas($id_lang, $page_name);
    }



And It won't works. Already I haven't in meta title default category or parent category. I have cleared cache and recompile theme.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...