How to get grouped products into collection

I have only grouped products visible. In my specific product collection I filter trough simple products. The results are shown in list.phtml but as simple products. What I need is the grouped Products related to the simple Product instead of the simple one shown in listview. I couldn’t figure it out how Magento’s catalog handles this.

php:

public function getMySpecialProducts($maxlength=null,$minlength=null,$maxwidth=null,$minwidth=null,$maxheight=null,$minheight=null){

        $products = Mage::getModel('catalog/product');
        $_collection = $products->getCollection();
        $_collection->addAttributeToSelect('*')
        ->addAttributeToFilter('size_length', array('lteq' => ''.$maxlength.''))
        ->addAttributeToFilter('size_length', array('gteq' => ''.$minlength.''))
        ->addAttributeToFilter('size_width', array('lteq' => ''.$maxwidth.''))
        ->addAttributeToFilter('size_width', array('gteq' => ''.$minwidth.''))
        ->addAttributeToFilter('size_height', array('lteq' => ''.$maxheight.''))
        ->addAttributeToFilter('size_height', array('gteq' => ''.$minheight.''))
        ->setPage(1, 10)
        ->addCategoryFilter($this->getMySpecialCategory())
        ->load();

        return $_collection;
    }

phtml output:

$_productCollection = $this->getMySpecialProducts(
    $range["length"]["max"],$range["length"]["min"],
    $range["width"]["max"],$range["width"]["min"],
    $range["height"]["max"],$range["height"]["min"]
);

$listView = $this->getLayout()->createBlock('catalog/product_list')
            ->setTemplate('catalog/product/list.phtml')
            ->setCollection($_productCollection);
$this->getLayout()->getBlock('content')->append($listView);
echo $listView->toHTML();

Any help appreceated.Thanks.

2 thoughts on “How to get grouped products into collection”

  1. Could you please try following codes :

    $collectionGrouped = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToFilter('type_id', array('eq' => 'grouped'));
    

    or

    // Get the grouped product a simple product belongs to
    $simpleProduct->loadParentProductIds();
    $parentProductIds = $simpleProduct->getParentProductIds();
    

    or

    $productCollection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('type_id', array('eq' => 'grouped'))
    ->addAttributeToFilter('size_length', array('lteq' => ''.$maxlength.''))
    ->setPage(1, 10)
    ->addCategoryFilter($this->getMySpecialCategory())
    ->load();
    

  2. solution: 2 seperate collections, 1st simple products -> get parent id’s, 2nd get grouped ones depending on id.

    following is added to the code above.
    php:

    public function getGroupCollection($groupId){
        $str = array();
        foreach($groupId as $value){
            foreach($value as $id){
                array_push($str, array('attribute'=>'entity_id', 'eq' => ''.$id.''));
            }
        }
    
        $products = Mage::getModel('catalog/product');
        $_groupCollection = $products->getCollection();
        $_groupCollection->addAttributeToSelect('*');
        $_groupCollection->addAttributeToFilter('type_id', array('eq' => 'grouped'));
        $_groupCollection->addAttributeToFilter($str);
        $_groupCollection->addCategoryFilter($this->getFormatfinderCategory())
        ->load();
    
        return $_groupCollection;
    }
    

    frontend:

    // get ID's of Parent Product (grouped products) from 1st collection
    $parentProductIds = array();
    foreach($_productCollection as $product){
        $product->loadParentProductIds();
        array_push($parentProductIds, $product->getParentProductIds());
    }
    

    create html block:

    $listView = $this->getLayout()->createBlock('catalog/product_list')
                ->setTemplate('catalog/product/list.phtml');
    
    $_group = $this->getGroupCollection($parentProductIds);
    $listView->setCollection($_group);
    
    $this->getLayout()->getBlock('content')->append($listView);
    echo $listView->toHTML();
    

    Its sure not the best solution, as for me it works fine.

Leave a Reply

Your email address will not be published. Required fields are marked *