【问题标题】:Magento : How to get full product URL using categoryMagento:如何使用类别获取完整的产品 URL
【发布时间】:2012-06-06 10:53:22
【问题描述】:

我正在尝试找到一种解决方案,以确保 Magento 始终显示每个产品的完整 URL,包括类别路径。 我希望完整的网址也出现在搜索结果中。 为此,我将每种产品都归为一个类别。

这可以通过magento通过Url Rewrite custom来完成吗? 使用 .htaccess 将 /product.html 重定向到 category/subcategory/product.html 的 301 重定向是个好主意吗?

谢谢 穆迪

【问题讨论】:

    标签: magento


    【解决方案1】:

    我们在产品 url 中获取完整类别的方法是使用一个帮助程序,它会尝试为您获取包含该类别的产品 URL。你也可以重写 product 方法,但是当另一个模块重写一些产品的东西时,这会很痛苦,这就是我们使用 helper 方法的原因。

    这是我们的方法:

    public static function getFullUrl (Mage_Catalog_Model_Product $product , 
            Mage_Catalog_Model_Category $category = null , 
            $mustBeIncludedInNavigation = true ){
    
        // Try to find url matching provided category
        if( $category != null){
            // Category is no match then we'll try to find some other category later
            if( !in_array($product->getId() , $category->getProductCollection()->getAllIds() ) 
                    ||  !self::isCategoryAcceptable($category , $mustBeIncludedInNavigation )){
                $category = null;
            }
        }
        if ($category == null) {
            if( is_null($product->getCategoryIds() )){
                return $product->getProductUrl();
            }
            $catCount = 0;
            $productCategories = $product->getCategoryIds();
            // Go through all product's categories
            while( $catCount < count($productCategories) && $category == null ) {
                $tmpCategory = Mage::getModel('catalog/category')->load($productCategories[$catCount]);
                // See if category fits (active, url key, included in menu)
                if ( !self::isCategoryAcceptable($tmpCategory , $mustBeIncludedInNavigation ) ) {
                    $catCount++;
                }else{
                    $category = Mage::getModel('catalog/category')->load($productCategories[$catCount]);
                }
            }
        }
        $url = (!is_null( $product->getUrlPath($category))) ?  Mage::getBaseUrl() . $product->getUrlPath($category) : $product->getProductUrl();
        return $url;
    }
    
    /**
     * Checks if a category matches criteria: active && url_key not null && included in menu if it has to
     */
    protected static function isCategoryAcceptable(Mage_Catalog_Model_Category $category = null, $mustBeIncludedInNavigation = true){
        if( !$category->getIsActive() || is_null( $category->getUrlKey() )
            || ( $mustBeIncludedInNavigation && !$category->getIncludeInMenu()) ){
            return false;
        }
        return true;
    }
    

    如果指定了一个类别,它会尝试获取与该类别相关的 url。

    如果未指定类别或找不到提供的 url,该方法会尝试获取与产品附加到的第一个类别相关的产品 URL,并检查它是否可接受(活动,带有 url键和匹配的导航条件)。

    最后如果它回退到原来的$product-&gt;getProductUrl() Magento 方法。

    您必须通过此调用在模板(类别、购物车产品、最近查看的等等)中使用它:

    echo $this->helper('yourcompany/yourmodule')::getFullProductUrl($_product);
    

    编辑:

    我考虑了 Zachary 的评论,并通过添加一些检查和选项对其进行了一些调整。希望现在很酷。 例子:

    echo $this->helper('yourcompany/yourmodule')::getFullProductUrl($_product, $aCategory);
    

    将尝试在 $aCategory 中查找产品 url,然后回退到其他类别 url,最后是产品基础 url

    echo $this->helper('yourcompany/yourmodule')::getFullProductUrl($_product, someCategory, false);
    

    还将考虑未包含在导航中的类别。

    【讨论】:

    • 我使用了类似的东西,但是您犯的一个错误是您假设 getUrlPath() 将返回一个值。在某些情况下并不总是如此。不幸的是,您也不能依赖具有 url 键的类别。在 $url 声明中使用它之前,您必须先检查以确保该类别有一个。 (如果没有,迭代到下一个类别)
    • 确实如此。感谢您的输入,我会尽快修改答案!
    • 感谢大家的反馈...将尽快实施。再次感谢..
    • 我知道这是一个旧答案,但我目前在 magento 1.8 中实施此解决方案时遇到问题,我不知道为什么。我的本地 magento 副本版本是 1.7,它在那里工作正常,但在 1.8 中,url 返回类别页面中的中断,无论您在哪个类别中,它都只返回没有产品名称的基本类别。 (例如:mysite/base-category)并且在产品页面中它总是只返回基本类别而没有子类别。 (例如:mysite/base-category/item.html
    猜你喜欢
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多