Yet another blog (hopefully a useful one though)
Archive for March, 2010
Magento – Show cart quantity
Mar 6th
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:
<br /> $cart = Mage::getModel('checkout/cart')->getQuote()->getData();<br /> if(isset($cart['items_qty'])){<br /> if($cart['items_qty'] != 1) {<br /> echo "You have " . (int)$cart['items_qty'] . " items in your shopping cart";<br /> } else {<br /> echo "You have " . (int)$cart['items_qty'] . " item in your shopping cart";<br /> }</p> <p>} else {<br /> echo "You have 0 items in your shopping cart"<br /> }<br />
Magento – Handy code snippets
Mar 6th

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'); ?>
- Show image using current skin path (PHTML)
<img src="<?php echo $this->getSkinUrl('images/logo.png');?>" alt="logo" />
- Show image using current skin path (CMS)
<img src={{skin url="images/logo.png"}} />
- Show a custom block (CMS)
{{block type="catalog/product_featured" name="product_featured" as="product_featured" template="catalog/product/featured.phtml"}}
- Show a custom block (XML)
<block type="catalog/navigation" name="catalog.myblock" after="-" template="catalog/navigation/myblock.phtml"/> - Show CMS block (PHTML)
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('my_block_identifier')->toHtml() ?>
- Show short version of review summary
<?php echo $this->getReviewsSummaryHtml($_link,"short",true) ?>
Suggestions for any more helpful code snippets are more than welcome!
Ben
Creative Lead
MS Internet
Magento – Show image from a random product in a specific category
Mar 6th

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, such as app/design/frontend/yourpackage/yourtheme/template/catalog/navigation/box.phtml
- Put this code in the file:
$images = array(); $catId=37; // put your category ID in here $products = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('image') ->addCategoryFilter(Mage::getModel('catalog/category')->load($catId)); Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($products); // Only select products that are salable $products->load(); foreach($products as $product){ $images[] = $product->getImageUrl(); } if (sizeof($images) > 1) { $random_image = array_rand($images, 1); } else { $random_image = 0; } echo "<div class='box'><img src='" . $images[$random_image] . "'/></div>";
You can find the ID for your category by logging into the admin area and going to Catalog > Manage Categories and clicking on a category. You should see the ID come up next to the category name in the editable area.
- All you need to do now is specify where the box is shown. Open a layout file, such as app/design/frontend/yourpackage/yourtheme/layout/catalog.xml and use this layout update:
<reference name="left"><!-- change left to any structural block you want to target, such as right or content. --> <block type="catalog/navigation" name="catalog.box" template="catalog/navigation/box.phtml"/> <!--change the template path to wherever you put the template file --> </reference>
And that’s it! You should now have a block that shows an image from a random product within a specific category. You’ll need to do some styling to get it looking right mind but I hope this helps.
Ben
Creative Lead
MS Internet
Magento – Swap product image on thumbnail click
Mar 6th

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, here’s what you’ll need to do:
- Open app/design/frontend/yourpackage/yourtheme/template/catalog/product/view/media.phtml
- Go down to line 71 and change:
<li> <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> </li>
to
<li> <a href="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()); ?>" title="<?php echo $_product->getName();?>" onclick="$('image').src = this.href; return false;"> <img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(70, 70); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" title="<?php echo $this->htmlEscape($_image->getLabel()) ?>"/> </a> </li>
And that’s it! Your product image should now swap out for whichever thumbnail you click.
Credit to Scott from http://www.magentocommerce.com/boards/viewthread/6402/#t61131
Ben
Creative Lead
MS Internet