获得目录大全-category

magento2 product category collection

Code for your class file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
protected $_categoryHelper ;
protected $_categoryRepository ;
 
public function __construct(
     \Magento\Catalog\Helper\Category $categoryHelper ,
     \Magento\Catalog\Model\CategoryRepository $categoryRepository ,
     array $data = []
)
{
     $this ->_categoryHelper = $categoryHelper ;
     $this ->_categoryCategoryRepository = $categoryRepository ;       
     parent::__construct( $context , $data );
}
 
public function getStoreCategories()
{
     return $this ->_categoryHelper->getStoreCategories();
}
 
public function getCategory( $categoryId )
{
     return $this ->_categoryRepository->get( $categoryId );
}

Code for your template file:

1
2
3
4
5
6
7
8
9
10
11
$categories = $block ->getStoreCategories();
foreach ( $categories as $category ) {
     echo $category ->getName();
     echo ' ( ' . $category ->getProductCount() . ' )' ;
 
     $subCategories = $block ->getCategory( $category ->getId());
     foreach ( $subCategories as $subCategory ) {
         echo $subCategory ->getName();
         echo ' ( ' . $subCategory ->getProductCount() . ' )' ;
     }
}

Magento 2: Get parent category, children categories & product count
This article shows how we can get parent category, children categories and total number of products in a category in Magento 2.

Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Catalog\Model\CategoryFactory class in the constructor of my module’s block class.

Objects of class \Magento\Catalog\Helper\Category and \Magento\Catalog\Model\CategoryRepository as also used in the constructor. They will be used to print a nested list of categories and sub-categories.

app/code/Chapagain/HelloWorld/Block/HelloWorld.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace Chapagain\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{   
     protected $_categoryFactory ;
     protected $_category ;
     protected $_categoryHelper ;
     protected $_categoryRepository ;
         
     public function __construct(
         \Magento\Backend\Block\Template\Context $context ,       
         \Magento\Catalog\Model\CategoryFactory $categoryFactory ,
         \Magento\Catalog\Helper\Category $categoryHelper ,
         \Magento\Catalog\Model\CategoryRepository $categoryRepository ,       
         array $data = []
     )
     {
         $this ->_categoryFactory = $categoryFactory ;       
         parent::__construct( $context , $data );
     }
     
     /**
      * Get category object
      * Using $_categoryFactory
      *
      * @return \Magento\Catalog\Model\Category
      */
     public function getCategory( $categoryId )
     {
         $this ->_category = $this ->_categoryFactory->create();
         $this ->_category->load( $categoryId );       
         return $this ->_category;
     }
  
     /**
      * Get category object
      * Using $_categoryRepository
      *
      * @return \Magento\Catalog\Model\Category
      */
     public function getCategoryById( $categoryId )
     {
         return $this ->_categoryRepository->get( $categoryId );
     }
  
     /**
      * Retrieve current store categories
      *
      * @param bool|string $sorted
      * @param bool $asCollection
      * @param bool $toLoad
      * @return \Magento\Framework\Data\Tree\Node\Collection or
      * \Magento\Catalog\Model\ResourceModel\Category\Collection or array
      */
     public function getStoreCategories( $sorted = false, $asCollection = false, $toLoad = true)
     {
         return $this ->_categoryHelper->getStoreCategories();
     }
     
     /**
      * Get parent category object
      *
      * @return \Magento\Catalog\Model\Category
      */
     public function getParentCategory( $categoryId = false)
     {
         if ( $this ->_category) {
             return $this ->_category->getParentCategory();
         } else {
             return $this ->getCategory( $categoryId )->getParentCategory();       
         }       
     }
     
     /**
      * Get parent category identifier
      *
      * @return int
      */
     public function getParentId( $categoryId = false)
     {
         if ( $this ->_category) {
             return $this ->_category->getParentId();
         } else {
             return $this ->getCategory( $categoryId )->getParentId();
         }
     }
     
     /**
      * Get all parent categories ids
      *
      * @return array
      */
     public function getParentIds( $categoryId = false)
     {
         if ( $this ->_category) {
             return $this ->_category->getParentIds();
         } else {
             return $this ->getCategory( $categoryId )->getParentIds();
         }       
     }
     
     /**
      * Get all children categories IDs
      *
      * @param boolean $asArray return result as array instead of comma-separated list of IDs
      * @return array|string
      */
     public function getAllChildren( $asArray = false, $categoryId = false)
     {
         if ( $this ->_category) {
             return $this ->_category->getAllChildren( $asArray );
         } else {
             return $this ->getCategory( $categoryId )->getAllChildren( $asArray );
         }
     }
  
     /**
      * Retrieve children ids comma separated
      *
      * @return string
      */
     public function getChildren( $categoryId = false)
     {
         if ( $this ->_category) {
             return $this ->_category->getChildren();
         } else {
             return $this ->getCategory( $categoryId )->getChildren();
         }       
     }   
}
?>

Now, we fetch and print the category, parent category, children categories and product count in template file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
$categoryId = 23; // fetching products in category id 23
  
// Load category by category ID
$category = $block ->getCategory( $categoryId );
  
// Get Category Level
echo $category ->getLevel() . '<br />' ;
  
// Get total products associated with the category
echo $category ->getProductCount() . '<br />' ;
  
// Get array parent categories of loaded category
$parentCategories = $category ->getParentCategories();
  
// Get array of child categories of loaded category
$childrenCategories = $category ->getChildrenCategories();
  
// Get single parent category object
$block ->getParentCategory();
  
// Get only the category id of single parent category
$block ->getParentId();
  
// Get array of all parent category ids
$block ->getParentIds();
  
// Get comma-separated children categories ids
$block ->getChildren();
  
// Get comma-separated or array of all childrent categories ids
$block ->getAllChildren(); // as comma-separated
$block ->getAllChildren(true); // as an array
  
// Get nested list of categories and sub-categories along with their product count
$categories = $block ->getStoreCategories();
foreach ( $categories as $category ) {
     echo $category ->getName();
     echo ' ( ' . $category ->getProductCount() . ' )' ;
  
     $subCategories = $block ->getCategoryById( $category ->getId());
     foreach ( $subCategories as $subCategory ) {
         echo $subCategory ->getName();
         echo ' ( ' . $subCategory ->getProductCount() . ' )' ;
     }
}
转发:http://www.weicot.com/magento2-获得目录-及子目录-备忘/

 

转载于:https://my.oschina.net/ganfanghua/blog/2395763

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值