Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all enable and disable products from a specific category?

i am trying to get all enabled and disabled products and i am using this code :

/*$categoryId = 3; // a category id that you can get from admin
    $category = Mage::getModel('catalog/category')->load($categoryId);

    $collection = Mage::getModel('catalog/product')
                    ->getCollection()
                   ->addCategoryFilter($category)
                    ->addAttributeToSelect('*')
                    ->addAttributeToFilter('status', array('gt' => 0))
                    ->load();

$categoryId = 3; // a category id that you can get from admin
    $category = Mage::getModel('catalog/category')->load($categoryId);*/

above code brings enabled products only.

By commenting status filter it still brings the same result i.e. only enabled products.

    /*$collection = Mage::getModel('catalog/product')
                    ->getCollection()
                   ->addCategoryFilter($category)
                    ->addAttributeToSelect('*')
                    //->addAttributeToFilter('status', array('gt' => 0))
                    ->load();*/

It still brings only Enabled products. But when i comment category check than it brings n all products :( Can anyone help plz ?

Note:

For those who are not clear about this query, let me tell you that Status Enabled = 1 and Status Disabled = 2.

So status greater than zero should bring me both enabled and disabled products, but it is not doing so. So any idea ???

I Edit the code and it

$collection = Mage::getModel('catalog/category')->load(3)
                    ->getProductCollection()
                    ->addAttributeToSelect('*')
                    ->addAttributeToSort('entity_id', 'ASC');

die((string) $collection->getSelect());

And this bring in this query :

SELECT `e`.*, `cat_index`.`position` AS `cat_index_position` FROM `catalog_product_entity` AS `e` INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id=1 AND cat_index.category_id='3' ORDER BY `e`.`entity_id` ASC
like image 591
atif Avatar asked Dec 04 '22 15:12

atif


1 Answers

I have found why is this happening.

This is because when you want to get products from category using $productCollection->addCategoryFilter() or using $category->getProductCollection() query use product to category connection INDEX table. When you run re-index that product category connection INDEX table is populated only with Enabled products, and you can not get disabled products on that way. Is this bug in Magento or not I do not know, you can use Raw SQL query to get your products from a specific category.

like image 194
Milos Glavinić Avatar answered Jan 20 '23 18:01

Milos Glavinić