5/17/2011

How to send Email in magento

Any body can send mail using php mail() function.But in magento all the functionality has wriiten you need to just send argument inside those functions. See the following code to send mail using magento function

<?php
$mail = Mage::getModel('core/email');
$mail->setToName('Your Name');
$mail->setToEmail('Youe Email');
$mail->setBody('Mail Text / Mail Content');
$mail->setSubject('Mail Subject');
$mail->setFromEmail('Sender Mail Id');
$mail->setFromName("Msg to Show on Subject");
$mail->setType('html');// YOu can use Html or text as Mail format

try {
$mail->send();
Mage::getSingleton('core/session')->addSuccess('Your request has been sent');
$this->_redirect('');
}
catch (Exception $e) {
Mage::getSingleton('core/session')->addError('Unable to send.');
$this->_redirect('');
}
?>

How to get all list of country in magento

Write the following code to any phtml file or template file you will get list of all the Country

<select name="country" id="country">
<?php $country_list = Mage::getResourceModel('directory/country_collection')
->loadData()
->toOptionArray(false) ?>
<?php if (count($country_list) > 0){ ?>
<option value="">Please Select</option>
<?php foreach($country_list as $country){ ?>
<option value="<?php echo $country['value'] ?>">
<?php echo $country['label'] ?>
</option>
<?php } ?>
<?php }?>
</select>

How to get category or subcategory postioned by the admin panel in magento

When we load a category it shows his subcategory according to the product id. If you wish to show category according to the order which you have in you admin panel then you have to fetch your category with little bit different style.See the below code in which I have fetched all subcategory with proper order

<?php
$obj= new Mage_Catalog_Block_Navigation();
$category = $obj->getStoreCategories();
foreach($category as $_category)
{
$catid = $_category->getId();
$parentCategory = Mage::getModel('catalog/category')->load($catid);
$childrenIdArray = explode(',',$parentCategory->getChildren());
if ($key = array_search($parentCategory->getId(),$childrenIdArray)) {
unset($childrenIdArray[$key]);
}
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('entity_id', array('in' => $childrenIdArray))
->addAttributeToSelect('*');

$sortedcategory = $collection->addOrderField('position')->getData();
foreach($sortedcategory as $sorted)
{
$_subcat = Mage::getModel('catalog/category')->load($sorted['entity_id']);
$subcatname = $_subcat->getName();
echo "<h3>".$subcatname."</h3>";
}
}
?>

5/04/2011

How to get all gallery image with resize of a product in magento

In product page you can get gallery image of the current product, But if you think to fetch all gallery image of a product in other page like cms page, then you have to write the code with little bit modification. The code should looks like below

<?php
$obj = new Mage_Catalog_Block_Product_View_Media();
$_product = new Mage_Catalog_Model_Product();
// Load all product information of a particular product
$Products_one = Mage::getModel('catalog/product')->load($id);
// Use your Product Id instead of $id
$countt = count($Products_one->getMediaGalleryImages());
if($countt>0){
foreach ($Products_one->getMediaGalleryImages() as $_image)
{
// For the Original Image
echo "<img src=".Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).str_replace(Mage::getBaseUrl('media'),"",$_image->url)." alt='' />";
//For gallery Image
$resizeimage = $obj->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->backgroundColor(242,242,243)->resize(400,300);
echo "<img src=".$resizeimage."alt='' />";
}
}

5/03/2011

CSS Hack for Google Chrome and Safari

If you are developing any site or designing any website, having design problem in chrome and safari you can write the below css, It only works on Google chrome and Safari Browser

@media screen and (-webkit-min-device-pixel-ratio:0)
{
.classname{ background:#ccc; } /*It will works on Google chrome and safari */
}