[英]How to fetch the category url of the product in magento
The below code is written to fetch required product details, but I want fetch the category url of the product too, please could any one help me to fetch the url within same loop. 编写下面的代码来获取所需的产品详细信息,但是我也想获取产品的类别网址,请谁能帮助我在同一循环内获取该网址。
<?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
}
?>
Product Object never give you category url because product only given you categories ids. 产品对象从不给您类别URL,因为产品仅给您类别ID。
$products->addAttributeToSelect('categoryurl'); $产品 - > addAttributeToSelect( 'categoryurl'); is wrong
是错的
You need to load category object by category ids and product will give you categories ids 您需要按类别ID加载类别对象,产品将为您提供类别ID
Please try this: 请尝试以下方法:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.