如何在magento中获取产品的类别URL

时间:2014-11-02 07:39:40

标签: php magento magento-1.7

以下代码是为了获取所需的产品详细信息而编写的,但我也想获取产品的类别网址,请任何人帮我在同一个循环中获取网址。

<?php
       error_reporting(E_ALL | E_STRICT);
       define('MAGENTO_ROOT', getcwd());
       $mageFilename = MAGENTO_ROOT . '/app/Mage.php';
       require_once $mageFilename;
       Mage::setIsDeveloperMode(true);
       ini_set('display_errors', 1);
       Mage::app();
       $products = Mage::getModel("catalog/product")->getCollection();
       $products->addAttributeToSelect(array('name','price','producturl','image'));
       $products->addAttributeToSelect('categoryurl'); // Is this correct
       $products->addAttributeToFilter('status', 1);
       $products->addAttributeToFilter('visibility', 4);

       foreach ($products as $product){

        $sku = $product->getSku();
        $name = $product->getName();
        $currentCat = $product->getCategoryUrl(); // I want to fetch the category url of the product

      }

   ?>

2 个答案:

答案 0 :(得分:1)

产品对象永远不会为您提供类别网址,因为产品仅为您提供类别ID。

$产品 - &GT; addAttributeToSelect( 'categoryurl');是错的

您需要按类别ID加载类别对象,产品会为您提供类别ID

答案 1 :(得分:0)

请试试这个:

foreach ($products as $product){

    // get a list of categories for each product
    $categories = $product->getCategoryCollection()->addUrlRewriteToResult(); 

    // get the category url for each category assigned to the product
    foreach ($categories as $category) { 
        $categoryUrl = $category->getUrl();
    }

    // get the product url
    $productUrl = $product->getProductUrl(); 
}