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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

How do I return only products from specific shop


Go to End


6 Posts   1607 Views

Avatar
zim

Community Member, 135 Posts

24 October 2010 at 5:51am

my problem is this.

I have created a bicycle web site.

I have a top level page 'shopholder.php' which has children 'productholder.php', and below that i have children 'productpage.php'; and these products are again broken down into the three types 'mountain', 'road', 'childrens' (eg type of bicycles)

so the structure looks like this

SHOPHOLDER (shopholder.php)
SHOP ONE (productholder.php)
- product one (productpage.php) (type=mountain)
- product two (productpage.php) (type=mountain)
- product three (productpage.php) (type=mountain)
- product four (productpage.php) (type=road)
- product five (productpage.php) (type=children)
SHOP TWO (productholder.php)
- product one (productpage.php) (type=mountain)
- product two (productpage.php) (type=mountain)
- product three (productpage.php) (type=road)
- product four (productpage.php) (type=road)
- product five (productpage.php) (type=children)
SHOP THREE (productholder.php)
- product one (productpage.php) (type=mountain)
- product two (productpage.php) (type=mountain)
- product three (productpage.php) (type=road)
- product four (productpage.php) (type=road)
- product five (productpage.php) (type=children)

In productpage.php i have the below functions to return the product item thumbnail images to display... however when i click through to any of the shop pages (productholder.php) eg SHOP ONE i get all the mountain bicycles etc returned. from all the shops.

How do i call just those products (Children) of the current shop? can anyone help? any help much appreciated

function ProductMountain() {
$mountain = DataObject::get_one("ProductHolder");
return ($mountain) ? DataObject::get("ProductPage", "Type = 'Mo'", "") : false;
}

function ProductRoad() {
$road = DataObject::get_one("ProductHolder");
return ($road) ? DataObject::get("ProductPage", "Type = 'Ro'", "") : false;
}

function ProductChild() {
$child = DataObject::get_one("ProductHolder");
return ($child) ? DataObject::get("ProductPage", "Type = 'Ch'", "") : false;
}

Does this make sense?

Avatar
Carbon Crayon

Community Member, 598 Posts

24 October 2010 at 12:14pm

Hi Zim,

If I understand correctly then you want something like this on your product page

public function getProductsOfType($Type)
{
	$Holder = $this->Parent();
	
	if($Holder && $Holder->ClassName == 'ProductHolder')
	{
		return DataObject::get('ProductPage', 'ParentID =' . $Holder->ID . " AND Type = '" . $Type . "'");
	}
}	

Then in your template you can use <% control getProductsOfType(Mo) %>.....<% end_control %>

Hope that helps :)

Aram

------------------------------------------------------------------------------

www.ssbits.com - Your one stop SilverStripe learning resource

Avatar
zim

Community Member, 135 Posts

25 October 2010 at 1:05am

Hi Aram,

thanks for reply mate.

This looks just the kind of thing i need. i can not test until tuesday as i am away at moment. I'll let you know.

Hope everything is going well :)

Avatar
zim

Community Member, 135 Posts

27 October 2010 at 4:16am

Hi Aram,

in actual fact I need to produce a productholder.ss page that shows only those products from a certain shop and type... so even tough this function is on productpage.php can I use in productholder.ss... dose that make sense?

I did as you said and put

public function getProductsOfType($Type)
{
$Holder = $this->Parent();

if($Holder && $Holder->ClassName == 'ProductHolder')
{
return DataObject::get('ProductPage', 'ParentID =' . $Holder->ID . " AND Type = '" . $Type . "'");
}
}

in the Page_Controller section of productpage.php

and then called

<% control getProductsOfType(Mo) %>
<li><a href="$Link" title="$Title">
<% control product_thumb %>
<img style="padding: 4px 0 0 4px;" src="$URL" alt="" />
<% end_control %>
</a>
</li>
<% end_control %>

but get nothing returned

sorry for being confused on this. I know it should be easy.

anyway mate. if you have any idea what i am doing wrong please let me know.... if this makes sense :)

Avatar
zim

Community Member, 135 Posts

27 October 2010 at 4:28am

... so i need to make a function that does something like

select all products where 'type' = "Mo" AND are children of current poductholder page... not all productholder pages...

any advice you can give is much appreciated Aram

Avatar
Ryan M.

Community Member, 309 Posts

27 October 2010 at 2:52pm

Edited: 27/10/2010 2:54pm

Did you set any has_one, has_many or has_many_many relationships between ProductHolder and ProductPage?

When you said you were being returned all the products regardless of which holder they belonged to, it sounded like they don't have any relationships set yet. After Aram gave you the new code, it returned nothing because there might not be any relationships set yet.

The function & the template must match, unless you're using the top-level Page.php and Page.ss files. Then you can use functions contained in Page.php anywhere on the site.