B-219 Sec-55 Noida, India
+918010221733

Get All Child Products in Magento

This script will return all child products from a Magento shop bypassing the category folders. This sample will also take al products shuttle them and return only three.

    require_once ‘webshop/app/Mage.php’;
    umask(0);
    Mage::app(‘default’);
    $newproducts = array();
    $id = 10; // Id of the category we want to start at
    $cat = Mage::getModel(‘catalog/category’);
    $cat->load($id);
    $products = $cat->getProductCollection();
    $subs = $cat->getAllChildren(true); // Get’s all children products (bypassing categories)
    $children = array();
    foreach($subs as $cat_id) {
    $category = new Mage_Catalog_Model_Category();
    $category->load($cat_id);
    $collection = $category->getProductCollection();
    foreach ($collection as $product) {
    $children[] = $product->getId();
    }
    }
    shuffle($children); // Mix them up
    $children = array_slice($children, 0, 3); // Get only 3
    foreach($children as $var => $value)
    {
    $product = Mage::getModel(‘catalog/product’)->load($value);
    echo ‘<div>
    <a href=”webshop/’.str_replace(“.html”,”/”,$cat->getUrlPath()).$product->getUrlPath().'”><img src=”‘.$product->getThumbnailUrl().'” width=”55″ /></a>
    <p><strong><a href=”webshop/’.str_replace(“.html”,”/”,$cat->getUrlPath()).$product->getUrlPath().'”>’.$product->getName().'</a></strong><br />
    <span>&pound;’.number_format($product->getFinalPrice(),2 ).'</span><br />
    ‘.$product->getShortDescription().’ <a href=”webshop/’.str_replace(“.html”,”/”,$cat->getUrlPath()).$product->getUrlPath().'”>more</a></p>
    <div></div>
    </div>’;
    }

(Visited 84 times, 1 visits today)

Leave a reply

You must be logged in to post a comment.