Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Not able to get a URL
 
number25
Member
 
Total Posts:  64
Joined:  2008-07-12
 

Im trying to retrieve a product and then retrieve its url. here is the code

echo "test";
$products Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('entity_id''134');
$the_product $products->load();
echo 
$the_product->getUrlPath();

but I keep getting this error

Fatal errorCall to undefined method Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection::getUrlPath() in C:\Windows\EasyPHP 2.0b1\www\magento\practice.php on line 48

anyone have any ideas ??

 
Magento Community Magento Community
Magento Community
Magento Community
 
crius
Guru
 
Avatar
Total Posts:  623
Joined:  2007-10-16
Denmark
 

You are trying to get an URL for a collection. That makes no sense. If you want the URL for a specific product, just do this:

$product Mage::getModel('catalog/product')->load(134);
echo 
$product->getUrlPath();
 
Magento Community Magento Community
Magento Community
Magento Community
 
Anton Makarenko
Magento Team
 
Avatar
Total Posts:  184
Joined:  2008-05-13
Los Angeles, CA
 

Try this one

...
$products $products->load();
foreach (
$products as $_product{
    $_product
->getUrlPath();
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
number25
Member
 
Total Posts:  64
Joined:  2008-07-12
 

yes but I thought that by me adding addAttributeToFilter() that narrows the search down to one entity ?? Maybe I am just misunderstanding what a collection is. And plus, how would I go about searching for a product using the sku number instead of entity number ??

 
Magento Community Magento Community
Magento Community
Magento Community
 
crius
Guru
 
Avatar
Total Posts:  623
Joined:  2007-10-16
Denmark
 

A collection can have any number of items, including 0 or 1. It is still af collection of products, not a product. You have a collection containing one product.

 
Magento Community Magento Community
Magento Community
Magento Community
 
crius
Guru
 
Avatar
Total Posts:  623
Joined:  2007-10-16
Denmark
 

You can get a product by SKU in this way:

$productId Mage::getResourceModel('catalog/product')->getIdBySku('ac674');
$product Mage::getModel('catalog/product')->load($productId);
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top