Friday 10 June 2011

Magento addAttributeToFilter

Get all product collection with the below code:
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('*');
 
Add different types of filters  on collection. example shown below
//  Equal To (eq)
$collection->addAttributeToFilter('status', array('eq' => 1));

//  Not Equal To (neq)
$collection->addAttributeToFilter('visibility', array('neq' => 1));

// Greater Than (gt)
$collection->addAttributeToFilter('price', array('gt' => 3.99));

// Less Than (lt)
$collection->addAttributeToFilter('price', array('lt' => 3.99));

// Greater Than or Equal To (gteq)
$collection->addAttributeToFilter('price', array('gteq' => 4.99));

// Less Than or Equal To (lteq)
$collection->addAttributeToFilter('price', array('lteq' => 4.99));

// Contains (like) - also uses % wildcards
$collection->addAttributeToFilter('sku', array('like' => 'DVD%'));

// Does Not Contain (nlike) - also uses % wildcards
$collection->addAttributeToFilter('sku', array('nlike' => 'ABC%'));

// In Array (in)
$collection->addAttributeToFilter('id', array('in' => array(1,3,12)));

// Not In Array (nin)
$collection->addAttributeToFilter('id', array('nin' => array(1,2,12)));

// Is NULL (null)
$collection->addAttributeToFilter('description', 'null');

// Is Not NULL (notnull)
$collection->addAttributeToFilter('description', 'notnull');

Thursday 9 June 2011

Magento Product Thumbnail Image Switcher

Step:1

go to file media.phtml
'app/design/frontend/default/default/template/catalog/product/view/media.phtml'


step:2
Find the code below
<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;">
  </a>
Replace it with
<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;">
   <!--nested img tag stays the same-->
</a>