0

I'm struggling to get a list of associated products from addtocart.phtml.

I've already tried all options from this and other forums but nothing is working as I wish.

I've got this:

$products = Mage::getModel('catalog/product')
                ->getCollection()
                ->addAttributeToFilter('type_id', array('eq' => 'grouped'));
            foreach ($products as $product) {
                $associatedProducts = $product->getTypeInstance(true)->getAssociatedProducts($product);
            }

But it seems is always pulling associated products only form first group product in catalog doesn't matter which group product currently viewing.

This probably mean I have to force it to get products for particular id by using $_product->getId() but I don't know how to do it as it's my first week with magento at all.

I will appreciate for any suggestions.

0

You assign all product to one variable $associatedProducts, you should use array instead of it. Try to get them in such way

$associatedProducts = array();
$products = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToFilter('type_id', array('eq' => 'grouped'));
foreach ($products as $product) {
    $associatedProducts[] = $product->getTypeInstance(true)->getAssociatedProducts($product);
}

To check result use next code:

foreach ($associatedProducts as $singleProduct) {
    foreach ($singleProduct as $product) {
       echo $product->getName().'<br/>';
    }
}
  • Thank you. That was silly to not see that $associatedProducts is overwriten every time. So I got now array with every single associated products from catalog but still can't determinate to show just for one group product. – makey Sep 12 '17 at 9:04
  • You can load product directly by Id or another attribute, for example, by id:**bold**$associatedProducts = array(); $product = Mage::getModel('catalog/product')->load(149); $associatedProducts[] = $product->getTypeInstance(true)->getAssociatedProducts($product); foreach ($associatedProducts as $singleProduct) { foreach ($singleProduct as $product) { echo $product->getName().'<br/>'; } }**bold** – olkuznw Sep 12 '17 at 9:36
  • Thank you @olkuznw again. I couldn't make your option working however find solution that do exactly what I was expected $_associatedProducts = array(); $_associatedProducts = $_product->getTypeInstance(true)->getAssociatedProducts($_product); foreach ($_associatedProducts as $product) { echo $product->getName()." - £".$product->getPrice().'"<br/>";} – makey Sep 12 '17 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.