Jump to content

Cannot override Tools Class


Recommended Posts

Hello,

 

I'm using PS 1.4.4.1 and I'm trying to override the Tools class but my changes in my override class don't seem to be getting picked up. I've overridden many classes before but for some reason I cannot override the Tools class. Is there something I'm doing wrong or is the Tools class not meant to be overridden?

 

Here is my sample code of my override/Tools.php

class Tools extends ToolsCore {
   public static function getHomeMetaTags($id_lang, $page_name)
   {
	   //do stuff
   }
}

Link to comment
Share on other sites

Woops sorry for my mistake in the path. It is in the override/classes/tools.php folder. I have over a dozen other classes I'm overriding in the same folder as I have my new Tools class. It's a mystery to me as to why it's not working.

 

I've overridden a function and just called die(); in the first line of the function and it still executes php as if nothing is wrong. There is no call to the parent function and when I go into the parent Tools class and modify the function I am trying to override, those changes occur on the front office, so I know the function I'm modifying is the correct one.

 

To confirm, you guys have all overridden the tools class with success?

Link to comment
Share on other sites

  • 2 weeks later...

I'd like to report that I'm having the same problem. I am trying to override the static getPath function in the very same file and it does not work.

  • Prestashop has a _Tools.php file in the override/classes directory which is part of the default install
  • When I installed 2 months ago I renamed override/classes/_Tools.php to override/classes/Tools.php so I could use the firephp logging features included in the file
  • It worked as expected
  • Today I added an override to the override/classes/Tools.php file for the static function getPath and it does not override the getPath function

I am using PHP 5.3.

 

Could this be because as of 5.3 the only classes that are allowed to have static abstract classes are interfaces?

Link to comment
Share on other sites

I have to explain what I'm doing as I think it will help to understand.

 

I wanted to created a CMS page that would act as a landing page for a CMS category in the breadcrumb. For example:

 

I created an Our Mission CMS page at the top level(under Home). I then created an Our Mission category under Home. I then created another CMS document called Our Team that was a child of the Our Mission category. When viewing the Our Team page the breadcrumb would look like this(link locations for these are in parenthesis):

 

Home(/) > Our Mission(/content/category/3-our-mission) > Our Team(no link, just CMS page name)

 

I modified the getPath function to substitute the link for the CMS page for the category link IF the category name matched the meta-title of a CMS page whose id_cms_category matched the parent_id of the category (this last part allows the code to work on an infinite number of subcategories). Therefore, when all is said and done the breadcrumb would look like this:

 

Home(/) > Our Mission(/content/6-our-mission) > Our Team(no link, just CMS page name)

 

To do this I modified the CMS section of the getPath function in the following manner(see below for the isCMSPage function definition):

 

elseif ($categoryType === 'CMS')
{
$category = new CMSCategory((int)($id_category), (int)($cookie->id_lang));
if (!Validate::isLoadedObject($category))
	die(self::displayError());
if($cms = CMS::isCMSPage($category->name, (int)($cookie->id_lang), $category->id_parent))
	$categoryLink = $link->getCMSLink($cms['id_cms'], $cms['link_rewrite']);
else
	$categoryLink = $link->getCMSCategoryLink($category);

if ($path != $category->name)
	$fullPath .= '<a href="'.self::safeOutput($categoryLink).'">'.htmlentities($category->name, ENT_NOQUOTES, 'UTF-8').'</a><span class="navigation-pipe">'.$pipe.'</span>'.$path;
else
	$fullPath = ($linkOntheLastItem ? '<a href="'.self::safeOutput($categoryLink).'">' : '').htmlentities($path, ENT_NOQUOTES, 'UTF-8').($linkOntheLastItem ? '</a>' : '');

return self::getPath((int)($category->id_parent), $fullPath, $linkOntheLastItem, $categoryType);
}

 

this is a static function I added to my CMS.php override class

public static function isCMSPage($name, $id_lang = 1, $id_cms_category)
{
return Db::getInstance()->getRow('
SELECT c.id_cms, l.link_rewrite
FROM `'._DB_PREFIX_.'cms` c
JOIN `'._DB_PREFIX_.'cms_lang` l ON (c.id_cms = l.id_cms)
WHERE l.meta_title = \''.$name.'\'
AND c.id_cms_category = '.(int)$id_cms_category.'
AND l.id_lang = '.(int)($id_lang));
}

 

Anyway, in the end my override of the getPath function in Tools.php did not work. I had to change the code of the core files for it to work correctly.

 

I also wanted to reiterate that I used the Tools.php override file created by the Prestashop team, so it must be in the right place and created correctly!

Link to comment
Share on other sites

this works for me.

1) I renamed _Tools.php to Tools.php in the override/classes folder.

2) I copied the entire getPath function from the core Tools class, to my override

3) I added the following line of code as the first statement in the override function

Logger::addLog("My get path called");

4) I went to the front office and selected a category, since the CategoryController calls the getPath function.

5) I went to the back office Tools | Logs section, and I see my entry

 

So this tells me I can override the Tools class, and expect prestashop to invoke it.

 

The issue is something you have not done correctly.

Link to comment
Share on other sites

  • 9 months later...
  • 10 months later...
  • 2 weeks later...
  • 5 months later...

Deleting cache/class_index.php works fine if i don't use debug mode.

But if in config/defines.inc.php is enabled debug profiling:

define('_PS_DEBUG_PROFILING_', true);

Then i can't override Tools class.

Tools class is already included in config/config.inc.php file and my override/classes/Tools.php file is never included:

if (_PS_DEBUG_PROFILING_)
{
	include_once(_PS_TOOL_DIR_.'profiling/Controller.php');
	include_once(_PS_TOOL_DIR_.'profiling/ObjectModel.php');
	include_once(_PS_TOOL_DIR_.'profiling/Hook.php');
	include_once(_PS_TOOL_DIR_.'profiling/Db.php');
	include_once(_PS_TOOL_DIR_.'profiling/Tools.php');
}

Prestashop version: 1.5.6.2

  • Like 1
Link to comment
Share on other sites

  • 11 months later...

I've had the same problem, and I've solved it by deleting the file

cache/class_index.php
Hope it helps others.

 

i helped me so thank you. i spent 5 hours today with a cannot find tools core class. getting frustrated at using prestashop if its not simple to use its no help. im trying to use multistore. yet the themes come up all over the place on different stores. v frustrating . still thank you for your help. trev

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...