0

I'm trying to get a list of all grouped products with no associated products.

I have the following code but it is not showing anything.

I know there is a least one grouped product with no associated products because I created a test one.

require_once '*<path-to>*/app/Mage.php';
Mage::app();

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToFilter('type_id', array('eq' => 'grouped'));
foreach ($products as $product) {
    $associatedProducts = $product->getTypeInstance(true)->getAssociatedProducts($product);
    $NoAssociatedProducts = count($associatedProducts);
    if ($NoAssociatedProducts <1){
    $grouped_name = $product->getName();
    echo $grouped_name;
    }
}

Can anyone see what's wrong?

1 Answer 1

3

You missed the below filter in the product collection

->addAttributeToSelect('*')

The name attribute of the products doesn't come with the below line of code.

$products = Mage::getModel('catalog/product')
    ->getCollection();

Instead you have too call explicitly to load that attributes to the collection shown as below.

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*');

So finally the corrected code will be as follows.

$mageFilename = 'app/Mage.php'; // Put your Mage.php path here
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app();

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*') // Added only this line to get all attributes
    ->addAttributeToFilter('type_id', array('eq' => 'grouped'));
foreach ($products as $product) {
    $associatedProducts = $product->getTypeInstance(true)->getAssociatedProducts($product);
    $NoAssociatedProducts = count($associatedProducts);
    if ($NoAssociatedProducts < 1) {
        $grouped_name = $product->getName();
        echo $grouped_name;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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