Sunday, 24 February 2013

Add fields in newsletter in magento

1- Create app/code/local/[package-name]/[module-name]/sql/mysql4-install-0.1.0.php and now the following code:-


$installer = $this;

$installer->startSetup();

$installer->run("
 ALTER TABLE {$installer->getTable('newsletter_subscriber')}
 ADD (`lastname` TEXT NULL,
 `firstname` TEXT NULL)
 ");

$installer->endSetup();

2- Create app/code/local/[package-name]/[module-name]/controllers/[controller-name].php(like:- IndexController.php)

Code to add first name and last name -



require_once 'Mage/Newsletter/controllers/SubscriberController.php';
class Packagename_Modulename_IndexController extends Mage_Newsletter_SubscriberController
{
    /**
      * New subscription action
      */
    public function newAction()
    {.
     .
     .
     .
                 $status = Mage::getModel('newsletter/subscriber')->subscribe($email);

/*custom fields*/
if ($this->getRequest()->getPost('firstname') || $this->getRequest()->getPost('lastname'))
                {
                     $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
                     $firstname     = (string) $this->getRequest()->getPost('firstname');
    $lastname     = (string) $this->getRequest()->getPost('lastname');
                     $subscriber->setFirstname($firstname)->setLastname($lastname);
                     $subscriber->save();
                }
/*custom fields*/
         .
         .
    }


3- Code to see the information on Admin-

    Go to this link : app\code\core\Mage\Adminhtml\Block\Newsletter\Subscriber\grid.php

    Add this code :

    $this->addColumn('subscriber_firstname', array(
            'header'    => Mage::helper('newsletter')->__('Subscriber First Name'),
            'index'     => 'subscriber_firstname',
            'default'   =>    '----'
        ));

and you are done...

Set default option on configurable products in magento


Open app\design\frontend\[your package]\[your theme]\template\catalog\product\view\type\options\configurable.phtml

Now add the below java-script code after :-
var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);


function fireEvent(element,event){
if (document.createEventObject){
// dispatch for IE
var evt = document.createEventObject();
return element.fireEvent('on'+event,evt)
}
else{
// dispatch for firefox + others
var evt = document.createEvent("HTMLEvents");
evt.initEvent(event, true, true ); // event type,bubbling,cancelable
return !element.dispatchEvent(evt);
}
}
Event.observe(window, 'load', function() {
spConfig.settings[0].selectedIndex = 1;
obj = spConfig.settings[0]; // this grabs the first select item
Event.observe(obj,'change',function(){});
fireEvent(obj,'change'); // this simulates selecting the first option, which triggers
spConfig.settings[1].selectedIndex = 1; // this selects the first option of the second attribute drop menu
});