Wednesday, January 12, 2011

Useful Codes in magento

Redirection in Magento Controllers.

Mage::app()->getResponse()->setRedirect(Mage::getUrl('customer/account/login')); 

How to run an custom query in magento?



$sql = "SELECT * FROM your_table"; 
$getData = Mage::getSingleton('core/resource') ->getConnection('core_read')->fetchAll($sql); 
 
foreach($getData as $row) { 
    echo $row['name'] . "\n"; 
} 
 
// fetch write database connection that is used in Mage_Core module 
$write = Mage::getSingleton('core/resource')->getConnection('core_write'); 
// now $write is an instance of Zend_Db_Adapter_Abstract 
$write->query("insert into yourtablename values ('val1','val2')");
 
//or you can do it like this 
$write = Mage::getSingleton('core/resource')->getConnection('core_write'); 
$sql  = "insert into yourtablename values (?, ?)"; 
$write->query($sql, array('val1','val2'));
 


Get Customer Data



$customerData = Mage::helper('customer')->getCustomer()->getData());

Tuesday, January 4, 2011

Some Code Samples In Magento

Get CMS Page ID
$pageId = Mage::getBlockSingleton('cms/page')->getPage()->getIdentifier();

How to get product attributes?
<?php echo $_product->getResource()->getAttribute('attribute name')->getFrontend()->getValue($_product) ?>

How to show breadcrumbs in magento?
<?php echo $this->getLayout()->getBlock("breadcrumbs")->toHtml(); ?>

How to show Static Blocks?
<?php /* $this->block_code2call, static block code name - the one added under CMS > Static Blocks */ $_block = Mage::getModel('cms/block') ->setStoreId(Mage::app()->getStore()->getId()) ->load($this->getLayout()->createBlock('cms/block')->setBlockId($this->block_code2call)->getBlockId()); ?>

Get current category
Mage::getModel('catalog/layer')->getCurrentCategory()->getName()