文档 下载安装 问答 主机

根据产品获取目录

今天介绍根据产品获取目录的方法。可以根据当前产品或者任意产品获取产品目录。

需要打开自定义模块(Mageplaza_HelloWorld)的块类,然后在块类的构造函数中注入对象 \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory, \Magento\Catalog\Model\ProductRepository 和 \Magento\Framework\Registry.

app/code/Mageplaza/HelloWorld/Block/HelloWorld.php

<?php
namespace Mageplaza\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{    
    protected $_categoryCollectionFactory;
    protected $_productRepository;
    protected $_registry;
        
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
        \Magento\Catalog\Model\ProductRepository $productRepository,
        \Magento\Framework\Registry $registry,
        array $data = []
        )
        {
            $this->_categoryCollectionFactory = $categoryCollectionFactory;
            $this->_productRepository = $productRepository;
            $this->_registry = $registry;
            parent::__construct($context, $data);
    }
    
    /**
     * Get category collection
     *
     * @param bool $isActive
     * @param bool|int $level
     * @param bool|string $sortBy
     * @param bool|int $pageSize
     * @return \Magento\Catalog\Model\ResourceModel\Category\Collection or array
     */
    public function getCategoryCollection($isActive = true, $level = false, $sortBy = false, $pageSize = false)
    {
        $collection = $this->_categoryCollectionFactory->create();
        $collection->addAttributeToSelect('*');        
        
        // select only active categories
        if ($isActive) {
            $collection->addIsActiveFilter();
        }
                
        // select categories of certain level
        if ($level) {
            $collection->addLevelFilter($level);
        }
        
        // sort categories by some value
        if ($sortBy) {
            $collection->addOrderField($sortBy);
        }
        
        // select certain number of categories
        if ($pageSize) {
            $collection->setPageSize($pageSize); 
        }    
        
        return $collection;
    }
    
    public function getProductById($id)
    {        
        return $this->_productRepository->getById($id);
    }
    
    public function getCurrentProduct()
    {        
        return $this->_registry->registry('current_product');
    }
}
?>

如果想使用当前产品,激活 getCurrentProduct() 方法。如果使用其他任何产品,激活 getProductById($id) 方法。然后,在 .phtml 文件中可以获取和这个产品相关连的分类 ID 的集合。添加以下代码片段:

$productId = 1; // YOUR PRODUCT ID
$product = $block->getProductById($productId);
 
// for current product
// $product = $block->getCurrentProduct();
 
$categoryIds = $product->getCategoryIds();
 
$categories = $block->getCategoryCollection()
                    ->addAttributeToFilter('entity_id', $categoryIds);
                    
foreach ($categories as $category) {
    echo $category->getName() . '<br>';
}

本教程翻译自Mageplaza模块开发系列,其他内容请点击下面链接: