开发者

Magento: Filter products by Status

I'm having some serious Magento issues here. As expected the following:

$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeTo开发者_JAVA技巧Filter('status', array('eq' => 1));

Will return all enabled products for my $category_id. However this:

$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status', array('eq' => 0));

Does not return disabled products. I can't seem to find a way to return disabled products, and I don't know why.

I've tried this:

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);

Which was meant to have worked, but apparently may have been deprecated.

Does anyone know how to get all products in a category, enabled and disabled?


Don't worry, you simply got trapped by a very unusual constant definition^^. Just try:

$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter(
    'status',
    array('eq' => Mage_Catalog_Model_Product_Status::STATUS_DISABLED)
);

For whatever reasons Varien decided to define this STATUS_DISABLED constant with a value of 2, instead of the more intuitive (and commonly used) value of 0.


I think you can do this by setting store to default like

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

make sure you save the current store value and set it back after doing the above.

Or by using a script from outside magento and invoke mage by

require_once '../app/Mage.php';
$app = Mage::app();
Mage::register('isSecureArea', true);


$products = Mage::getModel('catalog/category')->load($category_id)
        ->getProductCollection()
        ->addAttributeToSelect('*') 
        ->addAttributeToFilter('status', 1) 
        ->addAttributeToFilter('visibility', 4) 
        ->setOrder('price', 'ASC'); 


I haven't found an answer as such to my question above. But I have saved a lot of time by using a different method.

I exported a CSV of all the products in my Magento, deleted all columns except Category ID and SKU (this is all I needed), and then filtered that to return all the skus instead.

If it helps anyone here is the code -

<?php
$file  = fopen('allprods.csv', 'r');

// You can use an array to store your search ids, makes things more flexible.
// Supports any number of search ids.
$id = array($_GET['id']);    
// Make the search ids safe to use in regex (escapes special characters)
$id = array_map('preg_quote', $id);
// The argument becomes '/id/i', which means 'id, case-insensitive'
$regex = '/'.implode('|', $id).'/i';

$skus = array();
while (($line = fgetcsv($file)) !== FALSE) {  
    list($ids, $sku) = $line;

    if(preg_match($regex, $ids)) {
        $skus[] = $sku;
    }
}

$count = count($skus);
$i = 1;

echo $category_id;
foreach ($skus as $sku){
    echo $sku;
    if($i != $count) { echo "`"; }
    $i++;
}

This solution was created by using a previous topic about filtering CSVs, here

So for now, I can survive. however - an answer to this question is still needed!


Nothing works if catalog_flat_product is on in backend configuration->catalog.

try this..this will give all the products enabled and disabled ultimately

$collection = Mage::getResourceModel('catalog/product_collection'); //this will give you all products
foreach($collection as $col)
{
$var = Mage::getModel('catalog/product')->loadByAttribute('sku',$col->getSku());
echo"<pre>";print_r($var->getData());echo"</pre>";
}

its really simple and after this you can easily filter products by status

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜