最新消息:觉得本站不错的话 记得收藏哦 博客内某些功能仅供测试 讨论群:135931704 快养不起小站了 各位有闲钱就打赏下把 My Email weicots#gmail.com Please replace # with @

Magento 2 获得目录 及子目录 备忘

Magento 资料整理 ajiang-tuzi 5619浏览

2
down vote
favorite
How can I retrieve a product collection by a root category and from all its sub-categories?

Eg:

Root Category (2 products)

Sub Category 1 (2 products)
Sub Category 2 (3 products)
So I want to retrieve all 7 products in the collection.

magento2 product category collection

Code for your class file:

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:

$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

<?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.

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

参考源
Get product collection by root category and all it’s sub categories in Magento 2?
How to get list of categories including sub categories in Magento 2? [duplicate]
Magento 2: Get parent category, children categories & product count

转载请注明:(●--●) Hello.My Weicot » Magento 2 获得目录 及子目录 备忘

蜀ICP备15020253号-1