如何在PrestaShop中创建类别/子类别友好URL?

时间:2013-01-31 13:28:41

标签: prestashop

我有一个关于格式化类别和子类别的友好URL以及获取匹配产品的问题。我正在使用PrestaShop 1.5.2.0

假设我们有这样的结构:

第1类

  • 备件
  • 附件

第2类

  • 芯片
  • 附件

我想显示如下链接: / category-1 / accessories ,并显示1-类和附件类别的产品。我怎样才能做到这一点?

当前的行为是当我点击附件时,在类别1中,链接是 / accessories ,并且显示的产品属于/ category-1 / accessories和/ category-2 /附件

谢谢!

1 个答案:

答案 0 :(得分:0)

以上代码对旧版本有效,但不适用于较新/最新版本。我为较新的版本 (1.7.7.4.) 更新了相同的解决方案,它可能会用于其他人。

更改类/Dispatcher.php

在上面文件复制粘贴中的第 55 行

public $default_routes = [
        'category_rule' => [
            'controller' => 'category',
             **/**  added 1 line below*/
            'rule' => '{category:/}{id}-{rewrite}/',
             /** commented   1line below*/
            /**'rule' => '{id}-{rewrite}',*/
            'keywords' => [
                'id' => ['regexp' => '[0-9]+', 'param' => 'id_category'],
                /*** added  1 line below*/
                'category' => ['regexp' => '[_a-zA-Z0-9-\pL]*'],
                'rewrite' => ['regexp' => self::REWRITE_PATTERN],
                'meta_keywords' => ['regexp' => '[_a-zA-Z0-9-\pL]*'],
                'meta_title' => ['regexp' => '[_a-zA-Z0-9-\pL]*'],**
            ],
        ],

在文件 classes/link.php 中找到函数 getCategoryLink 并将其替换为下面的函数代码

 /**
     * Create a link to a category.
     *
     * @param mixed $category Category object (can be an ID category, but deprecated)
     * @param string $alias
     * @param int $idLang
     * @param string $selectedFilters Url parameter to autocheck filters of the module blocklayered
     *
     * @return string
     */
    public function getCategoryLink(
        $category,
        $alias = null,
        $idLang = null,
        $selectedFilters = null,
        $idShop = null,
        $relativeProtocol = false
    ) {
        $dispatcher = Dispatcher::getInstance();

        if (!$idLang) {
            $idLang = Context::getContext()->language->id;
        }

        $url = $this->getBaseLink($idShop, null, $relativeProtocol) . $this->getLangLink($idLang, null, $idShop);

        // Set available keywords
        $params = [];

        if (!is_object($category)) {
            $params['id'] = $category;
        } else {
            $params['id'] = $category->id;
        }

        // Selected filters is used by the module ps_facetedsearch
        $selectedFilters = null === $selectedFilters ? '' : $selectedFilters;

        if (empty($selectedFilters)) {
            $rule = 'category_rule';
        } else {
            $rule = 'layered_rule';
            $params['selected_filters'] = $selectedFilters;
        }

        if (!$alias) {
            $category = $this->getCategoryObject($category, $idLang);
        }
        $params['rewrite'] = (!$alias) ? $category->link_rewrite : $alias;
        if ($dispatcher->hasKeyword($rule, $idLang, 'meta_keywords', $idShop)) {
            $category = $this->getCategoryObject($category, $idLang);
            $params['meta_keywords'] = Tools::str2url($category->getFieldByLang('meta_keywords'));
        }
        if ($dispatcher->hasKeyword($rule, $idLang, 'meta_title', $idShop)) {
            $category = $this->getCategoryObject($category, $idLang);
            $params['meta_title'] = Tools::str2url($category->getFieldByLang('meta_title'));
        }
        
        if ($category !='var'){
        $category = $this->getCategoryObject($category, $idLang);
        $pcategory= new Category($category->id_parent, $idLang);
        if($category->id_parent!=1 && $category->id_parent!=2){
            $params['category'] = $pcategory->link_rewrite;
            //append the categoryID with its name
            $params['category'] = $category->id_parent . '-'. $params['category'];
        }
    }
        
        return $url . Dispatcher::getInstance()->createUrl($rule, $idLang, $params, $this->allow, '', $idShop);
    }

在同一个文件 classes/link.php 中更新 if 条件如下代码中的(函数 getProductLink)代码中的第 218 行

if ($dispatcher->hasKeyword('product_rule', $idLang, 'categories', $idShop)) {
            $product = $this->getProductObject($product, $idLang, $idShop);
            $params['category'] = (!$category) ? $product->category : $category;
            $cats = [];
            
            foreach ($product->getParentCategories($idLang) as $cat) {
                
                if (!in_array($cat['id_category'], Link::$category_disable_rewrite)) {
                    //remove root and home category from the URL
                    //commented the line below 
                    //$cats[] = $cat['link_rewrite'];
                    //replaced the above line with the line below to append the category ID in the products link


                    $cats[] = $cat['id_category'].'-'.$cat['link_rewrite'];
                }
            }
            $params['categories'] = implode('/', $cats);
        }

我碰巧使用的是 prestashop 版本 1.7.7.4。您可以在我的网站上看到此解决方案 https://jinbaba.pk

同样在代码文件中进行上述更改后,不要忘记更新shopparameters-->SEO&URL设置,将类别和产品路线更改如下(如果不是这样的话)

"Route to category" = {category:/}{id}-{rewrite}

"Route to product" = {categories:/}{id}{-:id_product_attribute}-{rewrite}{-:ean13}.html

只是对 SEO 的一个建议:您不需要从 URL 中删除类别 ID 和产品 ID。它们对 SEO 的影响很小或没有影响。

about 解决方案也适用于 2 级嵌套,例如

yourdomain.com/category-1/category-2/1-product.html

不要在您的目录中创建更多的类别嵌套。如果您打算创建更深的嵌套站点,则需要更新此解决方案。但是,对于 SEO 而言,不建议使用深度嵌套。

相关问题