Jump to content

(Solved) Question on overriding a class function entirely.


Bradley Clampitt

Recommended Posts

Hey Guys,

 

After a 6 hr failed attempt at doing an upgrade I am trying to clean up some past coding mistakes or modifications to be more easily upgradeable.

 

I have altered two classes; Feature.php and Product.php

 

What I am wanting to do is not ADD to a function but replace a given function.

 

For example in the Product.php file I have commented out the original function for "getFrontFeaturesStatic" and replaced it with an altered version that will hide the numbering scheme I had to add so that on the "Specifications" page of a given product it would list them in the order that I want.

 

The code I have altered was discussed on this page: http://www.prestasho...198#entry504198

 

But instead of editing the /classes/Product.php file I would like to move this function to the /override/classes/Product.php file so that when I do upgrades this is just one less thing I have to worry about. I mean I am sure I will have to double check to see if the function changed from upgrade to upgrade but alas it would be nice not to have to go through each file.

 

Here is the override function I created, but it doesn't appear to make a difference in things working or not working. I say that because I to the /classes/Product.php and removed my additions/edits and make it back to the original, uploaded it to the server and it performs as it did before I made changings, showing the numbers at the beginning of the features on the specifications page.

 

From there I created the /override/classes/Product.php file and added the code below, it still shows the exact same as it did before I added the file, if I make alterations which I have tried different things based off looking through the forums and on Rocky's website helpful pages and etc, but some of the modifications just leave me with a blank page. I have also forced compile and disabled/enabled cache, but every thing I do or adjust on that doesn't make a difference.

 

Here is the code I created in the override file:

 

class Product extends ProductCore {
public static function getFrontFeaturesStatic($id_lang, $id_product)
{
 if (!array_key_exists($id_product.'-'.$id_lang, self::$_frontFeaturesCache))
 {
  self::$_frontFeaturesCache[$id_product.'-'.$id_lang] = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
  SELECT name, value, pf.id_feature
  FROM '._DB_PREFIX_.'feature_product pf
  LEFT JOIN '._DB_PREFIX_.'feature_lang fl ON (fl.id_feature = pf.id_feature AND fl.id_lang = '.(int)$id_lang.')
  LEFT JOIN '._DB_PREFIX_.'feature_value_lang fvl ON (fvl.id_feature_value = pf.id_feature_value AND fvl.id_lang = '.(int)$id_lang.')
  WHERE pf.id_product = '.(int)$id_product.' order by name ASC');
 }

 /* Added Array Function to remove the Numbers at the beginning of the Feature */
 $resultsArray = array();
 foreach (self::$_frontFeaturesCache[$id_product.'-'.$id_lang] AS $row) {
  $row['name'] = preg_replace('/^[0-9]+\./', '', $row['name']);
  $resultsArray[] = $row;
 }
 return $resultsArray;
}
}

 

Any assistance or guidance on this would be helpful. I have been working with my prestashop store for well over a year, I have edited templates and various files but now I am wanting to go through and properly clean them up to make things easier for upgrades or just in general proper coding.

Link to comment
Share on other sites

I have sinced made it work with v1.4.4.0 with the following code

 

/override/classes/Product.php

 

class Product extends ProductCore {
static public function getFrontFeaturesStatic($id_lang, $id_product)
   {
    $result=Db::getInstance()->ExecuteS('
    SELECT name, value, pf.id_feature
    FROM '._DB_PREFIX_.'feature_product pf
    LEFT JOIN '._DB_PREFIX_.'feature_lang fl ON (fl.id_feature = pf.id_feature AND fl.id_lang = '.intval($id_lang).')
    LEFT JOIN '._DB_PREFIX_.'feature_value_lang fvl ON (fvl.id_feature_value = pf.id_feature_value AND fvl.id_lang = '.intval($id_lang).')
    WHERE pf.id_product = '.intval($id_product).'
    ORDER BY `name` ASC');
   $resultsArray = array();
    foreach ($result AS $row)
    {
	    $row['name'] = product::hideFeaturePosition($row['name']);
	    $resultsArray[] = $row;
    }
    return $resultsArray;
   }
   static public function hideFeaturePosition($name)
   {
    return preg_replace('/^[0-9]+./', '', $name);
   }
   public function getFrontFeatures($id_lang)
   {
    return self::getFrontFeaturesStatic($id_lang, $this->id);
   }
}

  • Like 1
Link to comment
Share on other sites

Hello,

the problem come from the Product class with this code :

	public function getFrontFeatures($id_lang)
{
	return self::getFrontFeaturesStatic($id_lang, $this->id);
}

This is something really bad that we changed in 1.5 by using Product:: instead of self::.

 

In PHP when you have self:: only the parent static function is called (they changed it in PHP 5.3 thx to the new static:: keyword). So to fix your bug you have to override getFrontFeature() function just like you did.

 

This problem won't happen anymore in 1.5.

 

Regards

  • Like 1
Link to comment
Share on other sites

thanks for the heads up Raphaël!

 

I have downloaded the beta version of 1.5 but have not had a chance to play with it yet, but I will probably install it on my own website instead of my companies, or will install it locally on my macbook to see what all is new.

 

Looking forward to some of the new features, currently setting up our webstore for the USA, but will be branching out to the UK, China, Germany and the Netherlands. The headaches are going to be configuring payments and shipping... to say the least.

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