Wednesday, July 27, 2011

How to crop images in magento?

Here is the easiest way to crop image in Magento.
Here goes the code:
$mainImage = Mage::getBaseDir('media') . DS . 'test' . DS . 'sample.jpg';
$image = new Varien_Image($mainImage);
// crop image
$image->crop(10, 10, 10, 10); // crop(top, left, $right, $bottom)
$image->save(Mage::getBaseDir('media'). DS . 'test' . DS . 'new.jpg');
Code Explanation:
In the code above, you can see that I have an image named sample.jpg inside media/test/ directory.

At first, I have cropped the image by 10px from top, left, bottom, and right side.
The new cropped image is saved inside media/test/ directory as new.jpg.

How to remove index.php from URL magento?

If you are using Magento and in your shop URL, if you see “index.php” and wanted to remove it then here is the solution.

- Login to Magento admin panel
- Go to System -> Configuration -> Web -> Search Engines Optimization -> Use Web Server Rewrites
- Select ‘Yes’ from the selection list and Save.
- Goto your site root folder and you can find the .htaccess file. Just edit that. Open it on a text editor and find this line, #Rewrite Base /magento/ . Just replace it with Rewrite Base / .
- Then goto your Cache Management page ( System > Cache Management) and refresh your Cache and also refresh the Web Redirects.

And, you are done. Now, index.php is gone from your Magento shop URL.

Tuesday, July 26, 2011

How to change the default welcome message in magento?

Here is the easier way to change the Default welcome message that appears in header of Magento shop.
When the user is not logged in, by default the message appears as ‘Default welcome msg!‘.

To change the message text, follow steps below:-

- Go to
System -> Configuration -> GENERAL -> Design -> Header -> Welcome Text = YOUR WELCOME TEXT

That’s all. Now, the default welcome message is changed with your custom welcome message.

How to enable maintenance or offline mode in magento?

For Magento version 1.4 and above, You just need to create a file named maintenance.flag in your Magento root. Then, your website automatically goes into maintenance mode. To make the site live again, you can just delete the newly created maintenance.flag file.

You can edit maintenance mode template file. It is present in "errors/default/503.phtml"

For Magento version 1.3 and below,
- create a file called index.html in Magento root and write your maintenance messege in it
- write the following code in the beginning of index.php

// replace with your development IP
if ($_SERVER['REMOTE_ADDR']!=='192.168.2.4') {
  header("Location: /index.html");
  exit;
}

How to send email easily in magento?

Here is a quick code to send email in Magento using the Zend_Mail class.
/**
 * Send email
 */
public function sendEmail()
{
    $fromEmail = "from@example.com"; // sender email address
    $fromName = "John Doe"; // sender name
 
    $toEmail = "to@example.com"; // recipient email address
    $toName = "Mark Doe"; // recipient name
 
    $body = "This is Test Email!"; // body text
    $subject = "Test Subject"; // subject text
 
    $mail = new Zend_Mail();       
    $mail->setBodyText($body);
    $mail->setFrom($fromEmail, $fromName);
    $mail->addTo($toEmail, $toName);
    $mail->setSubject($subject);
 
    try {
        $mail->send();
    }
    catch(Exception $ex) {
        // If you are using this code in your custom module use 'yourmodule' name.
        // If not, you may keep 'customer' instead of 'yourmodule'.
        Mage::getSingleton('core/session')
            ->addError(Mage::helper('yourmodule')
            ->__('Unable to send email.'));
    }
}

Magento: How to get visitor or guest information and ip address?

//Get Visitor's information
  $visitorData = Mage::getSingleton('core/session')->getVisitorData();
 
  // printing visitor information data
  echo "
"; print_r($visitorData); echo "
"; // user's ip address (visitor's ip address) $remoteAddr = Mage::helper('core/http')->getRemoteAddr(true); // server's ip address (where the current script is) $serverAddr = Mage::helper('core/http')->getServerAddr(true);

Magento error "There are no products matching the selection"

After adding products I had a problem. The home page shows "There are no products matching the selection". So I did the following steps and it worked for me.

Solution:

Please check following settings are done for your product

1. The products must be Visible in Catalog.
2. The products must be Enabled.
3. Product must have a stock Quantity.
4. The product must be set to In Stock.
5. If the product is set not to track stock, it still need to have a stock Quantity and be set to In Stock.
6. The product must be assigned to the target Category.
7. If using multi-website mode (or if you imported the products through Data Flow), the products must be assigned to the target Website.
8. You must refresh your “var/Cache” & rebuild all indexes from admin > system > index management

Hope this helps you.