2017-10-26 11 views
0

これは長いショットだと思うが、誰かが私を助けてくれることを願っている。以下は、すべての製品を取得する部分だと思うが、すべてを表示するのではなく、カテゴリ。これは、「シンプル」と呼ばれるテーマを使用するシルバーストライプサイトです。私はSSで働いたことがないので、どんな助けもスーパーになるでしょう。シルバーストライプ、特定のカテゴリのみを表示

<?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'); 
     } 
    } 

} 

答えて

3

それは本当にオープンで幅広い質問ですが、あなたの製品を仮定すると、Categoriesからmany_manyを持っており、CategoryProductからbelongs_many_manyを持っている一般的に、それはこのようなものになります。

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'); 
} 

を私がさらにお勧めするカップルのこと:

  • Link()を0にするこれを生成するには'show/...リンク。
  • private static $default_sort = 'Sort ASCProductに使用するのではなく、毎回ソートをハードコーディングします。

関連性:https://www.silverstripe.org/learn/lessons/creating-filtered-views

関連する問題