Jump to:

2084 Posts in 1005 Topics by 453 members

E-Commerce Modules

SilverStripe Forums » E-Commerce Modules » Best way to extend the Product class...

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

Page: 1
Go to End
Author Topic: 2634 Views
  • ImacSS
    Community Member
    35 Posts

    Best way to extend the Product class... Link to this post

    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!

  • billythefisherman
    Community Member
    1 Post

    Re: Best way to extend the Product class... Link to this post

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

  • ImacSS
    Community Member
    35 Posts

    Re: Best way to extend the Product class... Link to this post

    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.

  • oliver_phnd
    Community Member
    1 Post

    Re: Best way to extend the Product class... Link to this 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:
    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

  • dospuntocero
    Community Member
    54 Posts

    Re: Best way to extend the Product class... Link to this post

    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

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.