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; } } ?>
