Page 1 of 1

CGProducts and Menus - Producing menu if category has items

Posted: Thu May 28, 2015 8:37 am
by JimboDavies
I'm doing some tweaking work on an old website I built (changing it for a fully responsive template and updating things is a muuuuuch longer project!) and one of the things I have noticed is that the nature of my menu structure means I am producing a heading for each category, and it then calls the Products module to produce the links below.
As the people that work at the company change and replace pages, we've ended up with a few empty categories which I don't notice.
So, I thought it would be better to first check if the category has items, then produce the heading and link list.
The code I've started with so far, is:

Code: Select all

{products_getcategory category='2015-YachtRacing-WholeBoatCaribbeanRacing' assign='tmp'}
{if tmp !=''}
<h2>Caribbean Racing (Whole Boat)</h2>
{Products category='2015-YachtRacing-WholeBoatCaribbeanRacing' sortby='weight'}
{/if}
However, that doesn't seem to work - this is an empty category I'm testing it on.
What do I need to do to make it effectively check if that category has items, and only produce the following code if it does?
Cheers!
Jim

Re: CGProducts and Menus - Producing menu if category has it

Posted: Thu May 28, 2015 9:51 am
by Jo Morg
If your default summary template outputs HTML (or anything for that matter) $tmp will never be empty. Additionally the correct way to check a variable would be {if $tmp !=''} as you do need the $ to reference variables. The best way imo would be to check if count $items is > 0, like {if count($items) > 0} (IIRC $items is the correct variable but you should check the summary template).
The other inefficient thing is that you are calling the same tag twice and you don't actually need to. {$temp} already has the rendered template... so I'd go with something along the lines of:

Code: Select all

{Products category='2015-YachtRacing-WholeBoatCaribbeanRacing' sortby='weight' assign='tmp'}
{if count($items) > 0}
<h2>Caribbean Racing (Whole Boat)</h2>
{$temp}
{/if}
Not tested though...

Re: CGProducts and Menus - Producing menu if category has it

Posted: Thu May 28, 2015 10:02 am
by JimboDavies
Works perfectly, very many thanks Jo!

Now, say I wanted to use this for all of my categories. Do I need to give each one a discrete name, or can I reuse the tmp variable as it goes through my whole menu?

Jim

Re: CGProducts and Menus - Producing menu if category has it

Posted: Thu May 28, 2015 10:06 am
by Jo Morg
{$temp} will have a different value every time it is reassigned so you should be good sticking with it for the whole menu.

Re: CGProducts and Menus - Producing menu if category has it

Posted: Thu May 28, 2015 10:28 am
by JimboDavies
Great, cheers Jo!