2

I want to show some specific product on product listing page :

like product_collection.phtml

there is a code that grab the collection

  $_productCollection=$this->getLoadedProductCollection();

It show all products in particular category but I want to show few product by there ids may be array of product ids like

$productids=array(11,22,1,4);

how can I do that add filter or any other way ?..

i tried below code but it is not working . code of product_collection.phtml:

        <?php
/* added code for filter start */
        $productIds = array(1,3,2);
        $_productCollection=$this->getLoadedProductCollection()->addAttributeToFilter('entity_id', array('in' => $productIds));

/* added code for filter ends */
/*order code
$_productCollection=$this->getLoadedProductCollection();

*/
        $_helper = $this->helper('catalog/output');

    ?>
    <?php $_collectionSize = $_productCollection->count() ?>
    <?php $_columnCount = !empty($cols)?$cols:$this->getColumnCount(); ?>  
    <div class="category-products">
        <?php echo $this->getToolbarHtml(); ?>



        <div class="products-block" id="products-grid">
        <?php $i=0; foreach ($_productCollection as $_product): ?>
            <?php if ($i++%$_columnCount==0): ?>
            <div class="row products-grid">
            <?php endif ?>

                    <?php echo $_product->getName().'<br>';?>

                <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
                </div>
                <?php endif ?>
                <?php endforeach ?>
                <script type="text/javascript">decorateGeneric($$('div.products-row'), ['odd','even','first','last'])</script>
            </div>
        <div class="toolbar-bottom">
            <?php echo $this->getToolbarHtml() ?>
        </div>
    </div>
7

Instead of using getLoadedProductCollection() method, you can create your own custom collection. It should somewhat looks like this.

//use product ids that you need to show in this array
$productIds = array(11, 12, 1, 4);

//loads collection based on Ids
$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('entity_id', array('in' => $productIds));

//loop through custom collection in order to grab data
foreach ($collection as $item) {
     $item->getName();
}
  • giving downvote is ok. May I know what is the reason ? – Rajeev K Tomy Oct 13 '14 at 5:44
  • Same answer just look and fill diff this is not a good way to keep both answer same – Keyul Shah Oct 13 '14 at 5:45
  • I have not down vote your answer. – Keyul Shah Oct 13 '14 at 5:49
  • @KeyulShah:that +1 from my side for put a good answer – Rajeev K Tomy Oct 13 '14 at 5:55
1

Hello Try the Below code.

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

Or Try One of them.

$products =$this->getLoadedProductCollection();
$products ->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();
    }

In collection you have to add filter using addAttributeToFilter and pass the $productIds array.

  • please update the error – Keyul Shah Oct 13 '14 at 5:58
  • what is actual error so we can help – Keyul Shah Oct 13 '14 at 5:59
  • i wrote below code $productIds = array(1,3,2); $_productCollection=$this->getLoadedProductCollection()->addAttributeToFilter('entity_id', array('in' => $productIds)); $_helper = $this->helper('catalog/output'); it shows all products not filtered result – mcoder Oct 13 '14 at 6:02
  • does product id same as entity id? – Keyul Shah Oct 13 '14 at 6:08
  • yeah , in need to filter via product ids, see my updated quesion – mcoder Oct 13 '14 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.