Reputation: 2669
I'm trying get product datas using getmodel()
. My code is,
//$product_ids = $this->getproducts();
$product_ids = array(0=>1, 1=>2, 2=>3);
echo'<pre>';
$model = Mage::getModel('catalog/product');
foreach($product_ids as $id) {
$data = $model->load($id);
echo $id;
echo '<br>';
echo $data->getProductUrl();
echo '<br>';
echo $data->getName();
echo '<br>';
}
I can get the data's of each product name and url. Name is correctly displayed but the url I'm getting the same url of first product for each product in loop. But the looping is fine.
And I tried with getUrlPath()
it is also getting same url
And if I directly pass the id (not in loop) then I can get the correct url. Like
$model = Mage::getModel('catalog/product')->load(2);
echo $model->getProductUrl();
Is that any caching issue..? (But caching is disabled). these things make me mad.
And If used below code then I can get the correct url (loop),
foreach($product_ids_ids as $id) {
$_item = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('product_url')
->addAttributeToSelect('name')
->addAttributeToFilter('entity_id', $id)
->load();
foreach($_item as $product){
echo $product->getProductUrl();
echo $product->getName();
}
}
This is not making any sense. I tried reindex, checked the flat catalog -> it is not enabled. But I cannot find out what's reason. And I checked admin -> catalog -> Url rewrites having empty - No datas. May be this is the reason.
Any one can tell the reason please ?
Upvotes: 1
Views: 1037
Reputation: 11
Hi Elavarasan
Its because you are using something horrible like below as loop goes on:
Mage::getModel('catalog/product')->load(1)->load(2)->load(3);
Here is what i roughly wrote, but it should work. Try it.
$product_ids = array(
0=>1,
1=>2,
2=>3,
3=>4,
4=>5
);
//Why to load product object each time? Instead get collection, only one database call.
$productCollection = Mage::getModel("catalo/product")->getCollection();
$productCollection->addAttributeToFilter('entity_id',array('in'=>$product_ids));
foreach($productCollection as $_product) {
echo "<pre>";
echo "<br/>Id : ".$_product->getId();
echo "<br/>Name : ".$_product->getProductUrl();
echo "<br/>Url : ".$_product->getName();
echo "</pre>";
}
Upvotes: 1