CGProducts and Menus - Producing menu if category has items

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
JimboDavies
Forum Members
Forum Members
Posts: 130
Joined: Fri Feb 25, 2011 3:58 pm

CGProducts and Menus - Producing menu if category has items

Post 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
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1973
Joined: Mon Jan 29, 2007 4:47 pm

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

Post 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...
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
JimboDavies
Forum Members
Forum Members
Posts: 130
Joined: Fri Feb 25, 2011 3:58 pm

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

Post 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
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1973
Joined: Mon Jan 29, 2007 4:47 pm

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

Post 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.
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
JimboDavies
Forum Members
Forum Members
Posts: 130
Joined: Fri Feb 25, 2011 3:58 pm

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

Post by JimboDavies »

Great, cheers Jo!
Post Reply

Return to “Modules/Add-Ons”