Magento2所有儿童类别的产品数量和最低价格

时间:2016-11-02 06:30:35

标签: php magento2

我需要获得父类别的产品数量和最低价格(计数和最低价格应根据其所有子类别计算)。我使用以下代码

$categoryHelper = $this->helper('Magento\Catalog\Helper\Category');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
foreach($categoryHelper->getStoreCategories() as $category):
$category = $objectManager->create('Magento\Catalog\Model\Category')->load($category->getId());
    $_outputhelper    = $this->helper('Magento\Catalog\Helper\Output');                 
    $products = $category->getProductCollection()->addAttributeToSelect('*')->setOrder('price','ASC')->setPageSize(1);
    $fromprice = 0;
    foreach($products as $product){
        $fromprice = $product->getPrice();  
    }

    $count = $category->getProductCount();
endforeach;

但是上面的代码只给出了父类别的产品数量和最低价格。任何人都可以帮助,以便代码从子类别计算吗?

getProductCount()也会给出错误的结果。它包括所有产品甚至残疾产品

由于

1 个答案:

答案 0 :(得分:0)

尝试使用当前类别获取子类别:

$subcategory = $category->getChildrenCategories();

foreach($subcategory as $subcat) {
    echo $subcat->getName();
}