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.


Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

eCommerce multiple images


4 Posts   2352 Views


23 October 2008 at 6:49am Community Member, 13 Posts

How would i implement having multiple images for each product in the ecommerce module. I'm looking to have multiple images for each product. Reason i'm asking i know you have to change the

static $has_one = array(
      'Image' => 'Product_Image'

to a "has many" but i'm unsure of how images are actually processed within sapphire.

I've also implemented a javascript zoom image ( onto the product image so would also benefit from knowing how i can store both a resized image of the uploaded, plus the original image (to be zoomed)



23 October 2008 at 7:25am Community Member, 11 Posts

I believe you'll need to create a new class to manage multiple images. That built-in Image Object only supports one.

[url=]This thread[/url] has proven helpful to me as regards to multiple images on a page.

Hope that helps!


24 October 2008 at 3:49am Community Member, 13 Posts

Ok, i've implemented that ImageAttachment class directly to the Product page and got it workin how i want... however; i have dropped the has_one Image that Product has for each one, and now i need the first of my multiple images to be shown on the front of the product category (e.g. in featured products and nested product groups). I'll post my code below, hopefully someone can help me fathom my way through into getting to the object through all the has ones and has many's through the ecommerce module.

class ImageAttachment extends DataObject {
   static $db = array(
      'Name' => 'Text'
   static $has_one = array(
      'Image' => 'Resize_Image',
      'Product' => 'Product'
   static $field_names = array('Name' => 'Name');
   function getCMSFields_forPopup() {
      $fields = new FieldSet();
      $fields->push(new TextField('Name', 'Name'));
      $fields->push(new ImageField('Image', 'Image'));
      return $fields;

class Resize_Image extends Image {
   static $db = null;

   function generateThumbnail($gd) {
      return $gd->paddedResize(140,100);
   function generateContentImage($gd) {
      return $gd->resizeByWidth(200);
   function generateLargeImage($gd) {
      return $gd->resizeByWidth(600);

My Modified Product class

    * Image Support
   static $has_one = array(
      'ImageThumbnail' => 'Product_Image',
      'Image' => 'Image'
   static $has_many = array(
      'ImageAttachments' => 'ImageAttachment'
   static $defaults = array(
      'AllowPurchase' => true
    * Create the fields for a product within the CMS
   function getCMSFields() {
      $fields = parent::getCMSFields();

            // standard extra fields like weight and price
      $fields->addFieldToTab("Root.Content.Main", new TextField("Weight", "Weight (kg)", "", 12));
      $fields->addFieldToTab("Root.Content.Main", new TextField("Price", "Price", "", 12));
      $fields->addFieldToTab("Root.Content.Main", new TextField("Model", "Author", "", 50));

      $fields->addFieldToTab("Root.Content.Main", new TextField("InternalItemID","Product Code","",7));

      $imagetable = new ComplexTableField(
         'ImageAttachments', // relation name
         'ImageAttachment', // object class
         ImageAttachment::$field_names, // fields to show in table
         ImageAttachment::getCMSFields_forPopup(), // form that pops up for edit
         "ProductID = {$this->ID}", // a filter to only display item associated with this page
         "Name ASC" // Sort by name
      $fields->addFieldToTab('Root.Content.Additional Images', $imagetable);


   function ImageAttachment() {
      if($obj = DataObject::get('ImageAttachment', "ProductID = $this->ID")) {
         return $obj;
      } else {
         return false;


24 October 2008 at 9:07pm Community Member, 13 Posts

nevermind i fixed it :)