Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

magento product collection with specific id

Tags:

magento

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

like image 335
wutzebaer Avatar asked Nov 01 '12 13:11

wutzebaer


People also ask

How do you get the product collection which ids are in array 16 17 18 20?

Answer: $collection->addFieldToFilter('entity_id', array(16,17,18,19));

How do I get a product collection in Magento 2?

Overview of getting product collection in Magento 2 Here are two initial steps: Step 1: Declare in Mageplaza_HelloWorld Block. Step 2: Display product collection in phtml file.

How do I get all categories in Magento 2?

You can achieve functionality using the Catalog module interface Magento\Catalog\Api\CategoryListInterface with getList() method to get a category list.


1 Answers

    $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 @

  • Magento get a product collection in an arbitrary order

  • How to select mysql rows in the order of IN clause

like image 96
Renon Stewart Avatar answered Oct 25 '22 22:10

Renon Stewart