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();
}
}