2014-11-02 103 views
0

下面的代码是为获取所需的产品信息而编写的,但我也想获取产品的类别url,请任何人都可以帮助我在同一个循环中获取url 。如何获取magento中产品的类别url

<?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 

     } 

    ?> 

回答

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(); 
} 
+0

谢谢,它的工作... – 2014-11-03 08:44:34

1

产品对象从来没有给你类别的网址,因为产品只给你类别的ID。

$ products-> addAttributeToSelect('categoryurl');是错误的

您需要通过类别ID和产品加载类对象会给你类别IDS