How to get products from a particular category in magento ecommerce

后端 未结 6 1307
广开言路
广开言路 2020-12-05 11:50

I\'d like to get a list of random products from the same category as the current product for displaying within the product view - so far all I\'ve dug up is

Magento

6条回答
  •  天命终不由人
    2020-12-05 12:45

    what I ended up doing is in app/design/frontend/default/theme_name/template/catalog/product/list_random.phtml

    doing something like:

    getCurrentChildCategories();
    
    $_category = $this->getCurrentCategory();
    $subs = $_category->getAllChildren(true);
    $result = array();
    foreach($subs as $cat_id) {
        $category = new Mage_Catalog_Model_Category();
        $category->load($cat_id);
        $collection = $category->getProductCollection();
        foreach ($collection as $product) {
            $result[] = $product->getId();
        }
    
    }
    shuffle($result);
    ?>
    

    this will get you an array of product id's. You can loop through them and create products on the fly using:

    load($_product_id);
        //do something with the product here
    }?>
    

    then, create a static block in the cms with the following content

    {{block type="catalog/navigation" template="catalog/product/list_random.phtml"}} 
    

    Finally, in the Catalog->Manage categories section, choose the category, then the display settings tab. Switch the display mode to "Static block and products" and then choose your block from the drop list.

    And that should do it.

提交回复
热议问题