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.

Archive /

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

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

eCommerce multiple images


Go to End


4 Posts   3184 Views

Avatar
iculshaw

Community Member, 13 Posts

23 October 2008 at 6:49am

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 (http://www.mind-projects.it/blog/jqzoom_v10) 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)

Thanks

Avatar
Cy

Community Member, 11 Posts

23 October 2008 at 7:25am

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

This thread has proven helpful to me as regards to multiple images on a page.

Hope that helps!

Avatar
iculshaw

Community Member, 13 Posts

24 October 2008 at 3:49am

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.

<?php
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) {
		$gd->setQuality(80);
		return $gd->paddedResize(140,100);
	}
	
	function generateContentImage($gd) {
		$gd->setQuality(90);
		return $gd->resizeByWidth(200);
	}
	
	function generateLargeImage($gd) {
		$gd->setQuality(90);
		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(
			$this,
			'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;
		}
	}

Avatar
iculshaw

Community Member, 13 Posts

24 October 2008 at 9:07pm

nevermind i fixed it :)