【问题标题】:Magento getProductUrl() is not returning the right url (random?)Magento getProductUrl() 没有返回正确的网址(随机?)
【发布时间】:2012-07-30 14:53:29
【问题描述】:

我使用的是 Magento 1.5.0.1,并且产品页面上的交叉销售和追加销售块中使用的 getProductUrl() 函数会抛出不同的 URL 格式。

正确的网址如: /笔记本电脑包.html 或者错误的(它工作得很好,但当然不是重写 URL): /catalog/product/view/id/825/s/laptop-bag/category/16/

有时交叉销售和向上销售块都返回正确的 URL,有时都使用较长的版本,在某些情况下,一个使用正确的,另一个使用长的版本??

任何想法为什么会发生这种情况?

我已经运行了一次 magento 数据库修复、重新索引并刷新/刷新了所有缓存。

【问题讨论】:

    标签: magento magento-1.5


    【解决方案1】:

    尝试$product->getUrlPath() 而不是$product->getProductUrl()

    更新:根据@jordan314 的以下评论,Magento 向 EE 客户推荐:

    从 1.13 开始不再使用 url_path 属性,但仍可用于向后兼容,并且 Magento 不会为新产品分配值,因此不建议继续使用它。也许你可以尝试使用 $product->getProductUrl() 代替。

    【讨论】:

    • 谢谢!我可以看到现在发生了什么,基本上它发生在您查看某个类别的产品并且相关/追加销售产品不存在于同一类别中时。通过使用 urlpath,它从根目录获取它。像魅力一样工作!
    • 感谢 Kalpesh,遇到了同样的问题,经过大量挖掘发现!太棒了!
    • @AdamLesniak 很高兴为您提供帮助 :)
    • 我正在使用 EE。根据 Magento Support 的说法,“从 1.13 起不再使用 url_path 属性,但仍可用于向后兼容,并且 Magento 不会为新产品分配值,因此不建议继续使用它。也许你可以尝试改用 $product->getProductUrl()。”所以我会再次使用 $product->getProductUrl() 而不是 $product->getUrlPath()。
    【解决方案2】:

    由于找不到重写的url,生成了不正确的url。 可能是因为store_id不正确造成的。 例如:

    $id = 290;
    Mage::app()->setCurrentStore('default');
    echo "store_id: ".Mage::app()->getStore()->getId()."<br>";
    $url = Mage::helper('catalog/product')->getProductUrl($id);
    echo $url."<br>";
    
    //change store id
    Mage::app()->setCurrentStore('admin');
    echo "store_id: ".Mage::app()->getStore()->getId()."<br>";
    $url = Mage::helper('catalog/product')->getProductUrl($id);
    echo $url."<br>";
    

    结果:

    store_id: 1
    http://local.com/surestep-pro-diabetic-test-strips-50-strips-professional-care.html
    store_id: 0
    https://local.com/index.php/catalog/product/view/id/290/s/surestep-pro-diabetic-test-strips-50-strips-professional-care/
    

    正确的 url 重写可以在名为 core_url_rewrite 的表中找到(包括有关 store_id 的信息)

    如果在 core_url_rewrite 中找到匹配值,它将生成“正确的 url”,否则它将连接 product_id + url key + category_id

    $routePath = 'catalog/product/view';
    $routeParams['id']  = $product->getId();
    $routeParams['s']   = $product->getUrlKey();
    if ($categoryId) {
        $routeParams['category'] = $categoryId;
    }
    

    【讨论】:

    • 只需将Mage::app()-&gt;setCurrentStore('default'); 添加到我们方法的第一行(如建议的那样)就可以了,谢谢! :D
    【解决方案3】:

    当你得到你的收藏时尝试添加这个

    $collection->addUrlRewrite();
    

    它帮助了我。

    【讨论】:

    • 这行得通。但需要添加 $collection->load();并且在 $collection->addUrlRewrite(); 之后
    【解决方案4】:
    $id = 10;
    Mage::app()->setCurrentStore('admin');
    $url = Mage::helper('catalog/product')->getProductUrl($id);
    

    【讨论】:

    猜你喜欢
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    相关资源
    最近更新 更多