开发者

Magento - filter product collection by multiple SKU's

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')));
$_testprodu开发者_开发知识库ctCollection->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')))


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

->addAttributeToFilter( 'sku', array( 'in' => array( 'my-sku1', 'my-sku2', 'my-sku3' ) ) )


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.


You need split 'addAttributeToFilter' or split second params in this functionly example:

$collection->addAttributeToFilter(
array(
    array('attribute'=>'sku','like'=>"%$sku%"),
    array('attribute'=>'sku','like'=>"%$sku%")
));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜