0

Here is my code which show products based on specific category

echo $this->getLayout()->createBlock('catalog/product_list')
                        ->setData('category_id', '25')
                        // ->setAttribute('product_type', $category->getName(), 'text')
                        ->setData('column_count', '4')
                        ->setData('mode', 'grid')
                        ->setData('limit', '4')
                        ->setData('name', 'product_list')
                        ->setTemplate('catalog/product/checkout_list.phtml')
                        ->toHtml();

But what I am trying to implement is, say these are product ids 2,4 and Category Id 25 have products 1,2,3,4. So I would like to show only 2 product, but the above method shows all the 4 products. Any only can help me figure out this?

1 Answer 1

0

I advise you to create a custom block and load a product collection filtered by your conditions. You would have control over what is loaded there. The collection retrieval would look something like this:

public function getLimitedProductCollection($categoryId, $limit)
{
    $collection = Mage::getResourceModel('catalog/product_collection')
                 ->addCategoryFilter($categoryId);

    $attributesToSelect = [
        'name',
        'url_key'
    ];
     
    $collection->joinAttribute(
        'small_image', 
        'catalog_product/image', 
        'entity_id', 
        null, 
        'left'
    )->addAttributeToSelect('small_image');

    $collection
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents()
        ->addAttributeToSelect($attributesToSelect)
        ->addUrlRewrite();

    Mage::getSingleton('catalog/product_visibility')
        ->addVisibleInCatalogFilterToCollection($collection);
    $collection->setPageSize($limit);

    return $collection->getItems();
}

As you can see, you can easily manipulate the number of items by specifying the $limit, you can add the $categoryId and also you can change or add attributes in the $attributesToSelect array.

Good Luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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