Thursday, February 3, 2011

Magento product image switcher



Recently had a client who wants to display the more views thumbnail in the main picture when clicked. Then when the main picture is clicked, the main picture will be displayed via lightbox.
For the Image switcher: (replace the catalog/product/view/media.phtml content with these)
<?php
    $_product = $this->getProduct();
    $_helper = $this->helper('catalog/output');
    $_gallery = $this->getGalleryImages();
    $_resize = 265;
?>
<style type="text/css">
.product-img-box .more-views li.slide-current a{ border:2px solid #aaa; }
.product-img-box .product-image-zoom img { cursor: pointer; }
#slide-loader{ visibility:hidden; position:absolute; top:auto; left:auto; right:2px; bottom:2px; width: 25px; height: 25px; }
</style>
<script type="text/javascript">
function slide(url,num,gallery){
  if (typeof slide.loading == 'undefined') slide.loading = false;
    if(slide.loading) return false;
      var loader = new Image();
      $(loader).observe('load', function(){
         $('slide-loader').setStyle({'visibility':'hidden'});
         $('div.more-views li').each(function(el,i){
           (i==num) ? el.addClassName('slide-current') : el.removeClassName('slide-current');
         });
         var dummy = new Element('img', { src: url }).setOpacity(0);
         new Insertion.After('image', dummy);
         new Effect.Opacity(dummy, { duration:.5, from:0, to:1.0 });
         new Effect.Opacity($('image'), { duration:.5, from:1.0, to:0, 
         afterFinish: function(){
        $('image').writeAttribute('src',url).setOpacity(1).observe('click',function(e){
  Event.stop(e);
  popWin(gallery, 'gallery', 'width=300,height=300,left=50,top=50,location=no,status=yes,scrollbars=yes,resizable=yes'); 
  return false;
;})
dummy.remove();
slide.loading = false;
}
});
});
$('slide-loader').setStyle({'visibility':'visible'});
loader.src=url;
slide.loading = true;
return false;
}
</script>
<p class="product-image-zoom">
<?php
$_img = '<img id="image" src="'.$this->helper('catalog/image')->init($_product, 'image')->resize($_resize).'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'" onclick="popWin(\''.$this->getGalleryUrl().'.\', \'gallery\', \'width=300,height=300,left=50,top=50,location=no,status=yes,scrollbars=yes,resizable=yes\'); return false;" />';
echo $_helper->productAttribute($_product, $_img, 'image')
?>
<img id="slide-loader" src="<?php echo $this->getSkinUrl('images/opc-ajax-loader.gif') ?>" />
</p>
<p class="a-center" id="track_hint"><?php echo $this->__('Click on above image to view full picture') ?></p>
<?php if (count($_gallery) > 0): ?>
<div class="more-views">
<h4><?php echo $this->__('More Views') ?></h4>
<ul>
<?php foreach ($_gallery as $_image): ?>
<li>
<a href="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()); ?>" onclick="slide('<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile())->resize($_resize) ?>',<?php echo ($s = isset($s) ? ++$s : 0) ?>,'<?php echo $this->getGalleryUrl($_image) ?>'); 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>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
One thing to note though. When the main picture is clicked, it will load the image in a popup window which is not beautiful.. I want to add a lightbox effects that will load the main picture when clicked.
================================================================
Update: achieved the thickbox lightbox integration. Refer to this site : www.room9.nl
Here's the same code from above but modified...
<?php
$_product = $this->getProduct();
$_helper = $this->helper('catalog/output');
$_gallery = $this->getGalleryImages();
$_resize = 350;
?>
<style type="text/css">
.product-img-box .more-views li.slide-current a{ border:2px solid #aaa; }
.product-img-box .product-image-zoom img { cursor: pointer; }
#slide-loader{ visibility:hidden; position:absolute; top:auto; left:auto; right:2px; bottom:2px; width: 25px; height: 25px; }
</style>
<script type="text/javascript">
function slide(url,num,gallery){
if (typeof slide.loading == 'undefined') slide.loading = false;
if(slide.loading) return false;
var loader = new Image();
$(loader).observe('load', function(){
$('slide-loader').setStyle({'visibility':'hidden'});
$('div.more-views li').each(function(el,i){
(i==num) ? el.addClassName('slide-current') : el.removeClassName('slide-current');
});
var dummy = new Element('img', { src: url }).setOpacity(0);
new Insertion.After('image', dummy);
new Effect.Opacity(dummy, { duration:.5, from:0, to:1.0 });
new Effect.Opacity($('image'), { duration:.5, from:1.0, to:0, 
afterFinish: function(){
$('image').writeAttribute('src',url).setOpacity(1).observe('click',function(){
$('swapper').href = url;
})
dummy.remove();
slide.loading = false;
}
});
});
$('slide-loader').setStyle({'visibility':'visible'});
loader.src=url;
slide.loading = true;
return false;
}
</script>
<p class="product-image-zoom">
<?php
$_img_a = '<a id="swapper" class="thickbox" rel="group" href="'.$this->helper('catalog/image')->init($_product, 'image').'" title="'.$this->htmlEscape($this->getImageLabel()).'">';
$_img_b = '<img id="image" src="'.$this->helper('catalog/image')->init($_product, 'image').'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'" /></a>';
?>
<?php echo $_helper->productAttribute($_product, $_img_a.$_img_b, 'image') ?>
<img id="slide-loader" src="<?php echo $this->getSkinUrl('images/lightbox/loading.gif') ?>" />
  </p>
<?php if (count($_gallery) > 0): ?>
<div class="more-views">
<h4><?php echo $this->__('More Views') ?></h4>
<ul>
<?php foreach ($_gallery as $_image): ?>
<li>
<a rel="group" href="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()); ?>" onclick="slide('<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()) ?>',<?php echo ($s = isset($s) ? ++$s : 0) ?>,'<?php echo $this->getGalleryUrl($_image) ?>'); return false;"><img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(65); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" title="<?php echo $this->htmlEscape($_image->getLabel()) ?>" /></a>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>

Reference: http://inchoo.net/ecommerce/magento/magento-product-images-switcher/

Tuesday, January 18, 2011

Magento - Auto Store switcher using GEOIP

Prerequisites:
  • GEOIP (geoip.inc, geoip.dat etc)
  • Signup for an account in www.ipinfodb.com
  • Download the php class api HERE

  • 1. put the geoip files (preferably in a folder) in root directory
  • 2. edit the index.php of the root directory and replace these code
  • /* Store or website code */
    $mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
    
    /* Run store or run website */
    $mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
    
    Mage::run($mageRunCode, $mageRunType);
    
  • with these
//########### GEOIP ############//
$geoipPath = 'geoip.inc';
include($geoipPath);

$gi = geoip_open("GeoIP/GeoIP.dat",GEOIP_STANDARD);

$ip = $_SERVER['REMOTE_ADDR'];
$country_code = geoip_country_code_by_addr($gi, $ip);

if(strtoupper($country_code) != "NL"){
    $mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : 'en';
    $mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';

    Mage::run($mageRunCode, $mageRunType);
}else{
    $mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : 'nl';
    $mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';

    Mage::run($mageRunCode, $mageRunType);
}

Thursday, December 2, 2010

Magento - Custom Pagination

custom pagination

Ok so you want to have a different pagination.. that can be arranged.. :)

3 Files to edit:
  • styles.css
  • template/catalog/product/list/toolbar.phtml
  • template/page/html/pager.phtml

toolbar.phtml
<?php if($this->getCollection()->getSize()): ?>
<div class="toolbar">
<div class="pager">

<div class="limiter">
<label><?php echo $this->__('Toon:') ?></label>
<?php foreach ($this->getAvailableLimit() as  $_key=>$_limit): ?>
<a href="<?php echo $this->getLimitUrl($_key) ?>"><?php echo $_limit ?></a>
<?php endforeach; ?>
</div>
<?php echo $this->getPagerHtml() ?>

</div>
</div>


pager.phtml
<?php if($this->getCollection()->getSize()): ?>

<?php if($this->getUseContainer()): ?>
<div class="pager">
<?php endif ?>

<?php if($this->getShowAmounts()): ?>
<p class="amount">
<?php if($this->getLastPageNum()>1): ?>
<?php echo $this->__('Items %s to %s of %s total', $this->getFirstNum(), $this->getLastNum(), $this->getTotalNum()) ?>
<?php else: ?>
<strong><?php echo $this->__('%s Item(s)', $this->getTotalNum()) ?></strong>
<?php endif; ?>
</p>
<?php endif ?>

<?php if($this->getShowPerPage()): ?>
<div class="limiter">
<label><?php echo $this->__('Show') ?></label>
<select onchange="setLocation(this.value)">
<?php foreach ($this->getAvailableLimit() as  $_key=>$_limit): ?>
<option value="<?php echo $this->getLimitUrl($_key) ?>"<?php if($this->isLimitCurrent($_key)): ?> selected="selected"<?php endif ?>>
<?php echo $_limit ?>
</option>
<?php endforeach; ?>
</select> <?php echo $this->__('per page') ?>
</div>
<?php endif ?>

<!-- ############################ PAGER TOOLBAR ############################# -->

<?php if($this->getLastPageNum()>1): ?>
<div class="pages">
<ol>

<!-- ##################### PREVIOUS TOOLBAR ####################### -->
<div class="previous">
<?php if (!$this->isFirstPage()): ?>
<a class="previous-link" href="<?php echo $this->getPreviousPageUrl() ?>" title="<?php echo $this->__('Previous') ?>">
<?php if(!$this->getAnchorTextForPrevious()): ?>
<img src="<?php echo $this->getSkinUrl('images/i_pager-prev.gif') ?>" alt="<?php echo $this->__('Previous') ?>" class="v-middle" />
<?php else: ?>
<?php echo $this->getAnchorTextForPrevious() ?>
<?php endif;?>
</a>
<?php else: ?>
<span class="no-previous">

<?php echo $this->getAnchorTextForPrevious() ?>
</span>
<?php endif;?>
</div>

<?php if ($this->canShowFirst()): ?>
<li><a class="first" href="<?php echo $this->getFirstPageUrl() ?>">1</a></li>
<?php endif;?>

<?php if ($this->getCurrentPage() >= 4): ?>
<li><a class="first" href="<?php echo $this->getFirstPageUrl() ?>">1</a></li>
<li class="ellipse"> ... </li>
<?php endif; ?>

<?php if ($this->canShowPreviousJump()): ?>
<li><a class="previous_jump" title="" href="<?php echo $this->getPreviousJumpUrl() ?>">...</a></li>
<?php endif;?>

<?php foreach ($this->getFramePages() as $_page): ?>
<?php if ($this->isPageCurrent($_page)): ?>
<li class="current"><?php echo $_page ?></li>
<?php else: ?>
<li><a href="<?php echo $this->getPageUrl($_page) ?>"><?php echo $_page ?></a></li>
<?php endif;?>
<?php endforeach;?>


<?php if ($this->canShowNextJump()): ?>
<li><a class="next_jump" title="" href="<?php echo $this->getNextJumpUrl() ?>">...</a></li>
<?php endif;?>

<?php
$lastpage = $this->getLastPageNum();
$lastpage = $lastpage - 2;
?>

<?php if ($this->getCurrentPage() < $lastpage ): ?>
<li class="ellipse"> ... </li>
<li><a class="last" href="<?php echo $this->getLastPageUrl() ?>"><?php echo $this->getLastPageNum() ?></a></li>
<?php endif; ?>

<?php if ($this->canShowLast()): ?>
<li><a class="last" href="<?php echo $this->getLastPageUrl() ?>"><?php echo $this->getLastPageNum() ?></a><li>
<?php endif;?>

<!-- ##################### NEXT TOOLBAR ####################### -->
<div class="next">
<?php if (!$this->isLastPage()): ?>
<a class="next-link" href="<?php echo $this->getNextPageUrl() ?>" title="<?php echo $this->__('Next >') ?>">
<?php if(!$this->getAnchorTextForNext()): ?>
<img src="<?php echo $this->getSkinUrl('images/i_pager-next.gif') ?>" alt="<?php echo $this->__('Next') ?>" class="v-middle" />
<?php else: ?>
<?php echo $this->getAnchorTextForNext() ?>

<?php endif;?>
</a>
<?php else: ?>
<span class="no-next">
<?php echo $this->getAnchorTextForNext() ?>
</span>
<?php endif;?>
</div>
</ol>

</div>

<?php endif; ?>
<?php if($this->getUseContainer()): ?>
</div>
<?php endif ?>
<?php endif ?>
Style it with CSS and you can have a new pagination.. Godbless!

Wednesday, December 1, 2010

Magento - Layered Navigation Custom



If you want your layered navigation to be customized like the image above follow me... :)

We need to edit 3 files..
  • styles.css
  • template/catalog/layer/filter.phtml
  • template/catalog/layer/view.phtml

filter.phtml
<select onchange="setLocation(this.value)" class="layer-nav-homepage">
<option value="" selected="true">

<?php if($GLOBALS['filtername'] == "Category"): ?>
<span>Kunstenaars</span>
<?php else: ?>
<?php echo $GLOBALS['filtername']; ?>
<?php endif; ?>
</option>

<?php foreach ($this->getItems() as $_item): ?>
<?php if ($_item->getCount() > 0): ?>
<option value="<?php echo $this->urlEscape($_item->getUrl()) ?>"><?php echo $_item->getLabel() ?>&nbsp;(<?php echo $_item->getCount() ?>)</option>
<!--<a href="<?php echo $this->urlEscape($_item->getUrl()) ?>"><?php echo $_item->getLabel() ?></a>-->
<?php else: echo $_item->getLabel() ?>
<?php endif; ?>


<?php endforeach ?>
</select>

Remarks:
The list was changed into <select> for dropdown, then a default (no value) option was placed - this will get the name of the filter.. eg. category, price and style..., then the option..


view.phtml
<?php if($this->canShowBlock()): ?>
<div class="block block-layered-nav">
<div class="block-title">
<span><?php echo $this->__('Filter kunst op: ') ?></span>
</div>
<div class="block-content">
<?php echo $this->getStateHtml() ?>
<?php if($this->canShowOptions()): ?>
<!--<p class="block-subtitle"><?php // echo $this->__('Shopping Options') ?></p>-->
<dl id="narrow-by-list">

<?php $_filters = $this->getFilters() ?>
<?php $i=0; ?>
<?php foreach ($_filters as $_filter): ?>
<?php if($_filter->getItemsCount()): ?>

<?php $GLOBALS['filtername'] = $_filter->getName(); ?>

<!--<dt><?php // echo $this->__($_filter->getName()) ?></dt>-->
<dd><?php echo $_filter->getHtml() ?></dd>

<?php if($i==2): ?>
<?php else: ?>
<span> of </span>
<?php endif; $i++; ?>

<?php endif; ?>
<?php endforeach; ?>

</dl>
<script type="text/javascript">decorateDataList('narrow-by-list')</script>
<?php endif; ?>
</div>
</div>
<?php endif; ?>

Remarks:
The global var is for the filter name to be passed on the filter.phtml, then the condition if == 2 is for the OF... :)

Then to be able to put this on the homepage, you should enable isAnchor property of Default category... Also make the style attribute... That's IT! Hope this helps! Godbless!

Magento - Display all products in Homepage with pagination


A client asked to create a Magento shop with Art Gallery like functionality. What she want is to have a product list in the homepage having pagination. Let's get it on!

  • In the backend:
    Manage Categories > set the default category to anchor:yes, then assign all products in this category
  • CMS->homepage > put this snippet in the layout update xml

<!-- ########################## DISPLAY ALL PRODUCTS IN HOMEPAGE ############################ -->
<reference name="content">
<block type="catalog/layer_view" name="catalog.leftnav" before="-" template="catalog/layer/view.phtml"/>
<block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
<block type="page/html_pager" name="product_list_toolbar_pager"/>
</block>
<action method="addColumnCountLayoutDepend"><layout>two_columns_right</layout><count>4</count></action>
<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
</block>
<update handle="page_two_columns_right" />
</reference>

Tuesday, November 23, 2010

Magento - Display Categories (with category images) in the homepage

<div class="top-home-category">
<?php 
/******** Don't know why I commented this
$_helper = $this->helper('catalog/output');
$_category = $this->getCurrentCategory();
$_imgHtml = '';

if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '<p class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /></p>';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
****************************************/
?>

<?php // Iterate all categories in store
 $limit = 0;
$_helper    = $this->helper('catalog/output');
foreach ($this->getStoreCategories() as $_category): 
// If category is Active
if($_category->getIsActive()):
// Load the actual category object for this category
$cur_category = Mage::getModel('catalog/category')->load($_category->getId());
if ($_imgUrl = $cur_category->getImageUrl()){
$_imgHtml = '<img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($cur_category->getName()).'" title="'.$this->htmlEscape($cur_category->getName()).'"  width="105px" />';
$_imgHtml = $_helper->categoryAttribute($cur_category, $_imgHtml, 'image');
} 
?>
<div class="home-category">
<div class="linkimage">
<a href="<?php echo $this->getCategoryUrl($cur_category) ?>" style="border:none">
<?php echo $_imgHtml; ?>
</a>
</div>
<div class="category-link-container">
<a href="<?php echo $this->getCategoryUrl($cur_category) ?>">
<?php echo $_helper->categoryAttribute($cur_category, $cur_category->getName(), 'name') ?>
</a>
</div>
</div>

// Load a random product from this category
/*$products = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($cur_category)->addAttributeToSelect('small_image');
$products->getSelect()->order(new Zend_Db_Expr('RAND()'))->limit(1);
$products->load();
// This a bit of a fudge - there's only one element in the collection
$_product = null;
foreach ( $products as $_product ) {}
?>
<div class="home-category">
<div class="linkimage"><p><a href="<?php echo $this->getCategoryUrl($_category)?>">
<?php
if(isset($_product)):
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 135); ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
<?php
endif;
?> </a></p>
</div>
<a href="<?php echo $this->getCategoryUrl($_category)?>"><?php echo $_category->getName()?></a>
</div>
*/?> <?php
$limit+=1;
endif;
if ($limit==8):
break;
endif;
endforeach;
?> 
</div>

Monday, November 22, 2010

Magento - Adding breadcrumbs to Homepage


1. Register the new module (we will call the module CMS and give the development company the name Acme):

In your magento\app\etc\modules directory create a file named Acme_All.xml with the following content:-

<?xml version="1.0"?>
<config>
  <modules>
    <Acme_Cms>
      <active>true</active>
      <codePool>local</codePool>
    </Acme_Cms>
  </modules>
</config>

2.  Register the module (which will be implemented in the class
Acme_Cms_Block_Page) as being responsible for rewrite-ing the cms/page
block

In a magento\app\code\local\Acme\Cms\etc directory (you will need to
create it) create a config.xml file with the following content:-

<?xml version="1.0"?>
<config>
  <global>
    <blocks>
      <cms>
        <rewrite>
          <page>Acme_Cms_Block_Page</page>
        </rewrite>
      </cms>
    </blocks>
  </global>
</config>

3. Change the original block logic (based on what is in app\code\core\Mage\Cms\Block\Page.php) to handle our CMS home page.

In a magento\app\code\local\Acme\Cms\Block directory create a file named Page.php with the content:-
<?php class Acme_Cms_Block_Page extends Mage_Cms_Block_Page{
protected function _prepareLayout(){
  $page=$this->getPage();
  //show breadcrumbs
  if(Mage::getStoreConfig('web/default/show_cms_breadcrumbs') && ($breadcrumbs=$this->getLayout()->getBlock('breadcrumbs'))
&& ($page->getIdentifier()!==Mage::getStoreConfig('web/default/cms_no_route'))){
  $breadcrumbs->addCrumb('home',array('label'=>Mage::helper('cms')->__('Home'),'title'=>Mage::helper('cms')->__('Go to Home Page'),'link'=>Mage::getBaseUrl()));
  if ($page->getIdentifier()!==Mage::getStoreConfig('web/default/cms_home_page')){
    $breadcrumbs->addCrumb('cms_page', array('label'=>$page->getTitle(), 'title'=>$page->getTitle()));
  }
}
  if($root=$this->getLayout()->getBlock('root')){
    $root->addBodyClass('cms-'.$page->getIdentifier());
  }
  if($head=$this->getLayout()->getBlock('head')){
    $head->setTitle($page->getTitle());
    $head->setKeywords($page->getMetaKeywords());
    $head->setDescription($page->getMetaDescription());
  }
}
}
Be sure that you have enabled breadcrumbs for CMS pages
[Config->Web->Default Pages->Show breadcrumbs for CMS pages =
Yes]


Reference: http://www.magentocommerce.com/boards/viewthread/49743/