<?php Magento Home URL - Mage::getBaseUrl(); Magento Home Directory - Mage::getBaseDir(); URL of a file in the skin directory - $this->getSkinUrl('images/myfile.png'); // in a template or Block - Mage::getDesign()->getSkinUrl('images/myfile.png'); // from anywhere Format a price value - Mage::helper('core')->formatPrice($amount);
Some useful and important code in magento
Some useful and important code in magento :-
Labels:
Magento
How to insert,update,delete and show data in Magento
Suppose, I have a module named mynews. Here follows the code to select, insert, update, and delete data from the news table
1. INSERT DATA
$data contains array of data to be inserted. The key of the array should be the database table’s field name and the value should be the value to be inserted.
1. INSERT DATA
$data contains array of data to be inserted. The key of the array should be the database table’s field name and the value should be the value to be inserted.
<?php $data = array('title'=>'hello there','content'=>'how are you? i am fine over here.','status'=>1); $model = Mage::getModel('mynews/mynews')->setData($data); try { $insertId = $model->save()->getId(); echo "Data successfully inserted. Insert ID: ".$insertId; } catch (Exception $e){ echo $e->getMessage(); } ?>
Labels:
Magento
How to insert your block into any place in Magento
Hello guys, it’s time to tell you how to insert block into any place you want in Magento. As you know, very often we need to set some block into selected weird place without editing template, so we’ll try to describe the way how to do this using layouts and observers.In our case, for such experiments will be used category page. Let’s say, you need to place your block inside the other one and this block is instance of the Mage_Core_Block_Text_List class. That means, the block renders his children automatically, what’s easy. Then, you just need to follow the steps which are below:
This block will be displayed after all content of the “left“ block. But, what if we need it to be displayed before all content – just add the “before” directive. See below such example:
Labels:
Magento
Important operatinol functions of Magento
1.How to get the category url in magento
System => Configuration => Catalog => Search Engine Optimization and empty the fields "Product URL Suffix" and "Category URL Suffix"
3.How to get category’s Thumbnail image in magento
<?php echo Mage::getModel('catalog/category')->load($catId)->getUrl() ?>2.How to remove .html at the end of the url in magento
System => Configuration => Catalog => Search Engine Optimization and empty the fields "Product URL Suffix" and "Category URL Suffix"
3.How to get category’s Thumbnail image in magento
<?php $thumbnail=Mage::getModel('catalog/category')->load($categoryId)->getThumbnail(); //$category_Id -category id ,$thumbnail – image name if($thumbnail !=''){ $imgPath = Mage::getBaseUrl('media').'catalog/category/' // Get category image path echo ''; ?>4.How to get Customer Details By Session
<?php $customer = Mage::getSingleton('customer/session')->getCustomer(); print_r($customer);?>4.1How to get Customer Details By E-Mail
<?php $customer = Mage::getModel('customer/customer')->loadByEmail(example@mail.com); print_r($customer); ?>5.How to delete product by sku :-
<?php $product = Mage::getModel('catalog/product'); $productId = intval($product2->getIdBySku('putSkuHere'); $product->load($productId)->delete(); ?>6.How to get module table :-
<?php $resource = Mage::getSingleton('core/resource'); $tableName = $resource->getTableName('mymodule/mymodel') ?>
Labels:
Magento
how to upload any file in magento
Step 1 :- cretae html form for upload file
Step 2 :- Write Script for upload file
<?php if(isset($_FILES['myfilename']['name']) && $_FILES['myfilename']['name'] != '') { try { $path = Mage::getBaseDir('media') . DS . 'csvfile' . DS; //desitnation directory $fname = $_FILES['myfilename']['name']; //file name $filpath = $path.$fname; $uploader = new Varien_File_Uploader('myfilename'); //load class $uploader->setAllowedExtensions(array('xls','xlsx','csv')); //Allowed extension for file $uploader->setAllowCreateFolders(true); //for creating the directory if not exists $uploader->setAllowRenameFiles(false); //if true, uploaded file's name will be changed, if file with the same name already exists directory. $uploader->setFilesDispersion(false); $uploader->save($path,$fname); //save the file on the specified path } catch (Exception $e) { echo 'Error Message: '.$e->getMessage(); } } ?>
Labels:
Magento
Subscribe to:
Posts (Atom)