For new comers or novice Magento module developers, you may want to head over to part 1 of the Magento Module Development series
Creating a lightbox plugin for Magento product pages
Armed with the knowledge to create a Magento module, it’s now time to do something with our new found prowess! We are going to start by augmenting existing functionality in Magento. I think the pop ups for product images leave a lot to be desired. I think it’s time we spice things up a bit and add a light box feature to those picture thumbnails.

Default pop-up for a product. Bleh!
We want something a little fancier. We are going to pick up where we left off in part 1 and use our module knowledge to implement jQuery colorbox on our product pages:

Colorbox product - very much sexy!
Setup config.xml
Location: /app/code/local/Oneupweb/ProductColorBox/etc
The config.xml is responsible for wiring up the module. It is used to control layout, models, blocks, and resource configurations (like connection parameters). Since we are only focused on modifying the layout, we will be focusing on the layout portion. Create a file called config.xml and save it in the modules etc folder. Add the following:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Oneupweb_ProductColorBox>
<version>0.1.0</version>
</Oneupweb_ProductColorBox>
</modules>
<global>
</global>
<frontend>
<layout>
<updates>
<productcolorbox>
<file>productcolorbox.xml</file>
</productcolorbox>
</updates>
</layout>
</frontend>
</config>
But what does it all mean???
- modules
- This node is boilerplate. It really just contains the name of the module. The version node is for showing version information (surprising I know)
- global
- This is where model, block and resource configs go
- global
- This is where model, block and resource configs go
- frontend
- This is where non-admin layout, and rout configs go; this is where our focus will be
Let’s just make some observations on the layout portion of the xml:
<!-- We have some layout configs -->
<layout>
<!-- Magento renders "updates" on the backend. Dont worry about it, this will always be here -->
<updates>
<!-- Just an identifier for you. You can call it whatever. HotHamSandwich would work -->
<productcolorbox>
<!-- The name of the layout file in the theme you are using -->
<file>productcolorbox.xml</file>
</productcolorbox>
</updates>
</layout>
You will notice that our layout config points to a file. What is this file you ask? Onward!
Creating the layout file!
Layout files are used to configure view blocks. We are going to use this handy feature to add our scripts and styles to the product page. The key here is to make your module work with the current theme (not the most pluggable, but magento has ways of deploying this stuff). So let’s add our layout file:
Location:/app/design/frontend/default/layout/
Layouts and templates live in the /app/design folder. Navigate to the above directory (If you are using a different theme it would be /app/design/frontend/YOURTHEME/layout) and create the file we mentioned in our config.xml:productcolorbox.xml
Here is where things get a little tricky. Remember this xml file configures view blocks. From a layout xml file you can reference existing blogs, wire up new ones, and call methods that exist on these blocks. We are going to use our layout xml file to reference the head block on the product view page. Put the following in our productcolorbox.xml file and save:
<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
<catalog_product_view>
<reference name="head">
<action method="addJs"><script>oneupweb/productcolorbox/jquery-1.6.4.min.js</script></action>
<action method="addJs"><script>oneupweb/productcolorbox/jquery.colorbox-min.js</script></action>
<action method="addJs"><script>oneupweb/productcolorbox/init.js</script></action>
<action method="addCss"><stylesheet>css/colorbox.css</stylesheet></action>
</reference>
</catalog_product_view>
</layout>
Let’s dissect each of these nodes:
layout
This is where all your custom layout configurations go for your module. In addition to the top level layout node, you can also use default which targets things that are loaded on every page.
catalog_product_view
This little bit is important. It instructs Magento to load configurations on a specific page. The above pattern is the following: module_controller_action , which essentially pans out to /catalog/product/view. So in our example here we are configuring our blocks for this page, and this page only.
reference
This node allows you to reference an existing block. The name attribute is the name of the block you are referencing. In this case we are referencing the head block.
action
This allows you to call methods on the block you are targeting. The method of the block you are calling is specified in the method attribute. We are calling the addJs method of the Head block in /app/core/Mage/Page/Block/Html/Head.php. You can verify this by opening that file:
/**
* Add JavaScript file to HEAD entity
*
* @param string $name
* @param string $params
* @return Mage_Page_Block_Html_Head
*/
public function addJs($name, $params = "")
{
$this->addItem('js', $name, $params);
return $this;
}
Any node passed to the action node is treated as an argument that is passed to the method. We use script and stylesheet here, but it could just as easily be name or silly_hat. Some points to know about the addJs and addCss methods.
addJs adds scripts relative to /app/js
addCss adds stylesheets relative to /skin/frontend/current_interface/current_theme in my case /skin/frontend/default/default
Setup the product template
We have one minor setup here. We are going to modify the product media template to create a simple hook for our JavaScript to use on the product images. The product view template makes a call to include the output from the media template which is located at /app/design/frontend/base/default/template/catalog/product/view/media.phtml. We are going to modify the base template so it shows up in all themes, but you could just as easily recreate this directory structure in your own theme. We are going to modify the portion of this template where the block fetches gallery images for a product. We will be adding an html5 data attribute (don’t worry, these still work in older browsers). Head to the loop of gallery images and make sure it looks like this:
<?php foreach ($this->getGalleryImages() as $_image): ?>
<li>
<a data-url="<?php echo $_image->getData('url'); ?>" href="#" onclick="popWin('<?php echo $this->getGalleryUrl($_image) ?>', 'gallery', 'width=300,height=300,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes'); return false;" title="<?php echo $this->htmlEscape($_image->getLabel()) ?>"><img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(56); ?>" width="56" height="56" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" /></a>
</li>
<?php endforeach; ?>
The only thing we changed was the addition of the data-url attribute which stores the url to the gallery image. We will use this in our JavaScript.
Download libraries and create scripts!
Download our dependencies:
Then create our script folder we laid out in our layout xml and save our scripts to it.
/js/oneupweb/productcolorbox
add the colorbox css and it’s accompanying image folder to our skin css folder as we wired it up in the layout xml.
/skin/frontend/default/default/css
Next create the init.js script in our productcolorbox js folder as mentioned in the layout xml file and paste the following:
//be sure to use noConflict because Magento ships with the prototype library
jQuery.noConflict();
jQuery(document).ready(function(){
var thumbs = jQuery('.more-views a');
thumbs.each(function(){
var anchor = jQuery(this),
onClck = anchor.attr('onclick'),
matches = [],
imageHref = anchor.attr('data-url');
anchor.removeAttr('onclick');
anchor.attr('rel','cbox').attr('href',imageHref);
});
jQuery('a[rel=cbox]').colorbox();
});
That’s it! You now have a working lightbox for your product galleries and a solid understanding of how to spice up your catalog by adding scripts and styles via your custom modules.
Conclusion
Magento is a highly configurable system. To some it may seem like there are just too many steps (In fact I sit inside this camp), but once you understand the flow of this eCommerce powerhouse, you will be able to effectively develop and extend it with ease. You might find there aren’t as many limitations as you may have first believed.
GD Star Rating
loading...