2014-11-02 86 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

相關問題