rss
twitter
    Find out what I'm doing, Follow Me :)

Friday, December 4, 2009

Magento Vertical Category Menu

Magento vertical category menu with the code adapted from this thread. It’s quite easy to implement and to make it even easier for others.

* Add a new block type in "catalog.xml" file
* Create a new phtml file and name it "leftnav.phtml", place it inside catalog/navigation
* Edit CSS

Add a new block type in "catalog.xml" file
Here we presume you want to have the vertical category menu shows up on every page, to do so, we will place the code in the <default> layout like so, and we want it to always stay on the top, so we added "before="-": 
<default> 
<!-- Mage_Catalog --> 
<reference name="right"> 
<block type="catalog/navigation"before="-" 
name="leftNav" as="leftNav" template="catalog/navigation/leftnav.phtml"/> 
</reference>

You can change the reference name from ‘right’ to left if you prefer to have the vertical menu placed on the left column. 

Create a leftnav.phtml file
<div class="sidebox"> 
<h6>Browse Category</h6> 
<ul id="side-nav"> 
<?php foreach ($this->getStoreCategories() as $_category): ?> 
<?php if ($_category->getIsActive()) { ?> 
<?php $open = $this->isCategoryActive($_category); ?> 
<?php $potential = $_category->hasChildren(); ?> 
<li class="subcat"><a href="<?php echo $this->getCategoryUrl($_category); ?>"<?php if($open) { echo ' class="open"'; } ?><?php if($potential) { echo ' class="potential"'; } ?> ><?php echo $_category->getName();?></a> 
<?php if ($open && $potential): ?> 
<ul id="sub-sidenav"> 
<?php foreach ($_category->getChildrenCategories() as $subcategory): ?> 
<?php $subCat = Mage::getModel('catalog/category')->load($subcategory); ?> 
<?php $open = $this->isCategoryActive($subCat); ?> 
<?php $potential = $subCat->hasChildren(); ?> 
<li><a href="<?php echo $this->getCategoryUrl($subCat); ?>"<?php if($open) { echo ' class="subopen"'; } ?><?php if(!$potential&&$open) { echo ' class="final"'; } ?> ><?php if($potential&&$open) { echo ' '; } elseif($potential) { echo ' '; }?><?php echo $subCat->getName(); ?></a> 
<?php if ($open && $potential): ?> 
<ul> 
<?php foreach ($subcategory->getChildrenCategories() as $subsubcategory): ?> 
<?php $subsubCat = Mage::getModel('catalog/category')->load($subsubcategory); ?> 
<?php $open = $this->isCategoryActive($subsubCat) ?> 
<li><a href="<?php echo $this->getCategoryUrl($subsubCat); ?>" <?php if($open) { echo ' class="final"'; } ?>><?php echo $subsubCat->getName(); ?></a></li> 
<?php endforeach; ?> 
</ul> 
<?php endif; ?> 
</li> 
<?php endforeach; ?> 
</ul> 
<?php endif; ?> 
</li> 
<?php } ?> 
<?php endforeach ?> 
</ul> 
</div> 

Edit your CSS 
 ul#side-nav li.subcat {background-color:#f5f4f0;border-bottom:1px solid #ddd; 
color:#222;margin: 0 -5px;display: block;} 
ul#side-nav li.subcat a {display: block;padding: 2px 10px;font-size: .85em;} 
ul#side-nav li.subcat:hover { 
background-color:#fefefe;background-position: 50% top;} 
ul#side-nav li.subcat ul li {padding-left:18px; 
line-height: 1.6;background:#fff } 
ul#side-nav li.subcat ul li a {color: #1a443c;font-weight: bold; 
font-size: .85em!important; 
background: url(../images/base_images/icon-arrow-set.png) no-repeat 0 -59px;} 
ul#side-nav li.subcat ul li:hover {text-decoration: underline;} 
ul#side-nav li.subcat a , 
ul#side-nav li.subcat ul li a:hover {text-decoration: none;}

That’s it! Simple isn’t it?
Known issue in Magento version 1.3

The recent release of Magento v1.3 has introduced Flat Catalog Category and Flat Catalog Product features, and some codes had changed in code/core/Mage/Catalog/Naviation.php that resulting Peter’s code not compatible with v1.3, as such, you need to update: 

<?php foreach ($_category->getChildren() as $subcategory): ?>
to <?php foreach ($_category->getChildrenCategories() as $subcategory): ?>

if you wish to switch on Flat Catalog Category; however, we found a bug related to custom options when add to cart is performed with Flat Catalog Category switched on. The bug has been assigned, and before Varien releases an update, your choice is to use the old code with "getChildren" with no Flat Catalog Category enables if you have Configurable products or Simple Products with Custom Options setup. 

The above leftnav code is for Flat Catalog Category switch on. Simply revert "getChildrenCategories()" to "getChildren()" if you do not want to use Flat Catalog Category feature. 

Source : http://asia-connect.com.vn/

Thursday, December 3, 2009

Add a footer, Header or Content static callout block for Magento CMS page

Magento offers lots of flexibility for Magento theme design, but it takes a bit of digging and exploring before one can learn to do a new simple trick.

In the Magento default template, it shows us how to place a permanent callout block in the left column by using the

<reference name="left">...</reference>

Example of the code is located in the catalog.xml, right in the <default> section.

<reference name="left">
<block type="core/template" name="left.permanent.callout" template="callouts/left_col.phtml">
<action method="setImgSrc"><src>images/media/col_left_callout.jpg</src></action>
<action method="setImgAlt" translate="alt" module="catalog"><alt>Our customer service is available 24/7. Call us at (800) DEMO-NUMBER.</alt></action>
<action method="setLinkUrl"><url>checkout/cart</url></action>
</block>
</reference>

This is one way of doing it. Though it’s nice, easy and straight-forward, but it lacks the flexibility and the method maybe a bit intimidating to people who know very little about web design and get nervous on touching the source code. So, to make it a bit more friendly for store owner, we can replace the above code to a static block with a block ID, so that store owner can add a new static block in the admin/CMS/Static Block, and can place as many images and content as he sees fit.

How do we do that?
To achieve this, we use the following code instead, and name the block ID as left_column_block. Note that because I wanted this callout block shows up in every page, so I have it placed in the  section just like the example Magento default theme shows.

<reference name="left">
<block type="cms/bock" name="left.permanent.callout ">
<action method="setBlockId"><block_id>left_column_block</block_id></action>
</block>
</reference>

Then I go to admin/CMS/Static Block, add a new block, enter a block title, enter left_column_block as Identifier; enable it, and enter something in the content area. In this example I am using a promotional teaser image file:

<a href="http:/your-domain.com/destination-of-this-image"><img src="" width="206" class="callout" alt="25% off " /> </a>

How about taking it to the next level?
With the same method, we can extend the static block to header, footer and the content area. In the following example, I am going to show you how to do it for the footer block.

To do this, we follow the same step as the left_column_block and reference it to footer; because it’s most logical to have this footer static block shows up throughout the entire site, so I am going to place it in the <default> section in the catalog.xml and I named it footer_callout_block.

<reference name="footer">
<block type="cms/block" name="footer.permanent.callout" as="footer_callout">
<action method="setBlockId"><block_id>footer_callout_block</block_id></action>
</block>
</reference>

One more step before creating a new static block in the admin CMS.

In order for the footer callout block shows up, we need to add a “getChildHtml” to invoke the block in the CMS static block. So we go open up the footer.phtml which is located in the page/html. Note that to have the block shows up exactly where you want it be, requires a bit of CSS coding, but I will skip this part as this is a design decision that you should make, not me. In my example, I have a 2 columns floated layout in the footer, and I want this footer_callout_block placed on the right column, so I have it placed inside the col2 like so:

<div class="col2">
<?php echo $this->getChildHtml('footer_callout') ?>
</div>

Then I proceed to the last step by creating a new static block in the admin CMS, and name the following:

Block Title: Footer Callout Block
Identifier: footer_callout_block
Content : <a href=""> <img src="" width="377" alt="ship via USPS and UPS" /> </a>


Source : http://www.lotusseedsdesign.com/

Working with AJAX in Magento

Ajax in Magento can be pretty troublesome.Because you will need to take controllers and layout into account.And I almost used up a whole day trying to make ajax work. Here are some of the steps I’d like to share so that you will not waste lots of your time, before making this work.
Let’s first know in Magento terms what we need.

Controller: The url on which Ajax will work on, or the request URL. You will have to set up the controller with its frontend router set in config.xml and its corresponding controller class.
Layout: Layout to handle the requested URL and return HTML if required.
Block: The block to call through layout for the above controller.

Now lets be descriptive.

If you want to show a loader while request is being processed just add the following whereever you like in the phtml page.First fetch the Javascript.

<script src="<?php echo $this->getJsUrl() ?>mage/adminhtml/loader.js" type="text/javascript"></script> 

Then echo the loader which pops up. This loader will be similar to that of the admin.

<div id="loadingmask" style="display: none;"> 
<div class="loader" id="loading-mask-loader"><img src="<?php echo str_replace("index.php/","",$this->getUrl()) ?>skin/adminhtml/default/default/images/ajax-loader-tr.gif" alt="<?php echo $this->__('Loading...') ?>"/><?php echo $this->__('Loading...') ?></div> 
<div id="loading-mask"></div> 
</div>

Now to Call Ajax, use the following lines

/*Please note that the URL is created in reloadurl. Also see that the response text will be echoed in div with id=output-div*/ 
   
var reloadurl = '<?php echo $this->getUrl('router/controller/action') ?>'; 
Element.show('loadingmask'); 
new Ajax.Request(reloadurl, { 
method: 'post', 
parameters: "Params_Here", 
onComplete: function(transport) { 
Element.hide('loadingmask'); 
$('output-div').innerHTML = ""; 
$('output-div').innerHTML = transport.responseText; 
  
} 
});

After the Ajax Request is made it goes to your controller’s action, which in turn sees to your layout as follows:

class Namespace_module_frontendController extends Mage_Core_Controller_Front_Action  
{ 
public function actionAction(){ 
$this->loadLayout()->renderLayout(); 
} 
}

And here is the main part in layout where I spent most of my time in. Since in layout we will have to define reference where the html will echo (Most of the times), i got stuck here, because i need to echo the output on the div with id output-div not in any reference.And the trick is to name the layout as root and output as html. Like the following:

<module_controller_action> 
<block type="module/block"  name="root" output="toHtml"  template="module/template.phtml"/> 
</module_controller_action>

You are done now! What ever you write or echo in phtml file you will see populated in the DIV. Now you can treat block as normally as you do before.

Source: http://subesh.com.np/

Wednesday, December 2, 2009

Add New Reference in Magento

If you already performed some Magento research, you will know that it is built on a fully modular model that gives great scalability and flexibility for your store. While creating a theme, you are provided with many content blocks that you can place in structural blocks.Magento provides few structural blocks by default and many content blocks. This article tells what needs to be in place to create new structural block.

What are structural blocks?
They are the parent blocks of content blocks and serve to position its content blocks within a store page context.These structural blocks exist in the forms of the header area, left column area, right column..etc. which serve to create the visual structure for a store page. Our goal is to create a new structural block called "newreference".

Step 1: Name the structural block

Open the file layout/page.xml in your active theme folder. Inside you will find lines like:

<block type="core/text_list" name="left" as="left"/>
<block type="core/text_list" name="content" as="content"/>
<block type="core/text_list" name="right" as="right"/>

Let’s mimic this and add a new line somewhere inside the same block tag.

<block type="core/text_list" name="newreference" as="newreference"/>

Good. Now we told Magento that new structural block exists with the name "newreference". Magento still doesn’t know what to do with it.

Step 2: Tell Magento where to place it

We now need to point Magento where it should output this new structural block. Let’s go to template/page folder in our active theme folder. You will notice different layouts there. Let’s assume we want the new structural block to appear only on pages that use 2-column layout with right sidebar. In that case we should open 2columns-right.phtml file.

Let’s assume we wish the "newreference" block to be placed below 2 columns, but above the footer. In this case, our updated file could look like this:

<!-- start middle -->
<div>
<div>< ?php getChildHtml('breadcrumbs') ?>
<!-- start center -->
<div id="main"><!-- start global messages -->
< ?php  getChildHtml('global_messages') ?>
<!-- end global messages -->
<!-- start content -->
< ?php  getChildHtml('content') ?>
<!-- end content --></div>
<!-- end center -->
 
<!-- start right -->
<div>< ?php getChildHtml('right') ?></div>
<!-- end right --></div>
<div>< ?php getChildHtml('newreference') ?></div>
</div>
<!-- end middle -->


Step 3: Populating structural block

We have the block properly placed, but unfortunately nothing is new on the frontsite. Let’s populate the new block with something. We will put new products block there as an example. Go to appropriate layout XML file and add this block to appropriate place.

<reference name="newreference">
<block type="catalog/product_new" name="home.product.new" template="catalog/product/new.phtml" />
</reference>

Source : http://asia-connect.com.vn/

Monday, November 30, 2009

Integrate shadowbox (alternative of lightbox) 3.0b with Magento

Here comes new steps to add latest shadowbox 3.0b to Magento, if you have any questions, please leave a comment so that I may help you:

Step 1:

Download Shadowbox, or you may download the modifed shadowbox package(because we will need to make some modifications in later steps).

Unpack you downloaded files into js/shadowbox-3.0b/ folder:

Put shadowbox under Magento js folder

Step 2:

Open /app/design/frontend/default/flower/layout/catalog.xml (in my exxample the bold Italic text would different from yours, the same as below) in your favourate text editor, find:

    <catalog_product_view>
        <!-- Mage_Catalog -->
        <reference name="root">
            <action method="setTemplate"><template>page/2columns-right.phtml</template></action>
        </reference>
        <reference name="head">
            <action method="addJs"><script>varien/product.js</script></action>

            <action method="addItem"><type>js_css</type><name>calendar/calendar-win2k-1.css</name><params/><!--<if/><condition>can_load_calendar_js</condition>--></action>

Replace is with:

    <catalog_product_view>
        <!-- Mage_Catalog -->
        <reference name="root">
            <action method="setTemplate"><template>page/2columns-right.phtml</template></action>
        </reference>
        <reference name="head">
            <action method="addJs"><script>varien/product.js</script></action>
            <!-- shadowbox javascript -->
            <action method="addJs"><script>shadowbox-build-3.0b/shadowbox.js</script></action>
                      
            <action method="addItem"><type>js_css</type><name>calendar/calendar-win2k-1.css</name><params/><!--<if/><condition>can_load_calendar_js</condition>--></action>
            <!-- shadowbox css -->
<action method="addItem"><type>js_css</type><name>shadowbox-build-3.0b/shadowbox.css</name><params/><!--<if/><condition>can_load_calendar_js</condition>--></action>

Step 3:

Insert shadowbox initialization code around line 32 (after ?> tag) in /app/design/frontend/default/flower/template/catalog/product/view/media.phtml, after inserting, the code would be like:

/**
 * Product media data template
 *
 * @see Mage_Catalog_Block_Product_View_Media
 */
?>
<script type="text/javascript">
Shadowbox.init();
</script>

There are more parameters can be used in initialization of shadowbox, please refer to its own usage documentation.

Remove all the lines bellow within this file start from 39 to 65:

<?php if ($_product->getImage() != 'no_selection' && $_product->getImage()): ?>
<p class="product-image-zoom">
    <?php
        $_img = '<img id="image" src="'.$this->helper('catalog/image')->init($_product, 'image').'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'" />';
        echo $_helper->productAttribute($_product, $_img, 'image')
    ?>
</p>
<p class="a-center" id="track_hint"><?php echo $this->__('Double click on above image to view full picture') ?></p>

<div class="image-zoom" id="track_outer">
    <img id="zoom_out" src="<?php echo $this->getSkinUrl('images/slider_btn_zoom_out.gif') ?>" alt="<?php echo $this->__('Zoom Out') ?>" class="btn-zoom-out" />
    <div id="track">
        <div id="handle"></div>
    </div>
    <img id="zoom_in" src="<?php echo $this->getSkinUrl('images/slider_btn_zoom_in.gif') ?>" alt="<?php echo $this->__('Zoom In') ?>" class="btn-zoom-in" />
</div>
<script type="text/javascript">
    Event.observe(window, 'load', function() {
        product_zoom = new Product.Zoom('image', 'track', 'handle', 'zoom_in', 'zoom_out', 'track_hint');
    });
</script>
<?php else: ?>
    <?php
        $_img = '<img src="'.$this->helper('catalog/image')->init($_product, 'image')->resize(265).'" alt="'.$this->htmlEscape($_product->getImageLabel()).'" />';
        echo $_helper->productAttribute($_product, $_img, 'image')
    ?>
<?php endif; ?>

After that, add following lines below:

<p class="product-image-zoom">
<a href="<?php echo $this->helper('catalog/image')->init($_product, 'image'); ?>"
title="<?php echo $this->htmlEscape($this->getImageLabel()); ?>"
rel="shadowbox[product]">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'image')->resize(300); ?>"
alt="<?php echo $this->htmlEscape($this->getImageLabel()); ?>"
title="<?php echo $this->htmlEscape($this->getImageLabel()); ?>" /></a>
</p>
<p class="a-center" id="track_hint"><?php echo $this->__('Click on above image to enlarge') ?></p>

And then, scroll down to line 72, you will see something like this:

            <a href="#" onclick="popWin('<?php echo $this->getGalleryUrl($_image) ?>', 'gallery', 'width=300,height=300,left=50,top=50,location=no,status=yes,scrollbars=yes,resizable=yes'); return false;"><img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(56); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" title="<?php echo $this->htmlEscape($_image->getLabel()) ?>" /></a>

Now, we need to add some attribute to make shadowbox working with product gallery images, here will be the new lines after adding:

        <a href="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile()) ?>"
        title="<?php echo $this->htmlEscape($_image->getLabel()) ?>"
        rel="shadowbox[product]">
                <img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(143); ?>"
                alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>"
                title="<?php echo $this->htmlEscape($_image->getLabel()) ?>" />
                </a>

Well, all done, if you downloaded my package of shadowbox, all should be working fine now. if not, please take more steps as below:

Step 4:

Right now, if you are using shadowbox library which was downloaded from its own website, you would found that a dark overlay shows up and image does not comes out when you trying to click the product image from the product page at front-end. And if you have firebug, you will see some error message as bellow:

    ({initialize:(function (config) { ... formatPrice:(function (price) {return formatCurrency(price, this.priceFormat);})}) is not a function
    http://your.domain/flower/js/index.php?c=auto&f=,prototype/prototype.js,prototype/validation.js,scriptaculous/builder.js,scriptaculous/effects.js,scriptaculous/dragdrop.js,scriptaculous/controls.js,scriptaculous/slider.js,varien/js.js,varien/form.js,varien/menu.js,mage/translate.js,mage/cookies.js,jquery/jquery-1.2.6.min.js,varien/product.js,shadowbox-build-3.0b/players/shadowbox-img.js
    Line 1500

and/or

    uncaught exception: Unknown player: img
    Line 0

This is because re-declaration of the variable(s) of javascript which is located in /js/varien/product.js, and I can tell you is: this is caused by shadowbox.

The solution is easy, open the file /js/shadowbox-build-3.0b/shadowbox.js, at line around 616, replace:

            each(document.getElementsByTagName('script'), function(s){
                if((path = path_re.exec(s.src)) != null){
                    S.path = path[1];
                    return false;
                }
            });

with:

            each(document.getElementsByTagName('script'), function(s){
                if((path = path_re.exec(s.src)) != null){
                    S.path = path[1].split(",");
                    S.path = S.path[0]+","+S.path[S.path.length-1];
                    return false;
                }
            });
 All Done..

Source: http://www.swiftcore.com/

Sunday, November 29, 2009

Set up the AddThis-button in your Magento theme

I’m sure many of you are big fans of the AddThis-button, that lets your users very easily add links to all major sites like Facebook, MySpace, blogs like Wordpress and Blogger plus it even let your users very easily send the page by email or print it out.

So, how do you add this great function to your Magento theme you might ask. Well its very simple. Just follow the steps below and you should be up running in no-time.

Step 1 – Create your AddThis-button

Go to http://www.addthis.com/web-button-select and create your own-styled AddThis-button, and then copy the code that is generated to your clipboard.

Step 2 – Add the code to your template-file

Open up your local template/catalog/view.phtml-file in Magento and find the following line:
                <h4><?php echo $this->__(’Quick Overview’) ?></h4>

Right above that line add a div style like this:
                <div style=”float:right;”>
                </div>

And paste the generated code in between that div-tag.

3 – Done

Well, thats actually it! If you run into any problems or have any suggestions around this functionality please feel free to add it as a comment in this article.

Source: http://www.silverthemes.com/

Thursday, November 26, 2009

How to edit/change contact page form in magento

Step 1:

Change the contact form located at:

    Code:
    1-/app/design\frontend/default/your_template/template/contacts/form.phtml

Step 2:

Once it is ready create a new email template to get all the form data and post. You can design the template or leave as simple as it is. For this goto

    Code:
    1-admin -> System -> Transactional Emails -> Add New Template

Then from Template drop down select contact form and load the template. Then you can edit as you like and save the form with some name.

Step 3:

Now you need to select the newly created email template to use for sending data captured from contact us page form. For this goto:

    Code:
    1-admin -> System -> Configuration -> Contacts

Now under email options select the new contact form that you just created and save it.

That's it. You are all done.

Source: http://www.codefight.org/page/47/41/how-to-edit-change-contact-page-form-in-magento.html