【问题标题】:OpenCart Split category on more pages更多页面上的 OpenCart 拆分类别
【发布时间】:2013-11-15 15:50:04
【问题描述】:

我正在努力实现以下目标:

带有 CATEGORY GROUP1、CATEGORY GROUP2、CATEGORY GROUP3 的顶部菜单

在选择类别组1时,该组中的所有类别将在左栏中列出。

所以左栏看起来像这样:

CATEGORY GROUP1 category11

CATEGORY GROUP1 category12

CATEGORY GROUP1 category13

在选择产品时,只有这个 CATEGORY GROUP1 应该在左栏中可见,

当在顶部菜单中选择 CATEGORY GROUP2 时,我希望左栏只显示:

CATEGORY GROUP2 Category21

CATEGORY GROUP2 Category22

CATEGORY GROUP2 Category23

当从该列表中选择产品时,我只希望 CATEGORY GROUP2 在左侧列中可见

与类别 3 相同。

只是不知道如何做到这一点,因为如果您将类别模块添加到布局“产品”中,每次您在产品中时都会看到它。

我尝试过拆分类别并为每个拆分添加不同的布局, 示例路由 page1/page1、page2/page2,这适用于仅在不同页面上显示类别,但是一旦您选择产品,它就会显示分配给产品页面的所有模块。

如何做到这一点?

OpenCart 1.5.6

【问题讨论】:

    标签: php opencart


    【解决方案1】:

    我明白你想要达到的目标。我知道创建一个新问题会更好。这可以在catalog/controller/module/category.php 控制器中完成,其中为根检索类别并加载子项。把代码改成这样:

    class ControllerModuleCategory extends Controller {
        protected function index($setting) {
            $this->language->load('module/category');
    
            $this->data['heading_title'] = $this->language->get('heading_title');
    
            if (isset($this->request->get['path'])) {
                $parts = explode('_', (string)$this->request->get['path']);
            } else {
                $parts = array();
            }
    
            if (isset($parts[0])) {
                $this->data['category_id'] = $parts[0];
            } else {
                $this->data['category_id'] = 0;
            }
    
            $categories = $this->model_catalog_category->getCategories($this->data['category_id']);
    
            if (isset($parts[1])) {
                $this->data['child_id'] = $parts[1];
            } else {
                $this->data['child_id'] = 0;
            }
    
            $this->load->model('catalog/category');
    
            $this->load->model('catalog/product');
    
            $this->data['categories'] = array();
    
            foreach ($categories as $category) {
                $total = $this->model_catalog_product->getTotalProducts(array('filter_category_id' => $category['category_id']));
    
                $children_data = array();
    
                if (!$this->data['category_id']) {
                    $children = $this->model_catalog_category->getCategories($category['category_id']);
    
                    foreach ($children as $child) {
                        $data = array(
                            'filter_category_id'  => $child['category_id'],
                            'filter_sub_category' => true
                        );
    
                        $product_total = $this->model_catalog_product->getTotalProducts($data);
    
                        $total += $product_total;
    
                        $children_data[] = array(
                            'category_id' => $child['category_id'],
                            'name'        => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : ''),
                            'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
                        );      
                    }
                }
    
                $this->data['categories'][] = array(
                    'category_id' => $category['category_id'],
                    'name'        => $category['name'] . ($this->config->get('config_product_count') ? ' (' . $total . ')' : ''),
                    'children'    => $children_data,
                    'href'        => $this->url->link('product/category', 'path=' . $category['category_id'])
                );  
            }
    
            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/category.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/module/category.tpl';
            } else {
                $this->template = 'default/template/module/category.tpl';
            }
    
            $this->render();
        }
    }
    

    如果没有提供类别 ID,这应该和之前一样正常工作...

    【讨论】:

    • 谢谢,但这只是去掉了左侧列中的类别,而将这些类别留在了顶部菜单中?对不起,如果我不够清楚。如果我选择Category Group1,那么左列显示11,12,13类,选择产品时,11,12,13类仍然存在(image1 + 2),然后选择Category Group2时,类别21 ,22,23显示在左栏中,而是在选择产品时,那么这些类别就在那里。 (image3+4) 让产品页面继承你之前的内容,希望它有意义吗?
    • 好吧,我用 CSS 解决了这个问题,方法是根据路由向 body 添加不同的类,并简单地显示/隐藏我不想要的东西。不是最终的解决方案,但有效。我应该在这里发布答案还是太肮脏的解决方法?
    • 恕我直言,这是一种解决方法,但如果可行,OFC 将其发布在这里并接受它,以便将来有人也可以使用它;-)
    【解决方案2】:

    我得到了它的解决方法。

    我结合了这个答案:
    Adding CSS stylesheet to pages based on route in OpenCart

    有了这个: OpenCart module on product page based on route

    然后我添加了一个新的布局,这样我就可以在不同的页面上拆分类别, 并为基于产品路径的模块添加了 display:none。

    有效但不是最佳解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-08
      相关资源
      最近更新 更多