0

Synopsis

I have created a Helper class which is used to collect all associated products (Simple) for a Grouped Product. To clarify the purpose of this helper, my intention is once I have the children products I will build a different array to return for my view.

I have discovered an inconsistency with the results. In the Admin I have created a Grouped Product, I associated three Simple products.

Example #1:

$children = $this->getAssociatedProducts();
Zend_Debug::dump( count($children) ); // Output: int(1)

Evidently I am missing two products by this result, coincidentally the only product output is the last associated product in the control panel. However I then queried the database and found conflicting results as expected.

Example #2:

$children = $read->fetchAll($select);
Zend_Debug::dump( count($children) ); // Output: int(3)

Furthermore I continued to run through the Magento Core line by line to understand the underlying functionality, as usual I found nothing more than over-complicated processing.

I was hoping someone could enlighten me about how getAssociatedProducts works, more so to understand why I don't receive all of my expected results.

Snippet/Examples

File: app/code/local/Myvendor/MyModule/Helper/View/Product/Grid.php

<?php

class Myvendor_Mymodule_Helper_View_Product_Grid extends Mage_Core_Helper_Abstract
{
    public function getProducts(Mage_Catalog_Model_Product $product)
    {
        // Get a blank instance of the type model for validation.
        $modelType = $product->getTypeInstance(true);
        if (! $modelType instanceof Mage_Catalog_Model_Product_Type_Grouped) {
            Mage::throwException(sprintf(
                'Argument #1 expects type %s, %s given.', 
                'Mage_Catalog_Model_Product_Type_Grouped',
                get_class($modelType)
            ));
        }

        // XXX - When Magento ORM doesn't work as intended or expected - use Zend query builder!

        $adapter        = Mage::getSingleton('core/resource');

        $tableProducts  = $adapter->getTableName('catalog/product');
        $tableRelations = $adapter->getTableName('catalog/product_relation');
        $read           = $adapter->getConnection('read');

        $select = $read->select()
            ->from(['main_table' => $tableRelations], [
                'product.*',
            ])
            ->joinLeft(['product' => $tableProducts], '(main_table.child_id = product.entity_id)', null)
            ->where('main_table.parent_id = ?', (int) $product->getId());

        $children = $read->fetchAll($select);

        Zend_Debug::dump((string) $select);
        Zend_Debug::dump(count($children));
        die;

        return array();
    }
}

1 Answer 1

1

First of all make sure you reindexed everything.

When using getAssociatedProducts the product collection is retrieved this way:

$collection = $this->getAssociatedProductCollection($product)
                ->addAttributeToSelect('*')
                ->addFilterByRequiredOptions()
                ->setPositionOrder()
                ->addStoreFilter($this->getStoreFilter($product))
                ->addAttributeToFilter('status', array('in' => $this->getStatusFilters($product)));

There are some filters in this method.
addFilterByRequiredOptions - make sure the child products don't have custom options (and they are simple or virtual).
addAttributeToFilter - make sure they are enabled

Also make sure the products are in stock or that you have the flag Show out of stock products set to yes.

1
  • Cheers for the response - The associated products do not have custom options; the show out of stock flat is set to yes; and the reindexing didn't need doing from the fact Magento didn't display the usual flash message. So i'm confused.
    – Ash
    Commented Feb 28, 2014 at 15:53

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.