只计算magento中的活跃产品

时间:2014-02-07 07:18:59

标签: magento

我只想在列表中仅显示有效产品。我有一个脚本,计算所有启用和禁用产品,但我想只计算有效产品。 我的代码是: -

  <?php 
    $_categories = Mage::getBlockSingleton('catalog/navigation'); 
    foreach ($_categories->getStoreCategories() as $_category) { 
    $category = Mage::getModel('catalog/category'); 
    $category->load($_category->getId()); 
    $subcategories = explode(',', $category->getChildren()); 
    $_count = is_array($subcategories)?count($subcategories):$subcategories->count(); 
    $pcount = array();
    foreach ($subcategories as $subcategoryId) { 
        $subcategory = Mage::getModel("catalog/category")->load($subcategoryId);
        if($subcategory->getIsActive()){ 
        $pcount[] = $subcategory->getProductCount();  
        }
    }
    }

我尝试了如果条件getIsActive但它无法工作它获得所有启用和禁用产品,建议我在哪里我做一些更改只计算活动产品。

3 个答案:

答案 0 :(得分:2)

如果您希望活动产品与其所在类别无关,则可以执行以下操作:

$collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes()) //add the attributes visible in product list to the collection
            ->addMinimalPrice() //add the prices to the collection
            ->addFinalPrice()
            ->addTaxPercents()
            ->addUrlRewrite(); //add the url rewrites to the collection
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection); //filter only active products
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection); //filter only visible in catalog products

然后循环浏览集合并对每个产品执行某些操作

foreach ($collection as $product) {
    //do something with $product
}

如果您想获得该系列中的产品数量,请执行以下操作:

$size = $collection->getSize();

如果您想要执行上述操作,但只需使用一个类别:

$categoryId = 10;//replace with own id.
$category = Mage::getModel('catalog/category')->load($categoryId);

$collection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes()) //add the attributes visible in product list to the collection
                ->addMinimalPrice() //add the prices to the collection
                ->addFinalPrice()
                ->addTaxPercents()
                ->addUrlRewrite($categoryId); //add the url rewrites to the collection
$collection->addCategoryFilter($category);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection); //filter only active products
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection); //filter only visible in catalog products

答案 1 :(得分:2)

addVisibleFilterToCollectionaddSaleableFilterToCollection已弃用,在较新版本的Magento中不会执行任何操作。 你应该使用:

$collection->addAttributeToFilter('status', array('in'=>Mage::getSingleton('catalog/product_status')->getSaleableStatusIds()));
$collection->addAttributeToFilter('visibility', array('in'=>Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds()));

答案 2 :(得分:0)

在你的foreach循环中,你需要编辑类似的东西:

$collection = $category->getProductCollection()
                ->addAttributeToFilter('status', array('eq'=>'1'));
$pcount = $collection->count();

它应该做你想要的事情。如果没有,请告诉我。 干杯!