Magento getProductUrl()未返回正确的url

Magento getProductUrl()未返回正确的url,magento,Magento,我一直在获取产品集合中的产品url。我有这段代码来获取设置的特色产品的产品集合 $_productCollection = Mage::getModel('catalog/product') ->setStoreId(Mage::app()->getStore()->getId()) ->getCollection(); $_productCollection->addAttributeT

我一直在获取产品集合中的产品url。我有这段代码来获取设置的特色产品的产品集合

 $_productCollection = Mage::getModel('catalog/product')
                  ->setStoreId(Mage::app()->getStore()->getId())
                  ->getCollection();

 $_productCollection->addAttributeToFilter('feat_enabled', 1);
 $_productCollection->setVisibility(array(2,3,4));
 $_productCollection->addUrlRewrite();
 $_productCollection->groupByAttribute('entity_id');
 $_productCollection->load();

foreach($_productCollection as $_product){  
       $_product->load($_product->getId());
   if($_product->getIsSalable()){
          $_name = ltrim(rtrim($_product->getName()));
          $_url =  $_product->getProductUrl();
       }
}   
我在这里做错了什么,如果我在每个循环中回显$\uURL,那么返回的url就缺少了。它没有类别名称和子类别名称。正确的url应为:

index.php/category/subcategory/itemname.html

但目前它只返回

  index.php/itemname.html
它返回除url之外的更正数据。我有双重检查在管理如果我分配了一个类别和子类别的项目,我确认我分配了它

Upon further research, I have found the code below that is very close to what i need.
$_categories = $_product->getCategoryIds();
$_category = Mage::getModel('catalog/category')->load($_categories[0]);
$_url = Mage::getUrl($_category->getUrlPath()).basename($_product->getProductUrl());
这里的问题是,$\uURL值变成了这样

index.php/category/subcategory.html/itemname.html

子类别名称中包含.html。我不需要在我的url中使用.html,这样在单击时该项目将被重定向到正确的页面


有没有解决这个问题的想法?

这是预期的行为,因为相同的产品可以存在于多个类别中。

为了那些与我有相同问题的人的利益。我通过这种方式解决了这个问题

 foreach($_productCollection as $_product){ 
        $_categories = $_product->getCategoryIds();
        $_category = Mage::getModel('catalog/category')->load($_categories[0]);          
        $caturl=explode('.html', $_category->getUrlPath());            
        $_url = Mage::getUrl($caturl[0]).basename($_product->getProductUrl()); 
 }  
我只是在类别url路径中分解.html,因为我希望我的类别url中包含.html


我不知道这是否是好的修复,但这对我来说是有效的

问题在于,产品可能出现在多个类别中

因此,在某种程度上,无论你以何种方式处理它,你都必须涉及某个类别

因此,最干净的方法是使用
addurlRewrite
方法并传递类别id。Magento将检查是否存在与类别id和产品id匹配的重写-如果找到重写,您将在
getProductUr
l调用中获得所需的漂亮URL。例如,使用类别id 10:

$collection = Mage::getResourceModel('catalog/product_collection')
                ->addUrlRewrite(10);
如果您知道每个产品只会出现在一个类别中,那么您可以通过使用以下方法来获取第一个类别id,从而使其更加灵活:

因此,使用此方法的完整示例如下:

$categoryId = array_shift($_product->getCategoryIds());
$collection = Mage::getResourceModel('catalog/product_collection')
                ->addUrlRewrite($categoryId);

谢谢你的解决方案。 但如果包含根类别和更多深度类别,则它将不起作用。 我为您的解决方案添加了一些代码:

   $cat_count=count($_categories);

   if($cat_count>0){
       if($_categories[0]==2) // here 2 is root category id
       $cat_count=$cat_count-1;
       else
       $cat_count=$cat_count-2;
   }


    $_categories = $_product->getCategoryIds();

    $_category = Mage::getModel('catalog/category')->load($_categories[$cat_count]); 

    $caturl=explode('.html', $_category->getUrlPath());      

    $_url = Mage::getUrl($caturl[0]).basename($_product->getProductUrl()); 
addUrlRewrite()//这是一个神奇的过程


嗨,我找到了一个非常接近我需要的代码。有关详细信息,请参见我上面的问题。我编辑了它。谢谢@tuxmytty:)的帮助
$_productCollection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addCategoryFilter( $_category )
    ->addAttributeToFilter('status', array('eq' => 1))
    ->addUrlRewrite()
    ->load();

    foreach ($_productCollection as $_product):

        $_product_url = $_product->getProductUrl();

    endforeach;