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
Answer: $collection->addFieldToFilter('entity_id', array(16,17,18,19));
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.
You can achieve functionality using the Catalog module interface Magento\Catalog\Api\CategoryListInterface with getList() method to get a category list.
$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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With