Post by Skipper » Sun Feb 07, 2010 2:26 am

Short question: How can I extend the basic classes while maintaining upgradeability. I have a project with some particular product attributes. And I would prefer to touch the core files at each new release.

Are there recommended procedures for this. Can I, somehow, extend the core classes in a systematic way to build ontop of the base?

Thanks and thanks for a top-notch system!
Ted

Newbie

Posts

Joined
Thu Jan 07, 2010 3:48 pm

Post by itw_davies » Sun Feb 07, 2010 7:32 pm

Hi Ted

I have not seen documentation for developing modules (would love to be corrected here, and have only been working on oc for a few days) but it is fairly easy to create your own modules by following the oc rules, without overwriting much or any of the core code.

Could you explain what you are trying to do? I'll try to guide you ;)

Cheers

Matt

Matt Davies

www.itwiz.co.uk | matt.davies@itwiz.co.uk


User avatar
New member

Posts

Joined
Sun Feb 07, 2010 10:14 am
Location - Surrey and Hampshire, United Kingdom

Post by Skipper » Mon Feb 08, 2010 1:49 am

Hi Matt,

let me try to create a scenario. Say I want to sell computers. In addition to the general description, I have a technical description. This makes 1 more description than is available in the standard install. Adding the field is no big thing: database, model, view, controller, done. But on the next version of OC, I will probably have to redo the changes.

This is where I hope for a builtin mechanism that allows me to write "MY_product extends product" and my additions and changes are used to whatever functionality comes from the base class.

Thanks,
Ted

Newbie

Posts

Joined
Thu Jan 07, 2010 3:48 pm

Post by itw_davies » Mon Feb 08, 2010 3:12 am

Hi Ted

Ok, I haven't had a chance to test this and input from Daniel/Q would be super helpful, but what do you think to this...

You're dead right that adding the field isn't too hard, the problem as I see it really comes because you can't have different backend templates. Therefore if you want to change the way the core works you need to edit the core, but I agree with you, if possible we should try to minimise changes to the core.

I think I would start by creating a new model in admin/model/catalog. To keep in line with what I perceived the name structure for oc to be it should be called something like product_md.php (the md could be anything you want).

This would contain a class called ModelCatalogProductMD this class would extend ModelCatalogProduct.

ModelCatalogProductMD would have a really simple function just to add your one field to the db (which could be copied and adjusted from the original product model) we'll call this function addProductMD().

Once that it is done it needs to be loaded in admin/controller/catalog/product.php I believe the right code for this needs to go into the index function (around line 10) and would look like this: $this->load->model('catalog/product_md');

Now you can reach your new model! Next at line 23 of the product controller there is the code for adding a product. Just below this add $this->model_catalog_product_md->addProductMD($this->request->post); this will send the post from the insert product form to your function.

Right then, at this stage you have only one new file to add and only two lines of code to add to the product controller when you upgrade. I would write these down somewhere so I can get them quickly when I want to upgrade.

The final bit to consider is how to add textarea to the order form. Ultimately admin/view/template/catalog/product_form.tpl is going to have to be edited. To minimise the code you could use a php include, but I think I would likely just manually add the form code and write down what I added so I could quickly copy paste it into new oc projects or an upgraded version.

Sorry this is a huge post and I hope I haven't taught you to suck eggs, but I hate elusive forum posts that assume knowledge.

Two more points I just thought of, first to automate the install of your new plugin, you might want to write a php/sql install script that would simply add your field to the product table when run. You could then move it off the server after to use again. Second if your new text area element requires text from the language file you need to add it to the data array in the controller. This can be found around line 296 and looks like $this->data['new _var_name'] = $this->language->get('_var_name');

Phew! I'm finished, promise.

Hope it all helps. Let me know your thoughts.

Matt

Matt Davies

www.itwiz.co.uk | matt.davies@itwiz.co.uk


User avatar
New member

Posts

Joined
Sun Feb 07, 2010 10:14 am
Location - Surrey and Hampshire, United Kingdom

Post by Xsecrets » Mon Feb 08, 2010 3:38 am

that pretty much covers it the only other thing which could pull a bit more out might be to look into how children work and use that for the input. but unfortunately you still have to edit several core files which would have to be updated on upgrade.

OpenCart commercial mods and development http://spotonsolutions.net
Layered Navigation
Shipment Tracking
Vehicle Year/Make/Model Filter


Guru Member

Posts

Joined
Sun Oct 25, 2009 3:51 am
Location - FL US

Post by itw_davies » Mon Feb 08, 2010 5:03 am

Hi Xsecrets

I've only been using OC since yesterday, so I'm not 100% on everything yet. Could you explain what you meant by
Xsecrets wrote:look into how children work and use that for the input.
Cheers

Matt

Matt Davies

www.itwiz.co.uk | matt.davies@itwiz.co.uk


User avatar
New member

Posts

Joined
Sun Feb 07, 2010 10:14 am
Location - Surrey and Hampshire, United Kingdom

Post by Xsecrets » Mon Feb 08, 2010 6:15 am

well I've glanced over it, but haven't really dug into it or tried to see what happens when you do different things, but you'll notice that in say product.php controller there is children header, footer which is why you can simply do the echo $header echo $footer. could be a way to reduce some code by creating children classes. I plan on seeing exactly how far you can take it, but I haven't yet.

OpenCart commercial mods and development http://spotonsolutions.net
Layered Navigation
Shipment Tracking
Vehicle Year/Make/Model Filter


Guru Member

Posts

Joined
Sun Oct 25, 2009 3:51 am
Location - FL US

Post by itw_davies » Mon Feb 08, 2010 7:50 am

Hi Xsecrets

I looked into that. It's quite interesting, as far as I can see the child array is used to include block child elements.

The front end controller child array looks like this:

$this->children = array(
'common/header',
'common/footer',
'common/column_left',
'common/column_right'
);

The back end controller child array looks like this:

$this->children = array(
'common/header',
'common/footer'
);

But these appear to be just for common elements. On the other hand just above the child elements is $this->template = 'default/template/product/product.tpl';

I think it could be possible to change this of course if you wanted to use a different template without overwriting or changing the old one.

What I don't quite understand at the moment is how you set up modules so that they appear in the extensions menu in the admin panel. Can anyone explain that?

I have seen this post http://forum.opencart.com/viewtopic.php?f=22&t=6696 which makes sense, but hasn't worked for me yet. What if your module only effects something in the admin, as Ted and I have been talking about?

Cheers

Matt

Matt Davies

www.itwiz.co.uk | matt.davies@itwiz.co.uk


User avatar
New member

Posts

Joined
Sun Feb 07, 2010 10:14 am
Location - Surrey and Hampshire, United Kingdom

Post by Xsecrets » Mon Feb 08, 2010 1:12 pm

only files in the module folder show up as extensions, and you don't have to do anything to make that happen.

I realize that the children is currently only used for common stuff, but who's to say you couldn't create a complete model/view/controller for your addition to say products without having to overwrite anything then simply add it to the children array and simply echo $addonname in the product view?

OpenCart commercial mods and development http://spotonsolutions.net
Layered Navigation
Shipment Tracking
Vehicle Year/Make/Model Filter


Guru Member

Posts

Joined
Sun Oct 25, 2009 3:51 am
Location - FL US
Who is online

Users browsing this forum: No registered users and 2 guests