Reputation: 317
I used this code:-
$category_id = 5;
$products = Mage::getModel('catalog/category')
->load($category_id)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status',array(
'eq' => Mage_Catalog_Model_Product_Status::STATUS_DISABLED)
);
it's doesn't work. it's show empty result.
Please let me know how to solve this problem.
Thanks in advance.
Upvotes: 3
Views: 658
Reputation: 588
Here is my solution:
This is a working example if you want to run the script outside magento:
require_once('app/Mage.php');
umask(0);
Mage::app();
// set category id
$categoryIds = [4]; // add child category id's if you want to support child categories also
$productCollection = Mage::getModel('catalog/product')->getCollection();
$productCollection->addAttributeToSelect('*');
$productCollection->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
$productCollection->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
->addAttributeToFilter('category_id', array('in' => $categoryIds))
->addAttributeToSelect('*');
$filteredProducts = $productCollection->getItems();
foreach ($filteredProducts as $product) {
echo $product->getId() . PHP_EOL;
}
Inside magento:
// set category id
$categoryIds = [4]; // add child category id's if you want to support child categories also
$productCollection = Mage::getModel('catalog/product')->getCollection();
$productCollection->addAttributeToSelect('*');
$productCollection->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
$productCollection->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
->addAttributeToFilter('category_id', array('in' => $categoryIds))
->addAttributeToSelect('*');
$filteredProducts = $productCollection->getItems();
If you want to support multiple categories just add the id's to the array $categoryIds
If you have questions feel free to add a comment. If you got it working please let me know and add you magento version aswell. Thanks
Upvotes: 1