Yet another blog (hopefully a useful one though)
Magento: jQuery Accordian Menu
Ok here’s how to make a dynamic jQuery sliding accordian menu in Magento, as seen on http://www.waterfrontbathrooms.com/.
Waterfront bathrooms also has a dynamic tree menu system to deal with there being more than 2 levels of categories, but I’ll cover this in a future post.
- Upload jQuery
Upload the latest jQuery version to js/jquery/jqueryfile.js. You can get the latest jQuery file from: http://docs.jquery.com/Downloading_jQuery - Upload ddAccordian
Upload ddAccordian menu script to js/ddaccordian.js. You can get ddaccordian.js from http://www.dynamicdrive.com/dynamicindex17/ddaccordion.js - Call the javascript files from the page head
Open app/design/frontend/yourpackage/yourtheme/template/page/html/head.phtml and put the following lines in anywhere after the default lines of code.
<script type="text/javascript" src="/js/jquery/jqueryfile.js"></script><br /> <script type="text/javascript" src="/js/ddaccordion.js"></script>
- Create the navigation template file [download here]
Create a new phtml file in app/design/frontend/yourpackage/yourtheme/template/catalog/navigation/ You can call this whatever you like, but for the purpose of this post lets call it AccordianMenu.phtml.This would be app/design/frontend/yourpackage/yourtheme/template/catalog/navigation/AccordianMenu.phtml.
In this file you want to put this code. This is what gets all the categories from the database and displays them in a way that ddAccordian can understand.
<?php /* Get the categories that are active for the store */ $_main_categories=$this->getStoreCategories(); /* Get the current category the user is in */ $_default_category=$this->getCurrentCategory(); ?> <div id="leftmenu"> <?php if ($_main_categories): /* This bit cycles through the categories - setting the next one to current */ foreach ($_main_categories as $_main_category): // if(($_main_category->getIsActive()) && ($_main_category->getId() != 37)): // Skip Category with ID of 37 if($_main_category->getIsActive()): $cur_category=Mage::getModel('catalog/category')->load($_main_category->getId()); $layer = Mage::getSingleton('catalog/layer'); $layer->setCurrentCategory($cur_category); ?> <?php $_categories=$this->getCurrentChildCategories()?> <?php if($_categories->count()):?> <div><?php echo $this->getCurrentCategory()->getName();?></div> <div> <ul> <? foreach ($_categories as $_category):?> <? if($_category->getIsActive()): $cur_subcategory=Mage::getModel('catalog/category')->load($_category->getId()); $layer = Mage::getSingleton('catalog/layer'); $layer->setCurrentCategory($cur_subcategory); ?> <li>» <a href="<?php echo $this->getCategoryUrl($_category)?>"><?php echo $_category->getName()?></a></li> <? endif;?> <?endforeach?> </ul> </div> <?else:?> <div><a href="<?php echo $this->getCategoryUrl($_main_category)?>"><?php echo $_main_category->getName();?></a></div> <?endif;?> <?php endif; endforeach; ?> <?php endif; ?> <?php $layer->setCurrentCategory($_default_category); ?> <div><a href="#">Information Page</a></div> <div><a href="#">Information Page</a></div> <div><a href="#">Information Page</a></div> </div> - Refence the AccordianMenu.phtml in catalog.xml
In your main layout file (app/design/frontend/yourpackage/yourtheme/layout/catalog.xml), you need to tell Magento what the Accordian file is, and where to put it. This tutorial isn’t really about covering the workings of your catalog.xml file, you’ll find plenty of tutorials about this if you search around google. The code I use to link the accordian menu to the left column is:
<reference name="left"> <block type="catalog/navigation" name="catalog.accordianmenu" template="catalog/navigation/AccordianMenu.phtml"/> </reference>
- Configure ddAccordian settings
Open your head file again (app/design/frontend/yourpackage/yourtheme/template/page/html/head.phtml) and add the following code underneath where you called the scripts.<script type="text/javascript"><br /> //Initialize first demo:<br /> ddaccordion.init({<br /> headerclass: "navhead", //Shared CSS class name of headers group<br /> contentclass: "navcontent", //Shared CSS class name of contents group<br /> revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"<br /> mouseoverdelay: 400, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover<br /> collapseprev: true, //Collapse previous content (so only one open at any time)? true/false<br /> defaultexpanded: [0], //index of content(s) open by default [index1, index2, etc]. [] denotes no content.<br /> onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)<br /> animatedefault: true, //Should contents open by default be animated into view?<br /> persiststate: true, //persist state of opened contents within browser session?<br /> toggleclass: ["", ""], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]<br /> togglehtml: ["none", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively ["position", "html1", "html2"] (see docs)<br /> animatespeed: "normal", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"<br /> oninit:function(expandedindices){ //custom code to run when headers have initalized<br /> //do nothing<br /> },<br /> onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed<br /> //do nothing<br /> }<br /> })<br /> </script>
You can play with these values to get the desired effect, such as opening the sub categories on mouseover rather than click.
Now hopefully with a bit of styling you should be in business. If you have any questions/problems with this, leave a comment and I’d be happy to help.
Credit for the ddAccordian script goes to the guys at www.dynamicdrive.com. They have some pretty cool scripts over there, you should check it out.
Incase you missed it, you can download my AccordianMenu file from http://www.msinternet.co.uk/AccordianMenu.txt
Ben
Creative Lead
MS Internet
The future of eCommerce – mCommerce?
June 23, 2010 - 9:08 pm
Tags: Android, iphone, Magento, mcommerce
Posted in General, iphone, Magento, Marketing | 10 comments
After participating in the Magento Mobile webinar here is a good review of mCommerce and mobile development in general
Magento – Show cart quantity
March 6, 2010 - 1:05 pm
Tags: Magento, php
Posted in General | 14 comments
If you’re putting a shopping cart area anywhere other than the standard cart side block, then you’ll probably want to show how many items are in the cart at any one time. Here’s how:
Magento – Handy code snippets
March 6, 2010 - 12:57 pm
Tags: Magento, php
Posted in Code, Magento | 3 comments
Here’s a short list of magento codes to do some of the simple stuff. It’s a little empty at the moment but I’ll keep adding to it over time. Check if customer is logged in <?php $_customer = Mage::getSingleton(‘customer/session’)->isLoggedIn(); if ($_customer) { // do stuff } ?> Get product image <?php echo $this->helper(‘catalog/image’)->init($_product, ‘image’); ?> [...]
Magento – Show image from a random product in a specific category
March 6, 2010 - 12:39 pm
Tags: Magento, php
Posted in Code, Magento | 5 comments
A website we developed recently required some boxes on the homepage that link through to a specific category each, and each box needed to show an image from a random product from within that category. If you would like to do the same, here’s how it’s done: Create a PHTML file in your template folder, [...]
Magento – Swap product image on thumbnail click
March 6, 2010 - 12:10 pm
Tags: Magento, php
Posted in Code, Magento | 9 comments
One of the most frequent requests we get for Magento sites is to tame what the thumbnails do on the product page. Of course there are plenty of plugins for adding a lightbox of some sort, but if you simply want to swap out the image in the zoomer window for one of the thumbnails, [...]
Magento SagePay Form
February 25, 2010 - 3:15 pm
Tags: Magento, php
Posted in Code, Magento | 14 comments
SagePay is the UK and Ireland’s most popular payment gateway and Magento is rapidly gaining popularity so the absence of a SagePay Form Magento plugin is not ideal. Hopefully we have helped to fill that gap with our free plugin available at http://www.magentocommerce.com/extension/3060/sagepay-formerly-protx-form.
MS Internet Magento Easy Pre-Installer
February 18, 2010 - 11:42 am
Tags: Magento, php
Posted in Code, Magento | 2 comments
To speed up the process of uploading the Magento software without SSH access we have created this easy downloader to fetch and unpack the files and install the sample data in a MySQL database.
How to get ImageMagick to Convert pdf’s with GhostScript
December 14, 2009 - 5:22 pm
Tags: Code, ImageMagick, php
Posted in Code | 2 comments
Ok, this is one for the nerds. I have spent most of today trying to figure out how to get ImageMagick to convert a pdf file to an image. First I installed ImageMagick fine: > wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz > tar xvfz ImageMagick.tar.gz > cd ImageMagick-6.5.8 > ./configure > make > make install Then I installed the [...]
How to fix PushIt WordPress plugin wp-admin 500 Error
November 14, 2009 - 8:21 pm
Tags: php, wordpress
Posted in Code | 4 comments
We had a client with a mysterious plugin related server error when they tried to access thair admin dashboard with output: /*pushit_user_row_phone_variable_define_start*/ $phone = isset($user_object->phone_number) ? get_user_mobile($user_object) : “not set”; /*pushit_user_row_phone_variable_define_stop*//*pushit_user_row_phone_variable_insert_start*/break; case ‘phone’: $r .= “$phone”;/*pushit_user_row_phone_variable_insert_stop*/ They said that the problem had happened immediately after installing the PushIt plugin. So, we took the usual action [...]
Utility PHP Email Detection Script
November 14, 2009 - 8:17 pm
Tags: email, php, regular expression
Posted in Code | 1 comment
I wrote a little function to check the formatting of an email so I thought I would share it with the world: <?php function is_email($email) { if(eregi(“^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$”, $email)){ return 1; } else { return 0; } } ?>

March 6, 2010 - 11:26 pm
This is really useful, thanks. You should consider bundling this within a Magento-Connect Extension.
March 7, 2010 - 4:28 pm
i have done modify exactly how on description but i have ever this error:
/* End if there are categories */
Fatal error: Call to a member function setCurrentCategory() on a non-object in /home/studiodue/www/app/design/frontend/default/studiodue/template/catalog/navigation/vert_nav.phtml on line 85
AccordianMenu.phtml
i use magento 1.3.2.4
and i have tryed too 1.3.2.1
March 7, 2010 - 11:45 pm
it works fine .. thanks!!
i wait to tutorial for add dynamic tree menu.. for more than 2 levels of categories … thanks
March 8, 2010 - 12:07 pm
Good to see you got it working ben24. What was the problem?
I’ll post the tree menu tutorial in a week or two.
March 10, 2010 - 6:22 am
http://www.msinternet.co.uk – da mejor. Guardar va!
Gracias
[url=http://www.hope4live.com/]Boldy[/url]
April 1, 2010 - 4:31 pm
hi I am really interested in get a mega menu style on magento v1.4, no matter where I look I can not find a tutorial anywhere? would be so kind to help me out on creating one of these? a good example of what I mean is on asos.com.
Thanks
April 6, 2010 - 6:41 pm
Hi there.
Thanks for the post, but…
I did all the above and ended up with a top menu with no child categories.
Hope someone has made a step by step guide, how to use this more precise.
I’m using Magento 1.4.0.1
Thanks
-M
April 12, 2010 - 2:27 pm
Hi Michael,
My guess is that you havn’t referenced the navigation file in catalog.xml
I’ve updated the post, see step 5.
April 13, 2010 - 7:33 pm
Hi !
Thanks a lot for this! been looking all over the net on how to do this. However I have a problem when using this, When on first level subcategories all my menus expand. Do you know what the problem might be?
April 14, 2010 - 9:37 am
hi,
have u done tutorial to dynamic tree menu system to deal with tmore than 2 levels of categories?
thnks!
April 14, 2010 - 10:20 am
Hi Yeggeps. Do you have a link to the site your working on? I could have a look for you. It could be a setting in the ddaccordion.init (in head.phtml) maybe.
@ben24, I’m working a bit of overtime this Saturday so I should be able to get something up. If I don’t have time I can at least send over the scripts I used if you want to leave me your email address.
You can both contact me directly at ben@msinternet.co.uk
April 14, 2010 - 12:17 pm
Hey, here’s the site Im working on.
Checked the settings in ddaccordion.init but couldn’t figure it out. thanks
http://83.209.5.128:8888/webbshop/magento/
//Initialize first demo:
ddaccordion.init({
headerclass: “navhead”, //Shared CSS class name of headers group
contentclass: “navcontent”, //Shared CSS class name of contents group
revealtype: “clickgo”, //Reveal content when user clicks or onmouseover the header? Valid value: “click”, “clickgo”, or “mouseover”
mouseoverdelay: 400, //if revealtype=”mouseover”, set delay in milliseconds before header expands onMouseover
collapseprev: true, //Collapse previous content (so only one open at any time)? true/false
defaultexpanded: [], //index of content(s) open by default [index1, index2, etc]. [] denotes no content.
onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
animatedefault: false, //Should contents open by default be animated into view?
persiststate: true, //persist state of opened contents within browser session?
toggleclass: ["", "openpet"], //Two CSS classes to be applied to the header when it’s collapsed and expanded, respectively ["class1", "class2"]
togglehtml: ["none", "", ""], //Additional HTML added to the header when it’s collapsed and expanded, respectively ["position", "html1", "html2"] (see docs)
animatespeed: “normal”, //speed of animation: integer in milliseconds (ie: 200), or keywords “fast”, “normal”, or “slow”
oninit:function(expandedindices){ //custom code to run when headers have initalized
//do nothing
},
onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
//do nothing
}
})
April 14, 2010 - 12:39 pm
OK, it looks as though your not calling the javascript files from the header quite right.
try putting another ‘/’ before ‘skin’:
/skin/frontend/default/blank_seo/js/jquery-1.3.2.noConflict.min.js
/skin/frontend/default/blank_seo/js/ddaccordion.js
April 14, 2010 - 1:05 pm
Uhm, I’m sorry, you might have tried to access the site when I had URL rewrites turned off, the menu didn’t work then.
The script is loaded and works, and I can expand the parent categories, but when I click on a subcategory it expands all parent categories, instead of just the one for that particular parent.
Big thanks
April 14, 2010 - 1:25 pm
I see your problem. I think putting the ‘/’ before skin should solve your problem.
The javascript is being called on the home page, but when in the subcategories it isn’t being found at all, which is probably down to the script being called relative to the page you are on.
If you put a slash before the skin, the script will always be called in relation to the root. Try it and let me know.
April 14, 2010 - 3:05 pm
Ah! It didn’t work since I don’t have the site in the url root.
Thanks a lot man!
April 14, 2010 - 3:36 pm
No problem, glad you got it sorted
April 21, 2010 - 11:37 am
Trying to implement this menu.
But i’m running into a few problems..
I get a menu with all parent categories with working links but no interaction other than click and no subcategories.
What am i doing wrong?
April 24, 2010 - 11:07 am
Hi Rizz,
Leave me the URL to your site and I can have a look for you, it could be a number of things but probably a similar problem to what Yeggeps had.
May 2, 2010 - 1:57 pm
http://www.dsivoordeel.nl
Is the site i’m trying to implement the menu on.
May 5, 2010 - 4:10 pm
Thank you very much!!
Something else, do you know how to make a mega drop down menu with magento?
Make a tutorial Pleasssssssssssssssssssseeeee!!
May 5, 2010 - 4:32 pm
Hello Ben!!
I have a problem and i don’t have idea how to fix it.
My Home Page shows my menu like this:
Blister pack
/* Get subcategories and loop through if there are some. */
* kids
* Assembled
/* End loop through sub categories */
/* End if child categories */
Liste packs
/* Get subcategories and loop through if there are some. */
* Teen
/* End loop through sub categories */
/* End if child categories */
Deluxe packs
/* Get subcategories and loop through if there are some. */
* RR
/* End loop through sub categories */
/* End if child categories */
AAAAAAAA
/* Get subcategories and loop through if there are some. */ /* End if child categories */ /* End if there are categories */ /* Set the category back to whatever category the user is in */
Information Page
/* Additional navigation links if you want something in the navigation other than categories. */
Information Page
Information Page
HELP me please!!
May 16, 2010 - 3:22 am
Hi there…
Thanks for the tutorial, been scouring for this for ages… implementing at http://www.shainsware.co.nz (currently playing with loads of features on that page).
However, having followed your steps, my site universally detonates!
I get a blank Magento Error screen, with “there has been an error processing your request” and then nothing else intuitive from there.
I’ll go back to the top and start again, but am a bit lost…. thanks in advance for nudges in the right direction.
Cheers,
Todd
May 16, 2010 - 5:36 am
My bad… spelling errors!!
May 16, 2010 - 5:39 am
Ok, a few q’s…
1 – I also get all the children at first load. Will try and resolve this.
2 – is there a stock CSS to stylise the menu?
3 – My mouseover on the PARENT is a ‘text’ icon, and not the intuitive ‘finger’ icon to know its a link to expand the accordion. Any clues?
Cheers,
Todd
May 16, 2010 - 5:49 am
Me again!
As per above…
1 – Changed the defaultexpanded to [] which solved this.
2 – Ripped from your demo site, so had something to work with…. (sorry & thanks!).
3 – Resolved with the CSS.
————–
One BUG I’ve noticed, if a PARENT category has no CHILDREN, then it would make sense that a Click would take you to that page. Currently clicking on a PARENT with no children does nothing, and you basically have a dead menu item.
I’ll code around this, but could lead to future frustrations. Perhaps needs a mod option.
Cheers,
Todd
May 18, 2010 - 4:55 pm
Hi Rizz.
I checked your source code and you don’t seem to be referencing the JavaScript files at all.
Check points 1 to 3 again and you should get it fixed.
May 18, 2010 - 4:58 pm
Very strange. Do you have the PHP code between PHP tags? < ?php and ?>
If you send me a link to the site you’re working on I may be able to help you more.
May 18, 2010 - 5:25 pm
Hi Todd,
Thanks for that, I’ve just updated the accordian menu code which includes a work around for that.
If the category doesn’t have any subcategories, it assigns it a class of navhead2 (instead of navhead, to stop it being animated), and gives it a link to the category.
You can download the updated code from: http://www.msinternet.co.uk/AccordianMenu.txt
Hope that helps.
May 19, 2010 - 5:01 am
Hi, I would love to check out the dynamic tree category menu, would you be able to email me a copy of the files/code?
Tim
May 19, 2010 - 6:44 am
Sweet, thanks for the fix. Will implement in due course.
I’ve been doing a tarted-up version (graphically) in a theme I’m developing at: http://www.shainsware.co.nz
One issue I can’t seem to crack, is forcing the HOVER state of the navhead to “stick” when the submenu is expanded. Currently when you mouse-off the navhead it returns to its original state (obviously, because not ‘hover’ing any more). Almost need some kind of toggle-state for clicked/unclicked etc…
Can this be done in CSS?
Cheers,
Todd
May 19, 2010 - 9:55 pm
It’s a feature included in the accordian script. Check the init settings for it…
…
toggleclass: ["", "openpet"], //Two CSS classes to be applied to the header when it’s collapsed and expanded, respectively ["class1", "class2"]
…
I’ve not tested it myself, but the comment suggests that it assigns the navhead a second class depending on whether it’s open or not.
Nice design for the menu by the way, might have to take some inspiration from it myself!
Let me know if you get the open state to work.
Ben
May 19, 2010 - 10:00 pm
Hi Tim,
All the code is in the tutorial above, so follow it through carefully and you should be fine.
Ben
May 28, 2010 - 9:06 am
i didn’t reference the js scripts for a while because it stopped my normal menu from working.
Its now in noconflict mode. Menu works but still i only get the top categories and no other ones.
I’ll leave it in current config could you have another look Ben? http://www.dsivoordeel.nl
May 28, 2010 - 9:30 am
little update.. the accordion seems to be working fine now.
But ofcourse it by default takes the root category as top category. i need it to start building the tree on the second node.
Currently its shows -> webshop + all sub categories.
I need to show subcategories as the root and the sub-sub categories to be expandable.
June 1, 2010 - 7:00 am
Hi Ben,
Does this work on Magento 1.4.0.1? I have tried everything multiple times and I keep coming up with an error on the AccordianMenu.phtml file that reads:
Parse error: parse error in C:\wamp\www\pet\app\design\frontend\default\modern\template\catalog\navigation\AccordianMenu.phtml on line 103
Is anyone else getting this?
June 10, 2010 - 2:00 pm
Your code is excellent.
But I have a question :
In the case of sub-sub category :
Category=>Sub cat=>sub sub cat.
How Can I modify your code to run it ?
June 11, 2010 - 2:27 am
RIZZ – When setting up you Categories in Admin you should leave the original “Default” one there (maybe rename it to “All Store”) and make your main product categories as Subs of those. Thats how mine is set-up, and works gloriously!
June 11, 2010 - 2:29 am
June 14, 2010 - 7:52 am
Thank you for your swift reply.
This will not really work for me because i use the main category as actually a menu in my topbar.
To set it up like you will show all menus in my top bar
June 17, 2010 - 10:21 am
Hi Ben,
Hows things…. have you had a chance to upload the scripts for the 3-level menu?
Cheers,
Todd
June 22, 2010 - 1:59 pm
can any one who had received the script for treemenu send me it at choussamaster (at) gmail.com
Thanks
June 23, 2010 - 8:57 pm
any help please !
June 28, 2010 - 12:48 pm
still nothing
July 16, 2010 - 10:11 am
Apologies to anyone waiting for the Tree version of this menu, I’ve been far too busy at work the past few weeks to spend time on the blogs unfortunately.
I was planning on writing a tutorial for the tree menu but obviously I havn’t got round to it, so to save you waiting any longer I’ve just uploaded the files I made/used for Waterfront Bathrooms.
You can get the files here: http://www.msinternet.co.uk/accordian_tree.zip
It’s not particularly pretty code, and the tree thing is quite complicated from what I remember, so be careful what you edit.
Make sure you edit the filepaths of the images in tree.js to the domain that you are using. PLEASE don’t hotlink the images from Waterfront.
Thanks,
Ben Dunn.
MS Internet.
July 16, 2010 - 10:28 am
Oh, and credit to Destroydrop for the original tree.js script.
You can see the original documents here: http://destroydrop.com/javascripts/tree/v1/