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
});

Saturday, 26 January 2013

Programmatically add attributes's option in magento



$arg_attribute = '<attribute-code>';
$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
$attr_id = $attr->getAttributeId();
$opt['attribute_id'] = $attr_id;
$opt['value']['<admin-value>'][0] = '<admin-name>'; //admin name of option
$opt['value']['<admin-value>']][1] = '<admin-name>'; //default store name of option

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($opt);

Programmatically create a category within another category in magento


//$parentId is the category id of a category under which you want to want add new category.


$category = Mage::getModel('catalog/category');
$category->setName('name of new category')
->setIsActive(1)                       //activate your category
->setDisplayMode('PRODUCTS')
->setIsAnchor(1)
->setCustomDesignApply(1)
->setAttributeSetId($category->getDefaultAttributeSetId());

$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$category->setPath($parentCategory->getPath());

$category->save();
unset($category);


That's all.. Enjoy.. :)