mahipal negi
mahipal negi

Reputation: 317

magento: show disabled product

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

Answers (1)

clinical
clinical

Reputation: 588

Here is my solution:

  • First get a product collection
  • Add a filter for disabled products
  • Join the category table
  • Filter the category table for specific category id's

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

Related Questions