且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Magento:如何使用类别获取完整的产品URL

更新时间:2023-11-30 13:46:10

我们要在产品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.