Jump to content

Access individual category names in Cubecart V6


Madan Kumar

Recommended Posts

Hi, I am quite new to cubecart and recently I have installed cubecart v6 on my laptop just to learn and play around it. I know that boxes can be  included inside a page such as  {include file='templates/box.navigation.php'} in the main page to display the navigation bar. I have noticed that 'box.navigation.php' file contains the GLOBAL variable $CATEGORIES , which is a complete HTML object with its own template and style.

However, I want to access individual category names such as

 {foreach from=$CATEGORIES item=category}
                  <ul>
                         <li>
                           {$category.name}
                       </li>

                  </ul>
  {/foreach}

However, the code above does not fetch the category names

Any support/idea how this can be done will be highly appreciated.

 

Thank  you

Madan

Link to comment
Share on other sites

Smarty template variables look like PHP all-cap variables, but they are not global to PHP. They are, however, (by default) global to Smarty. The template with the .php file extension is not an actual PHP script file.

CubeCart slogs its way through the category hierarchy then caches the resulting HTML from the populated Navigation templates.

The raw list of categories has never had a template that would use it. So, we will need to create a code snippet to get a list and assign it to a Smarty template variable.

In admin, Manage Hooks, Code Snippet tab, click Add Snippet.

There is now a form to fill out at the bottom of the snippet list.

Enabled: Checked

Unique ID: catlist_to_smarty@CC600+

Execution Order: 99

Description: Assigns a raw list of categories to a Smarty variable.

Trigger: class.gui.display_navigation.pre_cache

Version: 1.0

Author: CubeCart Forums

PHP Code:

<?php
/************************************************
 * Hook: class.gui.display_navigation.pre_cache
 * Output: associative array w/ 'cat_id' as key
 * Smarty: $RAW_CAT_LIST
 *
 * Template code:
 * {foreach $RAW_CAT_LIST as $CAT}
 *   {$CAT.cat_name}
 * {/foreach}
 *
 * See /setup/db/install/structure.sql for a
 * list of column names in CubeCart_category
 ***********************************************/

$raw_cat_list_recordset = $GLOBALS['db']->select('CubeCart_category',false);
if ($raw_cat_list_recordset !== false)
{
  foreach($raw_cat_list_recordset as $cat_record)
  {
    $raw_cat_list_result[$cat_record[$cat_id]] = $cat_record;
  }
  $GLOBALS['smarty']->assign('RAW_CAT_LIST', $raw_cat_list_result);
}

The hook we are using is in GUI->_displayNavigation(). The hook is inside an if-block that runs if this template has not been rendered and cached already.

If the template has been cached, then this hook is not executed. If you plan to list the categories in a place outside of a cached navigation HTML string, then we need to use a different hook.

If so, know that once Smarty fetches a template to populate and render, whatever variables then assigned to Smarty after that are not available to that template. That is, if we use the hook a few lines later, class.gui.display_navigation, then $RAW_CAT_LIST will be available to other templates not yet rendered. See GUI->displayCommon() for the sequence of the common templates processed.

Link to comment
Share on other sites

Hi,

I have created the code Snippet as you suggested and then made a template called <box.category.php>, in which I put 

{foreach $RAW_CAT_LIST as $CAT}
 <h4>{$CAT.cat_name}</h4>
{/foreach}
 

Then, I have included the "box.category.php" template inside main.php as

<div class="small-3 large-3 columns" id="category">
                {include file='templates/box.category.php'}
</div>

However, no category name is being dispalyed.

I am not sure if I am missing something here.

Many thanks in advance.

Madan

Link to comment
Share on other sites

Since the hook is located within that if-block I mentioned, the snippet will only get executed in certain circumstances.

Change the trigger for that snippet to class.gui.display_navigation and update the comments in the PHP code to show that it is using that trigger.

Save.

Test.

Link to comment
Share on other sites

Hi,

I did all the changes as you have suggested but still no luck. The screen dumps are below.

image.thumb.png.99b62118f0dde4fcea4ca8260d2d6b89.png

file: box.category.php 
{foreach $RAW_CAT_LIST as $CAT}

 <h4>{$CAT.cat_name}</h4>
 
{/foreach}

 

file: main.php

.................

................................

{* -------------------the div below to put navigation on the left of the content page------------------------*}
                <div class="small-3 large-3 columns" id="category">
                {include file='templates/box.category.php'}
                </div>

..................................................

 

Thank you again for your response.

 

 

 

Link to comment
Share on other sites

Hi, I am back again with a new query. How do I generate the URL for each category in the category list so when a category name is clicked, the content page will be populated by the relevant products!

Currently, it is working from the Navigation Bar but the problem is the Whole Navitaion Bar has been built as an html  object with a variable name {NAVIGATION_TREE} and I don't know how this HTML object is formed and how I can access individual variables in this object in CubeCart v6. The whole navigation bar is included inside the main page as box.navigation.php.

Any explanation will be much appreciated.

Regards

Madan 

 

Link to comment
Share on other sites

The way CubeCart is currently coded, the category display is built from a template. Thereafter, the cached rendered template is used.

We could have CubeCart populate and render an additional template. Let's explore that later.

For now, bring the code snippet back up for editing.

Find:

    $raw_cat_list_result[$cat_record['cat_id']] = $cat_record;

Add after:

    $raw_cat_list_result[$cat_record['cat_id']]['url'] = $GLOBALS['seo']->generatePath($cat_record['cat_id'],'cat',null,true,true);

You now have $CAT.url as a template variable within the {foreach} block.

(Note: I am believing there is a problem with saving an existing but edited snippet. It gets databased, but may not replace the actual snippet file in the /includes/extra/ folder. If you find that there is no url element in the $CAT iteration, try bringing the snippet up for editing and save it again.)

(Found the issue. The admin MUST clear the cache after saving the code snippet.)

Link to comment
Share on other sites

The {$NAVIGATION_TREE} is built from the element.navigation_tree.php template. This template is used in a recursive manner - that is, its pattern fits no matter how deep any one category and its children is found in the entire structure. However, for Foundation, the top category only drops down the immediate child layer. Other skins could go further.

Styling is accomplished in the skin's CSS files.

 

Link to comment
Share on other sites

Hi,

Another issue just came up. The currency symbol (£) in the Foundation skin  is not being displayed with the price, instead a question mark <?> inside a diamond is showing up. I have checked on the admin side and it is set up with the British Pound (£) sign as default currency. Even, I have checked the iso value in the currency table which is set correctly at 826.

Any idea why it might be happening?

Regards

Link to comment
Share on other sites

Something is not set for UTF-8 character encoding.

CubeCart is, and probably your web browser is also.

That leaves the database. Please use a database utility to determine the character encoding of the tables.

It will be tricky to convert, if any table is not set to UTF-8.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...