开发者

Magento:constructing product URL

开发者 https://www.devze.com 2023-02-26 19:26 出处:网络
I have been able to get attributes like name, image UR开发者_运维技巧L, description from a magento store. However getting the product URL is tedious. Any help?? Thanks.

I have been able to get attributes like name, image UR开发者_运维技巧L, description from a magento store. However getting the product URL is tedious. Any help?? Thanks.

include_once 'app/Mage.php';
umask(0);
Mage::app();

$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect('*');
$products->load(); 
    $baseUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);

    foreach ($products as $id => $product):
   $output .= '
   <product>
   <name><![CDATA['. $product['name'] .']]></name>
   <description><![CDATA['. $product['description'] .']]></description>
   <url>'. $product['url_key'] .'</url>
   <image>'. $baseUrl ."media/catalog/product". $product['image'] .'</image>';
   </product>


The product URL is just:

$product->getProductUrl()

In fact, it is better if you use the getters and setters instead of array access on Magento objects because it allows other developers to provide overrides in a conventional way. So instead of $product['description'] please use $product->getDescription(). It will avoid problems in future.


Instead of calculating the base URL yourself you can simply use:

'<image'>.Mage::helper('catalog/image')->init($product).'</image>'

The helper has a __toString() function built in so automatically produces the URL when used as a string. It will also take care of watermarks, resizing, cropping and all the other things you normally see in action but take for granted.

0

精彩评论

暂无评论...
验证码 换一张
取 消