Magento:如何使用类别获取完整的产品URL [英] Magento : How to get full product URL using category

查看:52
本文介绍了Magento:如何使用类别获取完整的产品URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to find a solution for making sure that Magento always shows the complete URL of every product including the category path. I want the complete url to also be there in search results also. For this i have put every product in only one category.

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

Can this be done by Url Rewrite custom through magento? Is 301 redirect using .htaccess, for /product.html to category/subcategory/product.html a good idea?

谢谢 喜怒无常

推荐答案

我们要在产品url中获取完整类别的方法是使用帮助程序,该帮助程序将尝试为您提供具有该类别的产品URL.您也可以重写product方法,但是每当另一个模块重写某些产品时,它都会很痛苦,这就是我们使用辅助方法的原因.

What we do to get full category in products url is using a helper that'll try to get you a product URL with the category. You could also rewrite the product method but its kind of a pain whenever another module rewrites some product stuff, this is why we use the helper approach.

这是我们的方法:

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;
}

如果指定了类别,它将尝试获取相对于该类别的网址.

If a category is specified it tries to get a url relative to this one.

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

If no category is specified or couldn't find a url with provided one, the method tries to get the product URL relative to the first category that the product is attached to, and checks if it's acceptable (active, with a url key and matching navigation criteria).

最后,如果它退回到原始的$product->getProductUrl() Magento方法.

Finally if it falls back to original $product->getProductUrl() Magento method.

您必须在此调用中将其用于模板(类别,购物车产品,最近浏览过的商品....).

You'll have to use it in templates (categories, cart products, recently viewed etc....) with this call :

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

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

I took Zachary's remarks into account and tweaked it up a little by adding some checks and options. Hope it's cool now. Examples :

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

将尝试在$ aCategory中找到产品网址,然后回退到其他类别网址,最后返回产品基本网址

will try to find a product url in $aCategory then fall back to other category urls and finally product base url

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

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

will also consider categories that are not included in navigation.

这篇关于Magento:如何使用类别获取完整的产品URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆