2grosiek7 Posted October 1, 2016 Share Posted October 1, 2016 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 More sharing options...
rocky Posted October 2, 2016 Share Posted October 2, 2016 You should be able to use {$product->name} or {$product.name} depending on whether you have $product as an object or an array. Link to comment Share on other sites More sharing options...
2grosiek7 Posted October 2, 2016 Author Share Posted October 2, 2016 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 More sharing options...
rocky Posted October 4, 2016 Share Posted October 4, 2016 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 More sharing options...
2grosiek7 Posted October 4, 2016 Author Share Posted October 4, 2016 Summary:I should paste these code into new function in for example meta.php file, and then, call that function in header.tpl? Link to comment Share on other sites More sharing options...
rocky Posted October 4, 2016 Share Posted October 4, 2016 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 More sharing options...
2grosiek7 Posted October 4, 2016 Author Share Posted October 4, 2016 That function gave me only default category Link to comment Share on other sites More sharing options...
rocky Posted October 4, 2016 Share Posted October 4, 2016 Maybe the Category::getAllParents() function will help you? Link to comment Share on other sites More sharing options...
2grosiek7 Posted October 4, 2016 Author Share Posted October 4, 2016 It gave me blank site Link to comment Share on other sites More sharing options...
rocky Posted October 4, 2016 Share Posted October 4, 2016 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 More sharing options...
2grosiek7 Posted October 4, 2016 Author Share Posted October 4, 2016 Should I put it into meta.php? In which function? Link to comment Share on other sites More sharing options...
rocky Posted October 4, 2016 Share Posted October 4, 2016 It's hard for me to say, since I don't fully understand what it is that you're trying to achieve. Link to comment Share on other sites More sharing options...
2grosiek7 Posted October 4, 2016 Author Share Posted October 4, 2016 Actually, I have title od product page like:Name of product - Default CategoryI 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 More sharing options...
rocky Posted October 4, 2016 Share Posted October 4, 2016 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 More sharing options...
2grosiek7 Posted October 4, 2016 Author Share Posted October 4, 2016 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 More sharing options...
rocky Posted October 5, 2016 Share Posted October 5, 2016 Strange, I don't understand why it isn't working for you. It's working on my PrestaShop v1.6.1.7 test site: Can you try temporarily deleting the getMetaTags override see if that makes a difference? Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now