Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - filter product collection by multiple SKU's

Tags:

magento

I know it's possible to filter a collection by one SKU, as below:

$_testproductCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('SKU', array('like'=>array('my-sku')));
$_testproductCollection->load();

But is there some way to add a list of SKU's, like the following (which doesn't work):

->addAttributeToFilter('SKU', array('like'=>array('my-sku1, my-sku2, my-sku3')))
like image 499
Marlon Creative Avatar asked Feb 25 '11 18:02

Marlon Creative


2 Answers

I haven't tested this, but I believe something like this should work:

->addAttributeToFilter( 'sku', array( 'in' => array( 'my-sku1', 'my-sku2', 'my-sku3' ) ) )
like image 196
Josh Avatar answered Jan 09 '23 19:01

Josh


For future adventurers.

$productSkuString = 'PROD001, PROD002, PROD003';
$productIds = explode(', ', $productSkuString);

$products = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect('*') // <- careful with this
    ->addAttributeToFilter(
        'sku', array('in' => $productIds)
    )
    ->load();

// usage
foreach($products as $prod) {
    echo $prod->getName();
}

Tested in 1.7.0.2 and 1.8.1.0.

like image 29
Jongosi Avatar answered Jan 09 '23 19:01

Jongosi