Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Show parent product (grouped product) from a list of simple products
 
sanctuary29
Jr. Member
 
Total Posts:  1
Joined:  2009-06-12
 

HI.

I am trying to show a list in homepage of:

- most viewed
- most ordered.

I am using grouped products and simple products associated to them. I only show in the catalog the grouped products, the simple products are hidden. The magento cart and wishlist are inteligent enough to point the simple products to their grouped product page, instead of the simple product own url.

I am using the following code that works perfect to get both arrays (most views and most ordered):

<?php                               
function getBestsellers($showTotal 2$type 1)
{
    
// Array of arrays
    
$_arrayOfBestsellers getArrayOfBestsellers($showTotal$type);

    
// Array of Mage_Catalog_Model_Product
    
$_bestsellers = array();

    if(
count($_arrayOfBestsellers) > 0)
    
{
        
foreach ($_arrayOfBestsellers as $blueprint)
        
{
            $_product 
Mage::getModel('catalog/product');
            
$_product->load($blueprint['entity_id']);

            
/**
                 * NOTE: Default Mage_Catalog_Model_Product does not have some fields like "ordered_qty",
                 * by using "$_product->addData($blueprint)" we add those fields to object 
                 */
            
$_bestsellers[] $_product->addData($blueprint);
        
}
    }

    
return $_bestsellers;
}

function getArrayOfBestsellers($showTotal 2$type 1)
{
    
// get most ordered
    
if($type == 1{
        $_arrayOfBestsellers 
Mage::getResourceModel('reports/product_collection')
        ->
addAttributeToSelect('*')
        ->
addOrderedQty()
        ->
setOrder('ordered_qty''desc')
        ->
getSelect()->limit((int)$showTotal)->query();
    
}
    
    
// get most viewed
    
if($type == 2{
        $_arrayOfBestsellers 
Mage::getResourceModel('reports/product_collection')
        ->
addAttributeToSelect('*')
        ->
addViewsCount()
        ->
getSelect()->limit((int)$showTotal)->query();
    
}

    
return $_arrayOfBestsellers;                                
}

// get 3 most viewed products
$_productCollection getBestsellers(3,2);
?>

The problem is the object has all of the values for each simple product, but not the url or the name of the parent group where it belongs.

The user can;t go to the url of the simple product. User must be redirected to the parent group. Magento does that on cart and wishlist.

I think I must join somehow the query to get products, but I have no idea.

Any ideas????

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top