Sunday 25 November 2012

Avoid Document Expire while hitting browser back





Using  Get Post Get rule you can avoid this.
  • Let say I have example1.php example2.php & example3.php
  • I am posting some value from example1.php to example2.php then i did all the DB stuff as per my need and rendered the page (Not Redirected - Just posted and the page got rendered).
  • After submitting values from example2.php to sometest.php and I have redirected the page to example3.php. Now if you click browser back the Document will Expire.
    • To Avoid this we can post the values from example1.php to some other PHP and then we need to Redirect the user to example2.php
    • This is the GPG rule which every developer needs to follow to avoid Document expire and also to avoid redundant entry in DB on refreshing the page.

Thursday 30 August 2012

Magento DB locked while saving product

If you facing db lock while saving your product don't worry just you want to do the below.

Check the problem is due to your local module queries. If you have written any observer while before save or on after save.

If not take a backup of below listed tables and and truncate. Surely it will solve your problem i did the same and its worked for me.
 

truncate table sales_flat_quote;
truncate table sales_flat_quote_address;
truncate table sales_flat_quote_address_item;
truncate table sales_flat_quote_item;
truncate table sales_flat_quote_item_option;
truncate table sales_flat_quote_payment;
truncate table sales_flat_quote_shipping_rate;



Hip Hip Hurrey .............

Enjoy coding in magento............

Monday 5 March 2012

SMTP Mail configuration Magento

1. You need to override Mage_Core_Model_Email_Template [ app/code/core/Mage/Core/Model/Email/Template.php need to be override].
2. Create a Module in your app/code/local/Company/Core
3. Create a config.xml in app/code/local/Company/Core/etc, copy and paste the below code in that file.

<?xml version="1.0"?>
<config>
        <modules>
        <Companyname_Core>
            <version>0.1.0</version>
        </Companyname_Core>
    </modules>
    <global>
        <models>
            <core>
                <rewrite>                   
                    <email_template>Companyname_Core_Model_Email_Template</email_template>
                </rewrite>
            </core>
        </models>
    </global>
</config>

3. Create a Template.php in app/code/local/Company/Core/Model/Email/, copy and paste the below code in that file.

<?php
class Companyname_Core_Model_Email_Template extends Mage_Core_Model_Email_Template
{
    public function send($email, $name=null, array $variables = array())
    {
   
        if (!$this->isValidForSend()) {
            Mage::logException(new Exception('This letter cannot be sent.')); // translation is intentionally omitted
            return false;
        }

        $emails = array_values((array)$email);
        $names = is_array($name) ? $name : (array)$name;
        $names = array_values($names);
        foreach ($emails as $key => $email) {
            if (!isset($names[$key])) {
                $names[$key] = substr($email, 0, strpos($email, '@'));
            }
        }
       
        $variables['email'] = $email;
        $variables['name'] = $name;
       
        ini_set('SMTP', Mage::getStoreConfig('system/smtp_settings/host'));
        ini_set('smtp_port', Mage::getStoreConfig('system/smtp_settings/port'));
       
        $host = Mage::getStoreConfig('system/smtp_settings/host');
        $smtpuser = Mage::getStoreConfig('system/smtp_settings/username');
        $smtpuserpass = Mage::getStoreConfig('system/smtp_settings/password');
        $port = Mage::getStoreConfig('system/smtp_settings/port');
        $auth = strtolower(Mage::getStoreConfig('system/smtp_settings/auth'));
        $mail = $this->getMail();
       
        $setReturnPath = Mage::getStoreConfig(self::XML_PATH_SENDING_SET_RETURN_PATH);
        switch ($setReturnPath) {
            case 1:
                $returnPathEmail = $this->getSenderEmail();
                break;
            case 2:
                $returnPathEmail = Mage::getStoreConfig(self::XML_PATH_SENDING_RETURN_PATH_EMAIL);
                break;
            default:
                $returnPathEmail = null;
                break;
        }
       
        if ($returnPathEmail !== null) {
            $mailTransport = new Zend_Mail_Transport_Sendmail("-f".$returnPathEmail);
            Zend_Mail::setDefaultTransport($mailTransport);
        }
       
        foreach ($emails as $key => $email) {
            $mail->addTo($email, '=?utf-8?B?' . base64_encode($names[$key]) . '?=');
        }
       
        $this->setUseAbsoluteLinks(true);
        $text = $this->getProcessedTemplate($variables, true);
       
        if($this->isPlain()) {
            $mail->setBodyText($text);
        } else {
            $mail->setBodyHTML($text);
        }
        $config = array('port' =>$port);
        if($auth != 'NONE'){
            $config = array('port' =>$port, 'auth' => $auth, 'username' => $smtpuser, 'password' => $smtpuserpass);
        }
        $transport = new Zend_Mail_Transport_Smtp($host, $config);
       
        $mail->setSubject('=?utf-8?B?'.base64_encode($this->getProcessedTemplateSubject($variables)).'?=');
        $mail->setFrom($this->getSenderEmail(), $this->getSenderName());
       
        try {
            $mail->send($transport); // Zend_Mail warning.. // Note. here the $transport parameter contains the configuration for smtp authentication
            $this->_mail = null;
        }
        catch (Exception $e) {
            return false;
        }
        return true;
    }
}

Thats all now you can enjoy sending mail with smtp in magento................ :)