How to show just specific categories in box_categories.inc?

Merchant
De Slovenia
Miembro desde oct. 2023

Hi,

I want to show just specific categories / subcategories in box_categories.inc on homepage. What I need to modify in code bellow?

  <?php foreach ($categories as $category) echo functions::draw_listing_category($category); ?>

Thanks in advance!

Moderator
De Lithuania
Miembro desde mar. 2016

Not sure what you are trying to achieve here but you can try:

<?php 

  $categories = array(1, 3, 5);

  foreach ($categories as $category) echo functions::draw_listing_category($category); 

?>

Use categories ids instead 1,3,5

Merchant
De Slovenia
Miembro desde oct. 2023

Thanks!

I want to show just specific categories on homepage instead all master categories.

Seems this is not working...

Warning: Trying to access array offset on value of type int in ~/includes/functions/func_draw.inc.php (Line 67)
Warning: Trying to access array offset on value of type int in ~/includes/functions/func_draw.inc.php (Line 68)
Warning: Trying to access array offset on value of type int in ~/includes/functions/func_draw.inc.php (Line 69)
Warning: Trying to access array offset on value of type int in ~/includes/functions/func_draw.inc.php (Line 71)
Warning: Trying to access array offset on value of type int in ~/includes/functions/func_draw.inc.php (Line 72)
Warning: Trying to access array offset on value of type int in ~/includes/functions/func_draw.inc.php (Line 73)
Warning: Trying to access array offset on value of type int in ~/includes/functions/func_draw.inc.php (Line 80)

tim
Founder
De Sweden
Miembro desde may. 2013
tim

Swap out functions::catalog_categories_query() with your own custom database::query().

<?php
  $box_categories_cache_token = cache::token('box_categories', ['language'], 'file');
  if (cache::capture($box_categories_cache_token)) {

    $categories_query = database::query(
      "select c.id, c.parent_id, c.image, ci.name, ci.short_description, c.priority, c.date_updated from ". DB_TABLE_PREFIX ."categories c
      left join ". DB_TABLE_PREFIX ."categories_info ci on (ci.category_id = c.id and ci.language_code = '". database::input(language::$selected['code']) ."')
      where c.status
      and c.id in (1, 3, 5)
      order by c.priority asc, ci.name asc;"
    );

    if (database::num_rows($categories_query)) {
      $box_categories = new ent_view();

      $box_categories->snippets = [
        'categories' => [],
      ];

      while ($category = database::fetch($categories_query)) {
        $box_categories->snippets['categories'][] = $category;
      }

      echo $box_categories->stitch('views/box_categories');
    }

    cache::end_capture($box_categories_cache_token);
  }
Merchant
De Slovenia
Miembro desde oct. 2023

Thanks Tim!

Do I need to put this into index.inc.php?

LiteCart Fan
De Netherlands
Miembro desde dic. 2022

No. In file includes/boxes/box_categories.inc.php: replace this line

$categories_query = functions::catalog_categories_query();

with this code:

$categories_query = database::query(

      "select c.id, c.parent_id, c.image, ci.name, ci.short_description, c.priority, c.date_updated from ". DB_TABLE_PREFIX ."categories c

      left join ". DB_TABLE_PREFIX ."categories_info ci on (ci.category_id = c.id and ci.language_code = '". database::input(language::$selected['code']) ."')

      where c.status

      and c.id in (1, 3, 5)

      order by c.priority asc, ci.name asc;"

    );

The line

and c.id in (1, 3, 5)

means: show only categories with the id 1, 3 and 5

Merchant
De Slovenia
Miembro desde oct. 2023

Thanks jackmaessen!

Like this:

<section id="box-categories" class="box">
  <div class="card-header">
    <h1 class="card-title"><?php echo language::translate('title_shop_by_category', 'Shop By Category'); ?></h1>
  </div>

  <div class="card-body">
    <div class="listing categories">
     <?php
  $box_categories_cache_token = cache::token('box_categories', ['language'], 'file');
  if (cache::capture($box_categories_cache_token)) {

    $categories_query = database::query(
      "select c.id, c.parent_id, c.image, ci.name, ci.short_description, c.priority, c.date_updated from ". DB_TABLE_PREFIX ."categories c
      left join ". DB_TABLE_PREFIX ."categories_info ci on (ci.category_id = c.id and ci.language_code = '". database::input(language::$selected['code']) ."')
      where c.status
      and c.id in (44, 443, 316)
      order by c.priority asc, ci.name asc;"
    );

    if (database::num_rows($categories_query)) {
      $box_categories = new ent_view();

      $box_categories->snippets = [
        'categories' => [44, 443, 316],
      ];

      while ($category = database::fetch($categories_query)) {
        $box_categories->snippets['categories'][] = $category;
      }

      echo $box_categories->stitch('views/box_categories');
    }

    cache::end_capture($box_categories_cache_token);
  } 
    </div>
  </div>
</section>

I replace and get error...

Merchant
De Slovenia
Miembro desde oct. 2023

Oh I see now. Thanks, it works!

This website uses no cookies and no third party tracking technology. We think we can do better than others and really think about your privacy.