简体   繁体   中英

Get category count for a category where only the child categories have products

I'm having problems getting a count for a category collection that will include products in any of that categories children.

However I don't want just a full count I want to filter that count by a product collection (so only include products in the count that appear in the product collection)...

Any suggestions?

Code to get a filtered product collection (filtered by a multiselect attribute)

    /** @var $attribute Mage_Eav_Model_Entity_Attribute */
    $valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
    ->setAttributeFilter($attribute->getId())
    ->addFieldToFilter('value', array ('like' => $make))
    ->addFieldToSelect('option_id')
    ->setStoreFilter(0, false);


$set = array();
foreach($valuesCollection as $option){
    $set[] = $option->getData('option_id');
}

$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('sparex_makemodel',
        array('in' => $set
        )
)
->addAttributeToSelect('*');

I'm getting the child categories for a given category like thus..

$childCats = Mage::getModel('catalog/category')->load(2)->getChildrenCategories();

Now none of these categories have products assigned to them, however their children (or children of children) do.

I want to produce a count for these categories that includes the child categories but only where the products are in my filtered collection.

Perhaps something like this?

$counter = 0;
foreach($_productCollection as $_prod) {
    $categories = $_prod->getCategoryIds();
    foreach($childCats as $_cat) {
        if (in_array($_cat->getId(), $categories)) {
            $counter++;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM