开发者

Magento - filter product collection by multiple SKU's

开发者 https://www.devze.com 2023-02-13 17:11 出处:网络
I know it\'s possible to filter a collection by one SKU, as below: $_testproductCollection = Mage::getResourceModel(\'catalog/product_collection\')

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

精彩评论

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

关注公众号