【问题标题】:How to create Category / Subcategory Friendly URL in PrestaShop?如何在 PrestaShop 中创建类别/子类别友好 URL?
【发布时间】:2013-01-15 14:53:33
【问题描述】:

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

假设我们有这样的结构:

第 1 类

  • 备件
  • 配件

第 2 类

  • 筹码
  • 配件

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

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

谢谢!

【问题讨论】:

    标签: prestashop


    【解决方案1】:

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

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

    CLASSES/Dispatcher.php

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

    CLASSES/Link.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));
    

    【讨论】:

      【解决方案2】:

      以上代码对旧版本有效,但不适用于较新/最新版本。我为较新的版本(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 条件如下 (function 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,不建议使用深度嵌套。

      【讨论】:

        猜你喜欢
        • 2020-05-22
        • 2018-04-11
        • 2016-03-24
        • 1970-01-01
        • 2014-07-21
        • 1970-01-01
        • 2013-04-03
        • 1970-01-01
        • 2010-10-18
        相关资源
        最近更新 更多