I need to loop through all products to build a xml - file but my script ceeps running for hours, and with every loop it seems getting slower. What can i do to speed it up ? First i loop through the category tree, when i find a "leaf" (endpoint) of a category, i try to get all of it's products with the following script:
$collection = Mage::getModel('catalog/category')
->load($catId)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('category_id', $catId)
->addAttributeToFilter('type_id', 'simple')
->addAttributeToFilter('status', 1)->load();
Consideration Nr. 1:
Mage::getModel('catalog/product') in the foreach-loop:
foreach($collection as $col) {
$prod = Mage::getModel('catalog/product')->load($col->getId());
}
But the performance is horrible
Consideration Nr. 2: ceeping the Mage::getModel('...') out of the loop by just loading a new id inside the loop
$prod = Mage::getModel('catalog/product');
foreach($collection as $col) {
$prod->load($col->getId());
}
The performance is much better but some product values are alway the same so i think this solution wound't work ?
The best way would be using the $collection data itself but i'miss some important data in the $collection.
How can i get all attributest of a product in a collection when i try to get all attributes by using the $collection i get this:
foreach($collection as $col) {
echo "<pre>";
print_r(getAttributes($col));
echo "</pre>";
exit;
}
function getAttributes($prod) {
$attributes = $prod->getAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getIsVisibleOnFront()) {
$value = $attribute->getFrontend()->getValue($prod);
echo "The Value " . $value;
if($value=='keine Angabe' || !$value)
continue;
if ($attribute->getFrontendInput() == 'price' && is_string($value)) {
$value = Mage::app()->getStore()->convertPrice($value, true);
}
if (is_string($value) && strlen($value)) {
$data[$attribute->getAttributeCode()] = array(
'label' => $attribute->getStoreLabel(),
'value' => $value,
'code' => $attribute->getAttributeCode()
);
}
}
}
return $data;
}
8 [color] => Array ( [label] => color [value] => No [code] => color )
But when i use the getModel i get the correct value
$_product = Mage::getModel('catalog/product')->load($col->getId());
print_r(getAttributes($_product));
[color] => Array ( [label] => color [value] => white [code] => color )
Is there any way to get all attributest by using the collection ?
Thx