4

I am showing Most popular products and I want to be able to exclude certain products from showing.

I thought the best way would be to exclude a category and when I want to exclude a product I can put it it the exclude category.

I need this to work even if the product is in a different category.

I am showing Most popular products with this:

$storeId2 = Mage::app()->getStore()->getId();
    $products2 = Mage::getResourceModel('reports/product_collection')
        ->addOrderedQty()
        ->addAttributeToSelect('*')
        ->addAttributeToSelect(array('name', 'price', 'small_image'))
        ->setStoreId($storeId2)
        ->addStoreFilter($storeId2)
        ->setOrder('ordered_qty', 'desc'); 

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products2);         
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products2);

$x = 1;
    foreach($products2 as $prod2){
        $inventory2 = Mage::getModel('cataloginventory/stock_item')->loadByProduct($prod2);
        $actual_stock3 = (int)$inventory2->getQty();

    if ($actual_stock3<1){
    echo $prod2->getName() . ' - ' . $actual_stock3 . ' - ' . number_format($prod2->getData('ordered_qty'), 0) . '</br>';   
    if ($x++ == 10) break;  
    }
    }

How do I exclude all products in, lets say category 151 even if the product is in another category?

  • 2
    Instead of putting a product in a category in order to be excluded from the most popular, why not add a new product attribute called 'exclude_from_most_popular'. then you can filter by that. – Marius Jul 27 '16 at 9:24
  • Ah yes, seems simple now. Let me try it. – Mike Tim Turner Jul 27 '16 at 9:27
7

You can left join the catalog_category_product table to match products in your "exclude category", then filter your collection to only return products with no matches in the joined table:

This should work, given the "exclude" category as $excludeId:

$products2->joinTable(
    ['exclude_category' => 'catalog/category_product'],
    'product_id=entity_id',
    ['category_id'],
    ['category_id' => $excludeId],
    'left'
);
$products2->getSelect()->where(new Zend_Db_Expr('exclude_category.category_id IS NULL'));

Update

I see, you are already joining the category index with addVisibleInCatalogFilterToCollection() which results in a duplicate column "category_id". I updated the code to use an alias for the joined table, exclude_category.

  • Can you show this in my code please, cant get it to work. I'm obviously doing it wrong :) – Mike Tim Turner Jul 27 '16 at 9:42
  • @MikeTimTurner See update. Add the code before $x=1 – Fabian Schmengler Jul 27 '16 at 9:53
  • btw, I tried it out and had to remove the addAttributeToSelect lines because in my case these attributes were not available in the reports collection. But YMMV – Fabian Schmengler Jul 27 '16 at 9:55
  • I didn't have to remove this, It worked great. thank you :) – Mike Tim Turner Jul 27 '16 at 10:15

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.