0

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

假设我们有这样的结构:

第一类

  • 备件
  • 配件

第 2 类

  • 筹码
  • 配件

我想显示这样的链接:/category-1/accessories并显示类别 1->accessories 中的产品。我怎样才能做到这一点?

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

谢谢!

4

2 回答 2

2

这个问题在 PrestaShop 论坛上得到了回答。你可以在这里找到它http://www.prestashop.com/forums/topic/220017-category-subcategory-url/

解决方案 - 将此更改添加到休闲课程

类/Dispatcher.php

'rule' => '{categories:/}{id}-{rewrite}/',
'categories' => array('regexp' => '[/_a-zA-Z0-9-\pL]*'),

类/链接.php

$cats = array();    
foreach ($category->getParentsCategories() as $cat)    
if (!in_array($cat['id_category'], array(1, 2, $category->id)))//remove root, home and current category from the URL    
$cats[] = $cat['link_rewrite'];    
$params['categories'] = implode('/', array_reverse($cats));
于 2013-02-10T18:33:38.523 回答
1

上述代码对旧版本有效,但不适用于较新/最新版本。我为较新的版本(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 条件如下在代码中的第 218 行 (function getProductLink)

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,不建议使用深度嵌套。

于 2021-05-22T19:43:50.847 回答