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.

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, frankmullenger, biapar, Willr, Ingo, Jedateach, swaiba, simon_w

Best way to extend the Product class...


Reply

5 Posts   2647 Views

Avatar
ImacSS

10 December 2009 at 6:50am Community Member, 35 Posts

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

18 December 2009 at 11:45am Community Member, 1 Post

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

Avatar
ImacSS

19 December 2009 at 3:59am Community Member, 35 Posts

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

16 January 2010 at 1:31am (Last edited: 16 January 2010 2:46am), Community Member, 1 Post

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

18 January 2010 at 1:24pm Community Member, 54 Posts

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