Jump to:

23375 Posts in 18175 Topics by 2866 members

General Questions

SilverStripe Forums » General Questions » How do I return only products from specific shop

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

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

Page: 1
Go to End
Author Topic: 1070 Views
  • zim
    Avatar
    Community Member
    135 Posts

    How do I return only products from specific shop Link to this post

    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?

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: How do I return only products from specific shop Link to this post

    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

  • zim
    Avatar
    Community Member
    135 Posts

    Re: How do I return only products from specific shop Link to this post

    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

  • zim
    Avatar
    Community Member
    135 Posts

    Re: How do I return only products from specific shop Link to this post

    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

  • zim
    Avatar
    Community Member
    135 Posts

    Re: How do I return only products from specific shop Link to this post

    ... 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

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: How do I return only products from specific shop Link to this post

    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.

    1070 Views
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.