Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

E-Commerce Modules /

Discuss about the various e-commerce modules available:
Ecommerce, SS Shop, SilverCart and SwipeStripe
Alternatively, have a look the shared mailinglist.

Moderators: martimiz, Nicolaas, Sean, Ed, frankmullenger, biapar, Willr, Ingo, Jedateach, swaiba

Best way to extend the Product class...


Go to End


5 Posts   3318 Views

Avatar
ImacSS

Community Member, 35 Posts

10 December 2009 at 6:50am

First time working with the e-commerce module, and running into a problem regarding the best method to extend the Product class.

I have several product-type objects that I need to model; a "Course", "Seminar", "Subscription", "Publication" and "Archives".

For "Publication", I can just use the base Product class as there are no additional fields needed.

However, for the "Course" and "Seminar" types, there are several additional data items I need to track. Specifically, there is a secondary price based upon whether a user opted discount should be applied, as well as several relationships between the Course/Seminar and various other tables.

Would the best method of representing these classes be to subclass the Product class directly?

I realize that doing this will require modifying some of the cart processing functions in order to take into account these additional fields - just want to make sure I'm going down the right path to begin with.

Any assistance that can be provided would be greatly appreciated!

Avatar
billythefisherman

Community Member, 1 Post

18 December 2009 at 11:45am

I'm very interested in this as well - how have you got on with extending the product class - is it feasible?

Avatar
ImacSS

Community Member, 35 Posts

19 December 2009 at 3:59am

Sorry I didn't follow up on this.

All I did was create a subclass of the "Product" class. So in my case, I did this:

class Course extends Product
{
}

...then added the additional fields I needed into the $db array, and overrode the getCMSFields method.

Implementing the additional fields functionally into the ecommerce pipeline required tinkering with the core components.

Maybe there is a better way, but this is what is working for me at the moment.

Avatar
oliver_phnd

Community Member, 1 Post

16 January 2010 at 1:31am

Edited: 16/01/2010 2:46am

To add new attributes/fields to my products I added all required fields to the Product.php file and created php and ss files for each product type, that only showed the relevant fields.

So to add a product type called MyProduct with a Width attribute:

1. Add Width to ecommerce/code/Product.php:

class Product extends Page {
public static $db = array(
'Price' => 'Currency',
'Weight' => 'Decimal(9,2)',
'Model' => 'Varchar',
'FeaturedProduct' => 'Boolean',
'AllowPurchase' => 'Boolean',
'InternalItemID' => 'Varchar(30)',
"Width" => "Decimal(9,2)",
);
}

2. Create ecommerce/code/MyProduct.php:
<?php
class MyProduct extends Product {

static $add_action = 'MyProduct';

function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Content.Main", new TextField("Price", "Price"));
$fields->addFieldToTab("Root.Content.Main", new TextField("ShortDescription", "Description"));
$fields->addFieldToTab("Root.Content.Main", new TextField("ArtNr","Article number"));
$fields->addFieldToTab("Root.Content.Main", new TextField("Weight", "Weight (kg)"));
$fields->addFieldToTab("Root.Content.Main", new TextField("Width","Width"));

// product image field
if(!$fields->dataFieldByName("Image")) {
$fields->addFieldToTab("Root.Content.Images", new ImageField("Image", "Product Image"));
}

// flags for this product which affect it's behaviour on the site
$fields->addFieldToTab("Root.Content.Main", new CheckboxField("FeaturedProduct", "Featured Product"));
$fields->addFieldToTab("Root.Content.Main", new CheckboxField("AllowPurchase", "Allow product to be purchased",1));
return $fields;
}
}

class MyProduct_Controller extends Product_Controller {
}
?>

3. Create ecommerce/templates/Layout/MyProduct.ss as you would any other template

Avatar
dospuntocero

Community Member, 54 Posts

18 January 2010 at 1:24pm

the best way to add new attributes to any data object in silverstripe is using DataObjectDecorators, no overwriting the classes that come with a module, because if the module its upgraded your code will be losted. Look here: http://doc.silverstripe.com/doku.php?id=dataobjectdecorator