11

i select products with

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('entity_id', array('in' => $productIds)); 

how can i archieve that the collection is in the same order as the ids in $productIds?

thanks

wutzebaer
  • 14,365
  • 19
  • 99
  • 170

1 Answers1

21
    $productIds = array(1,3,2);
    $products = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToFilter('entity_id', array('in' => $productIds));


    $products->getSelect()->order("find_in_set(entity_id,'".implode(',',$productIds)."')");

    foreach($products as $product)
    {
        echo $product->getEntityId();
        echo $product->getSku();
    }

See more @

Community
  • 1
  • 1
MagePal Extensions
  • 17,646
  • 2
  • 47
  • 62