Jump to content

Extending the product class by one field – is an override the only solution?


Narancs

Recommended Posts

Hello,

 

Right now I was writing a small module, which should provide the possibility to input the manufacturer's recommended retail price in the back-end. The part in the back-end is done, however I was left wondering when I started to figure out how to provide that information in the front-end.

 

I do not want this field to be available only on certain hooks like displaying the product page or a product listing, I want it to be a part of the product class instances whenever they are present, so that I can adjust my templates to display that information on all occasions where the actual product price is displayed, too.

 

But I am (still) skeptical of the Product class override being the best solution because I sense some potential problems caused by future updates which alter the core product class – which then does not match anymore with my override.

 

In detail I found out that I need to extend the class by one public field (I named it "manufacturers_recommended_price") and extend the "definition" array (public member of Product class) by the same name.

 

If the core product class is updated in future and the definition is altered in that case, my override would overwrite the updated core product class with the deprecated array definition as I defined it while writing the override – correct me, if I am wrong. but that's how PHP's inheritance works.

 

Keeping that issue in mind, it is also a general question: Is an override of the core product class the best solution to extend the product entity itself by one field which is meant to be inseparable part of the product?

Link to comment
Share on other sites

Have you tried altering the $definition array inside a overloaded constructor ?

public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null){
	$new_fields = array('manufacturers_recommended_price' => array('type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isPrice'));
	parent::$definition['fields'] = array_merge(parent::$definition['fields'], $new_fields);
	parent::__construct($id_product, $full, $id_lang, $id_shop, $context);
}

LE:
This might not be a complete solution since the constructor prototype might change
at a later time (last change was in 2011-09-30 : 'NULL' -> 'null').
You could solve that with "func_get_args and call_user_func_array" but this could raise some
compatibility problems with older versions of PHP

Edited by Lupu (see edit history)
Link to comment
Share on other sites

The override is the best way to accomplish.  It is generally safe and will survive upgrades just fine since override classes generally stick out like a sore thumb for anyone with experience in performing upgrades.

 

Just do a google search, you are not re-inventing the wheel here, this has been asked and answer many times already

Link to comment
Share on other sites

@Lupu: That is a smart move towards the goal I did not think about, thanks.

 

@bellini13: Thank you for this information.

 

I am sorry for bothering an already answered question, I was already searching the internet but with a less successful wording. It was getting late yesterday, however I found what I was searching for – even better what I initially found, this is right what I wanted to do.

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