【问题标题】:Opencart: sort products by their subcategory on the category pageOpencart:按类别页面上的子类别对产品进行排序
【发布时间】:2013-05-29 06:08:14
【问题描述】:

我正在用 opencart 1.5.5.1 建立一个商店(我是新手),我遇到了一个小问题:

我的任何主要类别页面都如下所示:
优化搜索:
    -Subcategory1
-子类别2
-子类别3

产品1  产品2  产品3  ...

子类别 1,2 和 3 是指向子类别页面的链接。

我希望我的主要类别页面是这样的:

子类别 1
产品 1  产品 2  产品 3  .......

子类别 2
产品 1  产品 2  产品 3  .......

子类别 3
产品1  产品2  产品3  .......

其中子类别 1,2 和 3 不再是指向子类别页面的链接,而是简单的文本。

我找到了这个扩展程序:http://www.opencart.com/index.php?route=extension/extension/info&token=b4381909f29ca2d2511830b09f09fa84&extension_id=11190,但也许有人可以帮助我而无需支付 15 美元

谁能帮帮我?

【问题讨论】:

  • 您安装了自定义/新主题还是使用默认的 opencart 主题?
  • @BIT CHEETAH - 我使用默认主题。
  • @burhan - 我没试过那么多。 opencart 论坛上有一些帖子问同样的事情,但没有任何具体的答案。我尝试从默认主题修改一些 php 和 tpl 文件,但没有运气。

标签: php opencart


【解决方案1】:

→ 好的,所以我将逐步完成此步骤,在步骤之间提出问题,如果您明白了,请投票支持此步骤:

第 1 步:

打开以下文件:

catelog/controller/product/category.php

先保存此文件的备份,然后继续!

你应该在第 271 行附近看到这个:

     $this->data['categories'][] = array(

         'name'  => $result['name'] . ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : ''),

         'href'  => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url)

    );

把上面的代码改成:

     $this->data['categories'][] = array(

          'category_id'  => $result['category_id'], /*!!!*/

          'name'  => $result['name'] . ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : ''),

          'href'  => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url)

     );

第 2 步:

打开与第 1 步相同的文件:

catelog/controller/product/category.php

先保存此文件的备份,然后继续!

在第 271 行附近,您应该会看到:

    $this->data['categories'][] = array(

        'category_id'  => $result['category_id'], /*!!!*/

        'name'  => $result['name'] . ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : ''),

        'href'  => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url)

    );

} 

/* New code will be pasted here! */

$this->data['products'] = array();

显然,这是我们在第 1 步中处理的代码,但您现在要做的是将以下代码复制并粘贴到最后一个 '}' 之后但在 '$this->data['products '] = 数组();'

$this->data['products_all'] = array();

for( $x = 0; $x < count( $this->data['categories'] ); $x++ ) {

    $cat = $this->data['categories'][ $x ][ 'category_id' ];
    $this->data['products_all'][ $cat ] = array();

    $data = array(
        'filter_category_id' => $cat, 
        'sort'               => $sort,
        'order'              => $order,
        'start'              => ($page - 1) * $limit,
        'limit'              => $limit
    );

    $product_total = $this->model_catalog_product->getTotalProducts($data); 
    $results = $this->model_catalog_product->getProducts($data);

    foreach ($results as $result) {

        if ($result['image']) {
            $image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
        }
        else { $image = false; }

        if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
            $price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
        } 
        else { $price = false; }

        if ((float)$result['special']) {
            $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
        } 
        else { $special = false; }  

        if ($this->config->get('config_tax')) {
            $tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price']);
        }
        else { $tax = false; }              

        if ($this->config->get('config_review_status')) {
            $rating = (int)$result['rating'];
        } 
        else { $rating = false; }

        $this->data['products_all'][ $cat ][] = array(
            'product_id'  => $result['product_id'],
            'thumb'       => $image,
            'name'        => $result['name'],
            'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..',
            'price'       => $price,
            'special'     => $special,
            'tax'         => $tax,
            'rating'      => $result['rating'],
            'reviews'     => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
            'href'        => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'])

        );
    }
}

第 3 步:

打开以下文件:

catalog/view/theme/default/template/product/category.tpl

先保存此文件的备份,然后继续!

大约在第 19 行的某个地方,您应该会看到:

  <?php if ($categories) { ?>
    <h2><?php echo $text_refine; ?></h2>
    <div class="category-list">
    <?php if (count($categories) <= 5) { ?>
    <ul>
      <?php foreach ($categories as $category) { ?>
      <li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a></li>
    <?php } ?>
    </ul>
  <?php } else { ?>

把上面的代码改成:

  <?php if ($categories) { ?>

    <div class="category-list">

    <?php 

        if ( count( $categories ) <= 5 ) {

            foreach ( $categories as $k=>$category ) { 

                if( $k > 0 ) echo '<br /><br />';

                echo '<div class="product-filter" style="background:#222;padding:5px;padding-bottom:2px">';
                echo '<a href="' . $category['href'] . '" style="font-size:21px;text-decoration:none;"><h2 style="color:#eee;font-style:italic"><span style="color:#777;position:relative;bottom:2px">&rarr;&nbsp;</span><span style="border-bottom:solid 1px #777;">' . $category['name'] . '</span>:</h2></a>';
                echo '</div>';
                echo '<hr style="border:none;border-top:solid 1px #eee"/>';
                echo '<div class="product-list">';

                foreach ( $products_all[ $category['category_id'] ] as $product )
                {
                    echo '<div>';
                    if ( $product['thumb'] ) {
                        echo '<div class="image"><a href="' . $product['href'] . '"><img src="' . $product['thumb'] . '" title="' . $product['name'] . '" alt="' . $product['name'] . '" /></a></div>';
                    }
                    echo '<div class="name"><a href="' . $product['href'] . '">' . $product['name'] . '</a></div>';
                    echo '<div class="description">' . $product['description'] . '</div>';
                    if ( $product['price'] ) {
                        echo '<div class="price">';
                        if ( !$product['special'] ) { echo $product['price']; } 
                        else { echo '<span class="price-old">' . $product['price'] . '</span> <span class="price-new">' . $product['special'] . '</span>'; }
                        if ( $product['tax'] ) { echo '<br /><span class="price-tax">' . $text_tax . ' ' . $product['tax'] . '</span>'; }
                        echo '</div>';
                    }
                    if ( $product['rating'] ) {
                        echo '<div class="rating"><img src="catalog/view/theme/default/image/stars-' . $product['rating'] . '.png" alt="' . $product['reviews'] . '" /></div>';
                    }
                    echo '<div class="cart">';
                    echo '<input type="button" value="' . $button_cart . '" onclick="addToCart(\'' . $product['product_id'] . '\');" class="button" />';
                    echo '</div>';
                    echo '<div class="wishlist"><a onclick="addToWishList(\'' . $product['product_id'] . '\');">' . $button_wishlist . '</a></div>';
                    echo '<div class="compare"><a onclick="addToCompare(\'' . $product['product_id'] . '\');">' . $button_compare . '</a></div>';
                    echo '</div>';
              }
              echo '</div>';

            }

      ?>

    <?php } else { ?>

【讨论】:

  • 好的,我更改了 catalog/controller/product/category.php 中的代码(步骤 1 完成)。第二步?
  • 您不认为回答三遍并要求 OP 对您的每个答案进行投票(如果他得到它)有点苛刻吗? 您应该只回答一次并修改您的回答。我将标记您的所有三个答案。
  • @shadyyx,不,我没有,仅仅因为我回答了这个问题并不意味着答案就在我的电脑上。我知道该怎么做,但我必须像我那样编写代码。如果我没有把它分开,这个问题就会没有答案,因为我不会冒险在不知道提问者是否可以跟进并完成工作的情况下完成整个工作。投票请求是我不得不帮助拉杜的一个微不足道的激励,这也为他节省了 15 美元:我非常怀疑他会认为我的方法“有点苛刻”;这些都是版主没有真正的控制的东西。
【解决方案2】:

opencart 2 怎么样?如果我真的喜欢你,我会得到

注意:未定义的偏移量:60 in ..\category.tpl on line 59 警告:在第 59 行的 ..\category.tpl 中为 foreach() 提供的参数无效

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    相关资源
    最近更新 更多