【问题标题】:Silverstripe - show products from parent category and subcategorySilverstripe - 显示来自父类别和子类别的产品
【发布时间】:2021-07-30 09:05:33
【问题描述】:

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

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

  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和子类别的所有产品?

【问题讨论】:

    标签: silverstripe silverstripe-4


    【解决方案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]);
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2013-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多