Archive for Magento

Magento: Custom Category Images Listing Block Tutorial

Recently, I just finished coding out a website for a client in Magento. You can view the site at www.3graces.com, it was a full switch from some old legacy asp shopping cart to Magento. Im really happy with the way things turned out and the client is too, Magento just has so many wonderful features built-in.

One feature that I found wasn't built in was a way to display each sub-category with an image, and its name on a landing page. Magento has a nice feature which allows you to assign an image to a category, but doesnt give you a way to make that show up in a listing somewhere.

I setup 6 main categories, with each having multiple sub-categories and I wanted to have a way to show all those sub-categories to the user on a nice page that was pleasing to the eye. Here is an example: Payot Paris by Category.

To achieve this layout, I created a block called sub_navigation.html and placed it in the following directory: app/design/frontend/3graces/default/template/catalog/navigaton/sub_navigation.html

PHP:
  1. <div id="categories">
  2. <div class="col_full">
  3.  
  4.        
  5. <div class="listing">
  6. <?php $_maincategorylisting=$this->getCurrentCategory()?>                       
  7. <?php $_categories=$this->getCurrentChildCategories()?>
  8.  
  9. <?php if($_categories->count()):?>
  10.            <? foreach ($_categories as $_category):?>
  11.                      
  12.                         <? if($_category->getIsActive()):
  13.                        
  14.                         $cur_category=Mage::getModel('catalog/category')->load($_category->getId());
  15.            $layer = Mage::getSingleton('catalog/layer');
  16.            $layer->setCurrentCategory($cur_category);
  17.  
  18.             $catName = $this->getCurrentCategory()->getName()
  19.            
  20.             if($_imageUrl=!$this->getCurrentCategory()->getImageUrl()):?>
  21.            
  22.             <div class="category-box">
  23.                 <div class="category-image-box">
  24.                     <a href="<?php echo $this->getCategoryUrl($_category)?>"><img src="/skin/frontend/3graces/default/images/category_image_default.gif"></a>
  25.                 </div>
  26.                 <div class="category-name"
  27.                     <p><a href="<?php echo $this->getCategoryUrl($_category)?>">
  28.                         <?php echo $catName ?></a></p>
  29.                 </div>
  30.             </div> 
  31.        
  32.             <?endif?>
  33.          
  34.             <? if($_imageUrl=$this->getCurrentCategory()->getImageUrl()):?>
  35.            
  36.          <div class="category-box">
  37.                 <div class="category-image-box">
  38.                      <a href="<?php echo $this->getCategoryUrl($_category)?>"><img src="<?php echo $_imageUrl?>" height="80"></a>
  39.                 </div>
  40.                 <div class="category-name"
  41.                     <p><a href="<?php echo $this->getCategoryUrl($_category)?>"> <?php echo $_category->getName()?></a></p>
  42.                 </div>
  43.             </div>
  44.  
  45.  
  46.  
  47. <?                                             
  48.                         endif;
  49.                         endif;?>
  50.             <?endforeach?>
  51.            
  52.             <?php /* This resets the category back to the original pages category
  53. ****     If this is not done, subsequent calls on the same page will use the last category
  54. ****    in the foreach loop
  55. */   ?>
  56. <?php $layer->setCurrentCategory($_maincategorylisting)?>
  57.     <?endif;?>
  58.  
  59.  
  60. </div>
  61. <br clear=all>
  62. </div>
  63. </div>

If you notice, I placed an if statement in the code that first checks if an image has been set for the category. If one has, it will show it - if there isn't an image it will show a default image that I have chosen. Here is an example, when only 1 image for a category has been setup, and the rest are the default.

To make the block show up as an option in Magento Admin, you will need to go to CMS > Static Blocks. Once you get there, click Add New Block and fill in the following fields.

Block Title: Sub Category Listing
Identifier: sub_category_listing
Status: Enabled
Content: {{block type="catalog/navigation" template="catalog/navigation/sub_category_listing.phtml"}}

Once you have added all those fields, you can click save. Now to enable this static block for a parent category that contains a bunch of sub-categories with images that are set.
Go to: Catalog > Manage Categories

Then click on the parent category to which you wish to add this layout, scroll to the bottom of the first tab "General Information".

You will see the following fields, just setup as follows:

Display Mode: Static Block Only
CMS Block: Sub Category Listing
Is Anchor: Yes

Now if you go back to your category on the website, you should see your newly setup sub category image listing page. If you arent seeing any images, make sure you setup you default image and placed it in the current directory within your magento install.

Thanks!

Comments

Magento: How to use the Import/Export Tool

The import/export tool is very useful within Magento. Being that there is limited documentation on the topic, I thought I would add my .02.

Basically you can use the import/export tool to do mass imports of products. It makes it much easier if you would like to add 50 products all at once, you would just create a csv or xml file with the matching fields for the database, add your information and then use the import tool to import the products. You can retrieve a sample csv or xml file, but doing an export.

Doing an export is very easy, and allows you to export all of your products out of your website. You might want to do this, if you are moving your website or need to reinstall or upgrade your website, and need to get the products out.

Its a very useful tool.

Comments (2)

An Introduction to Magento: Open Source Ecommerce

I have been learning Magento for the past few months, for a client's website that I have been working on. Its a pretty robust Open Source Ecommerce plaform that is built on the Zend PHP Framework. At first, it was rather daunting trying to find my way around the software - its pretty robust and the documentation for creating a theme feels rather incomplete. It's nothing like WordPress which I would take in a heartbeat, if they had a solid enough Ecommerce plugin, I have been spoiled by the WordPress Theme engine for quite some time now.

To make my way around Magento, I have been finding bits and pieces of information scattered across many websites. To make it easier for others, Im going to try to assemble the basics here for you.

The hardest piece for me to grasp has been the folder structure, you have to bounce between two places to build a theme, here is a rundown.

http://www.magentocommerce.com/wiki/Magento-folder-structure/

  • /skin// / - is where design package css and images are
  • /app/design is location of design packages (layouts, templates, translations)
  • /app/design// - theme customizations
  • /app/design// /layout - .xml files that define block structure for different cases in website flow
  • /app/design// /template - .phtml (html with php tags) templates
  • /app/design// /translate - Zend_Db compatible translation files

Common Template Path Hints

<?=$this->getSkinUrl('css/styles.css')?>
<?=$this->getJsUrl()?>

Online Resources

Comments (2)