11/26/2010

How to change value of input text by jquery

I was searching in the internet for this solution ,But I couldn't find the proper solution.But I tried it myself so I think to share with all.

First i have created one input text with id inputText by this html code .
<input type="text" id="inputText" value="1" />


Now I wrote the following code in jquery to change value 1 to 5

$(document).ready(function(){
$('#inputText').val('5');
}

11/25/2010

How to show add Success message and add Error message in Magento

This is one of the easiest way to show Either Error message or Success message in magento for one time.If you are thinking to show an error message or success message one time when a page load then it is the best method to use in mganeto.

Show Error Message in frontend

$message = $this->__('Email Id Already Exist.');
Mage::getSingleton('core/session')->addError($message);

Show Success message in frontend

$message = $this->__('You have Register Successfully');
Mage::getSingleton('core/session')->addSuccess($message);

If you wish show Success message or Error Message in Admin html then you need to write your code little bit different

Show Error Message in Admin page

$message = $this->__('1 row has been updated successfully');
Mage::getSingleton('adminhtml/session')->addSuccess($message);

Show Success Message in Admin page

$message = $this->__('Email Id Already Exist.');
Mage::getSingleton(adminhtml/session')->addError($message);

11/22/2010

How to check if an user is logged in or log out in Magento

This is Magento default feature. In top.link you can find login or logout link.But if you want to put somewhere login/logout button then you need to check whether a user logged in or not by the code given below.

<?php
if ($this->helper('customer')->isLoggedIn() )
{
echo '<a href="<?php echo $this->getUrl('customer/account/login/');?>">Login</a>';
}
else
{
echo '<a href="<?php echo $this->getUrl('customer/account/logout/');?>">Logout</a>';
}
?>

How to overwrite cart controller add Action index Action in Magento

The Following Tutorial will Shows How to override cart controller Add Action or Index Action

First you need to give your Module name and Name_space ,I have taken Anjan as Namespace and override as Modulename
Now Create your module folders and files like below Instruction

1. Magento/app/code/local/Anjan/Override/etc/config.xml
2. Magento/app/code/local/Anjan/Override/controllers/CartController.php
3. Magento/app/etc/modules/Anjan_Override.xml

In config.xml Write the Foloowing code

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Anjan_Override> <!--Change Anjan To Your Module NameSpace and Override to Your Module name-->
<version>0.1.0</version>
</Anjan_Override>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<Anjan_Override before="Mage_Checkout">Anjan_Override</Anjan_Override><!--Change Anjan To Your Module NameSpace and Override to Your Module name-->
</modules>
</args>
</checkout>
</routers>
</frontend>
</config>

In CartController.php write the following code

<?php
require_once 'Mage/Checkout/controllers/CartController.php';
class Anjan_Override_CartController extends Mage_Checkout_CartController
{
public function addAction()
{
echo 'I successfully Override Cart Controller';
parent::addAction();
}
public function indexAction()
{
echo 'I successfully Override Cart Controller';
parent::addAction();
}
}
?>

In Anjan_Override.xml write the following code

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Anjan_Override>
<active>true</active>
<codePool>local</codePool>
</Anjan_Override>
</modules>
</config>

Now upload these files into your server,refresh your cache , the click on Add to cart button you can see the message is displaying . You can use die() statement to make sure whether is overrides or not .

How to Get Product details Using Product Id in Magento

If you got the product id then you can get details of a product by using the following method in magento.

$obj = Mage::getModel('catalog/product');
$_product = $obj->load($product_id); // Enter your Product Id in $product_id
// get Product's name
echo $_product->getName();
//get product's short description
echo $_product->getShortDescription();
//get Product's Long Description
echo $_product->getDescription();
//get Product's Regular Price
echo $_product->getPrice();
//get Product's Special price
echo $_product->getSpecialPrice();
//get Product's Url
echo $_product->getProductUrl();
//get Product's image Url
echo $_product->getImageUrl();

11/18/2010

How to Make http to https in magento login page register page checkout page and admin panel

Https is an important part in magento as customer or user gain trust and it increase web site's search engines rank also. To make your login page, register page, checkout page and admin panel as https you need to install SSL certificate into your server first.After Successful installation do the following process .
Go to System->Config->Web
then Click on Secure tab.
Change your base url to https://www.yoursite.com [ Important : Write HTTPS in your base url otherwise it will not work]
Make Use Secure URLs in Frontend to Yes
Make Use Secure URLs in Admin to Yes.

Seethe Below screenshot
Make http to https in magento login page register page checkout page and admin panel
Then Save config Clear your cache, Now go to your frontend then click on login page you can see that your login page is changed to https.

How to Read all files name in a folder using php

Write the below code to read filse name from an particular folder.

function dirFiles($directry) {
$dir = dir($directry); //Open Directory
while (false!== ($file = $dir->read())) //Reads Directory
{
$extension = substr($file, strrpos($file, '.')); // Gets the File Extension
if($extension == ".xml" || $extension == ".csv" || $extension == ".php" |$extension == ".txt") // Extensions Allowed
$filesall[$file] = $file; // Store in Array
}
$dir->close(); // Close Directory
asort($filesall); // Sorts the Array
return $filesall;
}

$array = dirFiles('/home/your absolute directory/');

foreach ($array as $key => $file)
{
echo $file; // Display Images
echo '<br />';
}

How to Reset all Folder and File permission in magento

There is two way to Reset all Folder and File permission in magento.

1. By using SSH

a. find . -type f -exec chmod 644 {} \;
b. find . -type d -exec chmod 755 {} \;
c. chmod 550 pear

2. By Using Magneto clean Up Tool
a.Download the magento clean up tool .
b.Unzip magento-cleanup.php to the root directory where Magento is installed.
c.Browse to http://yourdomain.com/magento-cleanup.php

11/17/2010

How to Change admin default url in magento

It's very important to change default admin url in magento to save your website from hackers. For this you need to do little bit changes in your site.

1. Go to app/etc folder of your magento website and open local.xml
<admin>
<routers>
<adminhtml>
<args>
<frontName><![CDATA[admin]]></frontName>
</args>
</adminhtml>
</routers>
</admin>
Instead of admin Write your new url (e.g: magAdmin).

2. Now go to your system cahce which located under var folder. Rename or delete the Cache folder.Clear all session. Now It's done. Visit the site admin by typing the new admin path.

11/16/2010

How to change the Magento Email Templates

app\locale\en_US\template\email\ is the actual path where you can find all Email templates of magento. There are 20 html files .Edit Html file which you want,