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>

Saturday, 4 June 2011

getting all top and sub categories in magento


place this code in you top.phtml (\app\design\frontend\base\default\template\catalog\navigation)


<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<div id="navMainWrap">
<?php if (count($_categories) > 0): ?>
<ul id="navMain"><?php foreach($_categories as $_category): ?>
<li class="navLevel1" id="navJewelry">
<a class="navLevel1Link" href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<div class="navSubWrap">
            <div class="navSub clearFix">
                <ul class="col1 clearFix">
<li><p class="colHead">
Shop by Category</p>
</li>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>           </ul>
<div class="navSubBnr">
                    <!--<img>-->
</div>
</div>
</div>
<?php endif; ?>
</li>
<?php endforeach; ?>   </ul>
<?php endif; ?>
</div>

Wednesday, 9 March 2011

Add a CMS Static Block to a page with php in Magento

you can use this code to display content of any cms page Just in place of 'identifier' give the name of the page your want to display.
<?php echo $this->getLayout()->
createBlock('cms/block')->setBlockId('identifier')->toHtml() ?>

Saturday, 5 March 2011

get collection of customer all address in magento


Use the following code to retrieve all address of customer with the help of its email id.


<?php
$websiteid = Mage::app()->getwebsite->getid();
$store = Mage::app()->getStore();

$customer = Mage::getModel("customer/customer");
$customer->website_id = $websiteid;
$customer->setStore($store);
$customer->loadByEmail("dexte@yahoo.com");
$address = Mage::getModel("Customer/Entity_Address_Collection");
$address->setCustomerFilter($customer);
$addtess->load();
?>

Thursday, 3 March 2011

display product in magento with sql in list.phtml file

Place this following code in your  phtml file where you want to display particular category products this will return array of products and you can use this array to display product of particular category.
$sql = “SELECT product_id  FROM catalog_category_product WHERE category_id=57″;
$data = Mage::getSingleton(‘core/resource’) ->getConnection(‘core_read’)->fetchAll($sql);

Happy Coding...