Question

I want to use the URL http://mydomain.test/category-name.html?cat=9,10,40 I need to get all products from category "category-name" and categories with ids 9, 10 and 40.

How can I do this. Maybe there is such an extension?

Was it helpful?

Solution

If you want all the products for category 9 you can do this:

$category_id = 9;
$category = Mage::getModel('catalog/category')->load($category_id);
$products = Mage::getResourceModel('catalog/product_collection')
        ->setStoreId(Mage::app()->getStore()->getId())
        ->addCategoryFilter($category);

You can do this for the other categories.

to print the products you can do something like this:

foreach ($products as $_product) { ?>
    <a href="<?php echo $_product->getProductUrl() ?>">
        <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" />
    </a>
    <a href="<?php echo $_product->getProductUrl(); ?>">
        <?php echo $_product->getName(); ?>
    </a>
<?php } ?>

If you want a list try something like this:

$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('category_id', array(
        array('finset' => '9'),
        array('finset' => '10'),
        array('finset' => '40')
    )
    ->addAttributeToSort('created_at', 'desc');
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top