Silverstripe - 显示来自父类别和子类别的产品

时间:2021-05-08 08:13:52

标签: silverstripe silverstripe-4

当我尝试从父类别及其子类别循环所有产品时遇到问题。

我创建了两种额外的页面类型:

  1. 产品类别
  2. 产品

SiteTree(页面)中,我创建了结构:

 |-Pages
    |- Dental equipment (productCategory)
           |----- Sub category (productCategory)
                      |----- Sub sub category (productCategory)
                                    |----- Sub sub sub category (productCategory)
                                           |----- Product 1 (product type)
                                           |----- Product 2 (product type)
                                           |----- Product 3 (product type)
                                           |----- Product 4 (product type)

现在在 ProductCategory_Controller 中,我创建了循环所有不工作的产品的方法。

 public function Products()
    {

        $products = Product::get()->filter([
            'ParentID' => $this->ID
        ]);

        return  $products;
    }

我的问题是如何获取属于所有parenet和子类别的所有产品?

2 个答案:

答案 0 :(得分:1)

如果您正在尝试获取当前类别页面和所有父类别页面的产品,那么下面的示例代码可能会为您指明正确的方向。

如果我误解了您的问题,请发表评论,我会更新我的答案。

<?php

namespace {
    class ProductCategoryPageController extends PageController
    {
        public function ProductsInCurrentPageTree()
        {
            $currentPage = $this->data();
            $parentPage = $currentPage->getParent();
            if (!$parentPage) {
                // No parent page, return the current page products
                return ProductPage::get()->filter('ParentID', $currentPage->ID);
            }

            $pageTreeIds = [
                $currentPage->ID,
                $parentPage->ID,
            ];

            while ($parentPage = $parentPage->getParent()) {
                // Any conditions to break out of the loop, for instance, if your category/product pages does not start from the root level
                if (!$parentPage instanceof ProductCategoryPage) {
                    break;
                }
                $pageTreeIds[] = $parentPage->ID;
            }
            
            return ProductPage::get()->filterAny(['ParentID' => $pageTreeIds]);
        }
    }
}

答案 1 :(得分:0)

对子类别页面递归调用相同的函数,但当没有子类别时,则将产品添加到数组列表中。未经测试的代码,但应该足够清楚:

class ProductCategoryPageController extends PageController {

    public function ProductsRecursive($iParentID,$productList) {
        $productCategoryPages = ProductCategoryPage::get()->filter(['ParentID' => $iParentID]);
        if ($productCategoryPages) {
            //assuming product category pages only have children and no products themselves
            foreach ($productCategoryPages as $productCategoryPage) 
                $productList = $this->ProductsRecursive($productCategoryPage->ID,$productList);
        }
        else {
            //assuming you only are collecting product pages that have no children
            foreach (ProductPage::get()->filter(['ParentID' => $iParentID]) as $productPage)
                $productList->push($productPage);
        }
        return $productList;
    }

    public function Products() {
        return $this->ProductsRecursive($this->ID, ArrayList::create());
    }
}