0
votes

I think this is a long shot but lets hope somebody can help me, below is the I think that part which gets all of the products, but I only want to show certain categories instead of showing all categories. This is a silverstripe site which uses a theme called 'simple'. I have never worked with SS so any help would be super.

<?php

class ConceptPage extends Page
{
    static $has_one = array(
    );

    static $many_many = array(
        'Products' => 'Product'
    );

    static $allowed_children = array(
        'none' => 'none'
    );

    function getCMSFields()
    {
        $fields = parent::getCMSFields();
        return $fields;
    }
}

class ConceptPage_Controller extends Page_Controller
{
    static $allowed_actions = array(
        'show',
    );

    public function init()
    {
        parent::init();

        Requirements::css('products/css/products.css');
    }

    //Return the list of products for this category
    public function getProductsList()
    {
    /*
    $products =  $this->Products(Null, 'Sort ASC');
        foreach($products as $product){
            $productsArray[] = $this->Link() . 'show/' . $product->URLSegment;
        }
        if(count($productsArray) > 0){
            GoogleSitemap::register_routes($productsArray);
        }
        return $products;
    */
        return $this->Products(Null, 'Sort ASC');
    }
    public function getProductsListDE()
    {
        $products =  $this->Products(Null, 'Sort ASC');
        $productsList = new ArrayList();
        foreach ($products as $product) {
            $product->Title = $product->Title_DE;
            $product->SubTitle = $product->SubTitle_DE;
            $productsList->push($product);
        }

        return $productsList;
    }

    //Get's the current product from the URL, if any
    public function getCurrentProduct()
    {
        $Params = $this->getURLParams();
        $URLSegment = Convert::raw2sql($Params['ID']);

        if($URLSegment && $Product = DataObject::get_one('Product', "URLSegment = '" . $URLSegment . "'"))
        {
            return $Product;
        }
    }

    //Shows the Product detail page
    function show()
    {
        //Get the Product
        if($Product = $this->getCurrentProduct())
        {
            $Data = array(
                'Product' => $Product,
                'MetaTitle' => $Product->Title,
                'ExtraLink' => 'show/' . $Product->URLSegment
            );

            $this->addToBreadcrumbs($Product);
            //return our $Data array to use, rendering with the ProductPage.ss template
            return $this->customise($Data)->renderWith(array('ProductPage', 'Page'));
        }
        else //Product not found
        {
            return $this->httpError(404, 'Sorry that product could not be found');
        }
    }

}
1

1 Answers

3
votes

It's a really open and broad question, but in general, assuming your product has a many_many to Categories, and Category has a belongs_many_many to Product, it would look something like this:

public function getProductsList()
{
    $categoryID = $this->request->getVar('category');
    if ($categoryID) {
      $category = Category::get()->byID($categoryID);
      if (!$category) return $this->httpError(404, 'Category not found');
      return $category->Products();
    }
    return $this->Products(Null, 'Sort ASC');
}

A couple things I would additionally recommend:

  • put a Link() method on Product to generate that 'show/... link.
  • use private static $default_sort = 'Sort ASC on Product rather than hard coding the sort every time.

Relevant: https://www.silverstripe.org/learn/lessons/creating-filtered-views