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

Swipestripe related Products?


Go to End


9 Posts   3625 Views

Avatar
araiko

Community Member, 4 Posts

7 December 2014 at 12:30am

I'm fairly new to Silverstripe so if someone could outline the exact steps then that would be greatly appreciated.

Basically I'm building a site where at the bottom of each Product page, I want to have a list of related Products and recommended Products. Each Product will have a specific list (max 4).

What would I need to add to the code to allow for this functionality?

Avatar
helenclarko

Community Member, 166 Posts

12 December 2014 at 1:40pm

Edited: 12/12/2014 1:43pm

You will need to make adjustments to the product controller or product.php.

It might be worth making a copy of the product.php file from the Swipestripe folder and placing it into your "/mysite/code" folder

Product.php

Place the following under your Many_Many array

'RelatedProducts' => 'Product'

Then place the following under the controller

//Related Products
		$gridFieldComplex = GridFieldConfig::create()->addComponents(
		  new GridFieldToolbarHeader(),
		  new GridFieldSortableHeader(),
		  new GridFieldDataColumns(),
		  new GridFieldManyRelationHandler(),
		  new GridFieldPaginator(10),
		  new GridFieldEditButton(),
		  new GridFieldDeleteAction(),
		  new GridFieldDetailForm()
		);
		
		$tablefield = new GridField(
            'RelatedProducts',
            'Related Products:',
			$this->RelatedProducts(),
            $gridFieldComplex
        );
        $fields->addFieldToTab('Root.RelatedProducts', $tablefield);

function RelatedProductsLimit($limit = '') {
		return $this->owner->RelatedProducts("ProductID = " . $this->owner->ID, "", NUll, $limit);
	} 

Then inside your Product.ss file (Remember to also take a copy of this file and place it into your "/themes/*YOUR THEME*/template/layout" folder.

<% if RelatedProducts %>
				<div id="relatedProductsWrap">
					<div id="relatedTitle">
						Related Products
					</div>
					<div id="related">
						<% loop RelatedProductsLimit(4) %>
							<a href="$Link" alt="Find out more about $Title" title="Find out more about $Title" class="eachRelated" <% if Last %>style="margin-right:0px;"<% end_if %> >
								<% loop Images.First %>
									$Image.SetRatioSize(160,180)
								<% end_loop %>
								<h4>$MenuTitle</h4>
								$Amount.Nice
							</a>
						<% end_loop %>
						
						<div class="clear"></div>
					</div>
				</div>
			<% end_if %>

If my code copy is correct, you should have Releated Products as a tab on each of your products.
Then a grid field where you can select the related products you want.
They should all then show up as related products in your template.
Of course you will need to add some css to tidy the thing up.

Avatar
araiko

Community Member, 4 Posts

29 December 2014 at 11:19pm

Edited: 30/12/2014 1:50pm

Thanks for the quick reply! I've finally had a chance to try and add in the code you provided however it seems to come up with an "Internal Server Error". Would it be ok if you could please check to see if I did it all correctly?

class Product extends Page {
...
   public static $many_many (
      'RelatedProducts' => 'Product'
   );
...
public function getCMSFields() {
   ...
   //Related Products 
      $gridFieldComplex = GridFieldConfig::create()->addComponents( 
       new GridFieldToolbarHeader(), 
       new GridFieldSortableHeader(), 
       new GridFieldDataColumns(), 
       new GridFieldManyRelationHandler(), 
       new GridFieldPaginator(10), 
       new GridFieldEditButton(), 
       new GridFieldDeleteAction(), 
       new GridFieldDetailForm() 
      ); 
       
      $tablefield = new GridField( 
'RelatedProducts', 
'Related Products:', 
         $this->RelatedProducts(), 
$gridFieldComplex 
); 
$fields->addFieldToTab('Root.RelatedProducts', $tablefield);
function RelatedProductsLimit($limit = '') { 
      return $this->owner->RelatedProducts("ProductID = " . $this->owner->ID, "", NUll, $limit); 
   }

return $fields;

}

I've done a quick error-check and it's this particular line that is causing the issue:

new GridFieldManyRelationHandler(),

Is there an add-on that I need to install?

Avatar
helenclarko

Community Member, 166 Posts

12 January 2015 at 2:00pm

Hi Arakio,

Sorry I did not notice this earlier.

you will need to add the GridFieldRelationHandler addon from the link below:

https://git.simon.geek.nz/simon_w/gridfieldrelationhandler

The addon is required for creating links in many to many relationships.

Regards,
-helenclarko

Avatar
araiko

Community Member, 4 Posts

21 January 2015 at 12:17am

Hi helenclarko,

Thanks for your help once again! I think I'm one step closer to getting this sorted :) I've come across the following issues now that I've added in the GridFieldRelationHandler:
- when I try to 'Save & Publish' the Product page type, I now get an internal server error
- when I view the page on the front-end, the page seems to detect that there is a related product (when applicable) as if fulfils the 'if' clause and shows the heading as well as the other code within the conditional statement, however, it doesn't seem to be pulling all the RelatedProducts' data i.e. Title, image etc.

Thanks in advance!

Avatar
helenclarko

Community Member, 166 Posts

21 January 2015 at 2:32pm

Edited: 21/01/2015 2:34pm

What happens when you open the related products tab on the products page?
What is the Error saying?
You might be able to run "?isDev=1" on your URL and find some more information.

Avatar
araiko

Community Member, 4 Posts

26 January 2015 at 12:32am

I found that the initial error of not showing the products was due to the RelatedProductsLimit function - to counter this, I updated the template to just be:
<% loop RelatedProducts.Limit(4) %>

However, I still seem to be getting errors within the CMS -
* whenever I try to Save and Publish a Product page, an 'Internal Server Error' message is shown, however, the page still Saves & Publishes successfully
* I've managed to add a few RelatedProducts successfully however after adding quite a few, I reached a point where it's not longer letting me add anymore relationships - the CMS shows a pop-up window with the following message:
"An error occured while fetching data from the server
Please try again later."
Additionally, the CMS also shows the following error:
"Error at line 678 of [PATH]/framework/forms/gridfield/GridField.php"

If possible, would it be easier if you had a look at my code to see if I've done anything wrong? I really appreciate all the help you've provided so far, you've been awesome!

Avatar
helenclarko

Community Member, 166 Posts

28 January 2015 at 12:42pm

Hi Araiko,

Progress! I'll be it minor, but progress none the less.

I should probably ask which version of Silverstripe you are running, as if the version of SilverStripe you are running is earlier than 3.0.3 you will need to apply a patch to GridFieldRelationshipHandler

I haven't looked at this code for some time, as it just seems to work on my end.

"* whenever I try to Save and Publish a Product page, an 'Internal Server Error' message is shown, however, the page still Saves & Publishes successfully"
I've had a similar issue with this before, I'm trying to wrap my head around how I solved it.
Can you check that your page doesnt have closing php tags, E.g. remove "?>" from the end of product .php if it exists.

I am more than happy to take a look at your code, but first can you run ?isDev=1 and the add a product to the relationship.
This should hopefully capture the error.

Regards,
-helenclarko

Go to Top