3/13/2014

How to add custom variables to new order E-mail in magento?

You can add custom variable data or something related to product's attribute in any transactional email. You also can write your custom code according to the current order id and display the required result. If you want to use new module, you can create hope for creating new module I don't need to describe the steps here.

open your layout file.xml then write below xml there

<sales_email_order_anything>
<block type="sales/order_email_items" name="anything" template="email/anyname.phtml"/>
</sales_email_order_anything>

In anyname.phtml file you can write <?php $_order = $this->getOrder(); ?> to get the order object.

Now go to order transactional email then write

{{layout handle="sales_email_order_anything" order=$order}}

Replace all anything/anyname to suitable text

12/06/2013

How to use WYSIWYG editor TinyMCE in custom Admin Magento Module

It's so easy to enable by the below 3 steps
1) Go to Yournamespace_Modulename_Block_Adminhtml_Modulename_Edit
then copy and paste the below code
protected function _prepareLayout() {
parent::_prepareLayout();
if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled()) {
$this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
}
}


2) In the same File inside public function __construct() function add the following condition
$this->_formScripts[] = "
function toggleEditor() {
if (tinyMCE.getInstanceById('show_content') == null) {
tinyMCE.execCommand('mceAddControl', false, 'show_content');
} else {
tinyMCE.execCommand('mceRemoveControl', false, 'show_content');
}
}
";

If you already have the $this->_formScripts[] then insert function toggleEditor() and it's content to it.

3) Now go to Yournamespace_Modulename_Block_Adminhtml_Modulename_Edit_Tab_Form
$fieldset->addField('description', 'editor', array(
'name' => 'description',
'label' => Mage::helper('show')->__('Description'),
'title' => Mage::helper('show')->__('Description'),
'wysiwyg' => true,
'required' => false,
));

10/29/2013

How to add Diners Club credit card in magento

Default Magento doesn't have this card in his payment gateway. If you want to add then follow the below steps, I have describe here in core file you need to override all core files before doing any changes

Go to app/code/core/Mage/Payemtn/etc/Config.xml then go to line number 56 you can find there are already some card has defined, you need to add your code there

<payment> <!--(already defined)-->
<cc> <!--(already defined)-->
<types> <!--(already defined)-->
<DC>
<code>DC</code>
<name>Diners Club</name>
<order>80</order>
</DC>
</types> <!--(already defined)-->
</cc><!--(already defined)-->
</types><!--(already defined)-->

You can copy paste this section in your custom module to add the Diners Club card else Copy from <DC> to </DC> and paste it after the </OT>

Now you successfully added the card in to Credit card Dropdown.

Next you have to validate the card, Magento use two types of validation one Javascript validation another PHP validation You have to validate both way.

1) Javascript Validation
Go to magento root directory then js/prototype/validation.js then go to end of the page you will find there already all card validation added, The code will be something like this

Validation.creditCartTypes = $H({
'SS': [new RegExp('^((6759[0-9]{12})|(5018|5020|5038|6304|6759|6761|6763[0-9]{12,19})|(49[013][1356][0-9]{12})|(6333[0-9]{12})|(6334[0-4]\d{11})|(633110[0-9]{10})|(564182[0-9]{10}))([0-9]{2,3})?$'), new RegExp('^([0-9]{3}|[0-9]{4})?$'), true],
'SO': [new RegExp('^(6334[5-9]([0-9]{11}|[0-9]{13,14}))|(6767([0-9]{12}|[0-9]{14,15}))$'), new RegExp('^([0-9]{3}|[0-9]{4})?$'), true],
'SM': [new RegExp('(^(5[0678])[0-9]{11,18}$)|(^(6[^05])[0-9]{11,18}$)|(^(601)[^1][0-9]{9,16}$)|(^(6011)[0-9]{9,11}$)|(^(6011)[0-9]{13,16}$)|(^(65)[0-9]{11,13}$)|(^(65)[0-9]{15,18}$)|(^(49030)[2-9]([0-9]{10}$|[0-9]{12,13}$))|(^(49033)[5-9]([0-9]{10}$|[0-9]{12,13}$))|(^(49110)[1-2]([0-9]{10}$|[0-9]{12,13}$))|(^(49117)[4-9]([0-9]{10}$|[0-9]{12,13}$))|(^(49118)[0-2]([0-9]{10}$|[0-9]{12,13}$))|(^(4936)([0-9]{12}$|[0-9]{14,15}$))'), new RegExp('^([0-9]{3}|[0-9]{4})?$'), true],
'VI': [new RegExp('^4[0-9]{12}([0-9]{3})?$'), new RegExp('^[0-9]{3}$'), true],
'MC': [new RegExp('^5[1-5][0-9]{14}$'), new RegExp('^[0-9]{3}$'), true],
'AE': [new RegExp('^3[47][0-9]{13}$'), new RegExp('^[0-9]{4}$'), true],
'DI': [new RegExp('^6011[0-9]{12}$'), new RegExp('^[0-9]{3}$'), true],
'JCB': [new RegExp('^(3[0-9]{15}|(2131|1800)[0-9]{11})$'), new RegExp('^[0-9]{4}$'), true],
'OT': [false, new RegExp('^([0-9]{3}|[0-9]{4})?$'), false]
});

then replace this function with the below content

Validation.creditCartTypes = $H({
'SS': [new RegExp('^((6759[0-9]{12})|(5018|5020|5038|6304|6759|6761|6763[0-9]{12,19})|(49[013][1356][0-9]{12})|(6333[0-9]{12})|(6334[0-4]\d{11})|(633110[0-9]{10})|(564182[0-9]{10}))([0-9]{2,3})?$'), new RegExp('^([0-9]{3}|[0-9]{4})?$'), true],
'SO': [new RegExp('^(6334[5-9]([0-9]{11}|[0-9]{13,14}))|(6767([0-9]{12}|[0-9]{14,15}))$'), new RegExp('^([0-9]{3}|[0-9]{4})?$'), true],
'SM': [new RegExp('(^(5[0678])[0-9]{11,18}$)|(^(6[^05])[0-9]{11,18}$)|(^(601)[^1][0-9]{9,16}$)|(^(6011)[0-9]{9,11}$)|(^(6011)[0-9]{13,16}$)|(^(65)[0-9]{11,13}$)|(^(65)[0-9]{15,18}$)|(^(49030)[2-9]([0-9]{10}$|[0-9]{12,13}$))|(^(49033)[5-9]([0-9]{10}$|[0-9]{12,13}$))|(^(49110)[1-2]([0-9]{10}$|[0-9]{12,13}$))|(^(49117)[4-9]([0-9]{10}$|[0-9]{12,13}$))|(^(49118)[0-2]([0-9]{10}$|[0-9]{12,13}$))|(^(4936)([0-9]{12}$|[0-9]{14,15}$))'), new RegExp('^([0-9]{3}|[0-9]{4})?$'), true],
'VI': [new RegExp('^4[0-9]{12}([0-9]{3})?$'), new RegExp('^[0-9]{3}$'), true],
'MC': [new RegExp('^5[1-5][0-9]{14}$'), new RegExp('^[0-9]{3}$'), true],
'AE': [new RegExp('^3[47][0-9]{13}$'), new RegExp('^[0-9]{4}$'), true],
'DI': [new RegExp('^6011[0-9]{12}$'), new RegExp('^[0-9]{3}$'), true],
'JCB': [new RegExp('^(3[0-9]{15}|(2131|1800)[0-9]{11})$'), new RegExp('^[0-9]{4}$'), true],
'OT': [false, new RegExp('^([0-9]{3}|[0-9]{4})?$'), false],
'DC': [new RegExp('^3(?:0[0-5]|[68][0-9])[0-9]{11}$'), new RegExp('^[0-9]{3}$'), true]
});

So this way you can do the javascript validation

If you don't want to edit the js core file then add the below script in to your form/cc.phtml or in your custom phtml file which is responsible for showing the payment Block

<script type="text/javascript">
Validation.creditCartTypes.set('DC', [new RegExp('^3(?:0[0-5]|[68][0-9])[0-9]{11}$'), new RegExp('^[0-9]{3}$'), true]);
</script>

2) PHP validation
Go to app/code/core/Mage/Payment/Model/Method/Cc.php
there you can find the validation line number near about 111

$ccTypeRegExpList = array(
//Solo, Switch or Maestro. International safe
'SO' => '/(^(6334)[5-9](\d{11}$|\d{13,14}$))|(^(6767)(\d{12}$|\d{14,15}$))/', // Solo only
'SM' => '/(^(5[0678])\d{11,18}$)|(^(6[^05])\d{11,18}$)|(^(601)[^1]\d{9,16}$)|(^(6011)\d{9,11}$)|(^(6011)\d{13,16}$)|(^(65)\d{11,13}$)|(^(65)\d{15,18}$)|(^(49030)[2-9](\d{10}$|\d{12,13}$))|(^(49033)[5-9](\d{10}$|\d{12,13}$))|(^(49110)[1-2](\d{10}$|\d{12,13}$))|(^(49117)[4-9](\d{10}$|\d{12,13}$))|(^(49118)[0-2](\d{10}$|\d{12,13}$))|(^(4936)(\d{12}$|\d{14,15}$))/',

'SS' => '/^((6759[0-9]{12})|(6334|6767[0-9]{12})|(6334|6767[0-9]{14,15})|(5018|5020|5038|6304|6759|6761|6763[0-9]{12,19})|(49[013][1356][0-9]{12})|(633[34][0-9]{12})|(633110[0-9]{10})|(564182[0-9]{10}))([0-9]{2,3})?$/', // Maestro / Solo
'VI' => '/^4[0-9]{12}([0-9]{3})?$/', // Visa
'MC' => '/^5[1-5][0-9]{14}$/', // Master Card
'AE' => '/^3[47][0-9]{13}$/', // American Express
'DI' => '/^6011[0-9]{12}$/', // Discovery
'JCB' => '/^(3[0-9]{15}|(2131|1800)[0-9]{11})$/', // JCB
);

replace the above code with the below code

$ccTypeRegExpList = array(
//Solo, Switch or Maestro. International safe
'SO' => '/(^(6334)[5-9](\d{11}$|\d{13,14}$))|(^(6767)(\d{12}$|\d{14,15}$))/', // Solo only
'SM' => '/(^(5[0678])\d{11,18}$)|(^(6[^05])\d{11,18}$)|(^(601)[^1]\d{9,16}$)|(^(6011)\d{9,11}$)|(^(6011)\d{13,16}$)|(^(65)\d{11,13}$)|(^(65)\d{15,18}$)|(^(49030)[2-9](\d{10}$|\d{12,13}$))|(^(49033)[5-9](\d{10}$|\d{12,13}$))|(^(49110)[1-2](\d{10}$|\d{12,13}$))|(^(49117)[4-9](\d{10}$|\d{12,13}$))|(^(49118)[0-2](\d{10}$|\d{12,13}$))|(^(4936)(\d{12}$|\d{14,15}$))/',

'SS' => '/^((6759[0-9]{12})|(6334|6767[0-9]{12})|(6334|6767[0-9]{14,15})|(5018|5020|5038|6304|6759|6761|6763[0-9]{12,19})|(49[013][1356][0-9]{12})|(633[34][0-9]{12})|(633110[0-9]{10})|(564182[0-9]{10}))([0-9]{2,3})?$/', // Maestro / Solo
'VI' => '/^4[0-9]{12}([0-9]{3})?$/', // Visa
'MC' => '/^5[1-5][0-9]{14}$/', // Master Card
'AE' => '/^3[47][0-9]{13}$/', // American Express
'DI' => '/^6011[0-9]{12}$/', // Discovery
'JCB' => '/^(3[0-9]{15}|(2131|1800)[0-9]{11})$/', // JCB
'DC' => '/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/', //Diners Club
);

Now go to Line number 183 there already a function which validate CVV number . The codes are

public function getVerificationRegEx()
{
$verificationExpList = array(
'VI' => '/^[0-9]{3}$/', // Visa
'MC' => '/^[0-9]{3}$/', // Master Card
'AE' => '/^[0-9]{4}$/', // American Express
'DI' => '/^[0-9]{3}$/', // Discovery
'SS' => '/^[0-9]{3,4}$/',
'SM' => '/^[0-9]{3,4}$/', // Switch or Maestro
'SO' => '/^[0-9]{3,4}$/', // Solo
'OT' => '/^[0-9]{3,4}$/',
'JCB' => '/^[0-9]{4}$/' //JCB
);
return $verificationExpList;
}

Now replace this function with below function

public function getVerificationRegEx()
{
$verificationExpList = array(
'VI' => '/^[0-9]{3}$/', // Visa
'MC' => '/^[0-9]{3}$/', // Master Card
'AE' => '/^[0-9]{4}$/', // American Express
'DI' => '/^[0-9]{3}$/', // Discovery
'SS' => '/^[0-9]{3,4}$/',
'SM' => '/^[0-9]{3,4}$/', // Switch or Maestro
'SO' => '/^[0-9]{3,4}$/', // Solo
'OT' => '/^[0-9]{3,4}$/',
'JCB' => '/^[0-9]{4}$/', //JCB
'DC' => '/^[0-9]{3}$/' // Diners Club
);
return $verificationExpList;
}

That's All , Now magento will successfully validate your Diners club code. Instead of doing this changes in magento core files try to override the model class then change it in your custom module.

9/03/2013

Create host in ubuntu

To create host you need to have superuser permission. If you have then follow the below command line

sudo vi /etc/hosts
sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/yoursite
sudo vi /etc/apache2/sites-available/yoursite

Then edit the code
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName www.yourwebsite.dev
DocumentRoot /var/www/yourwebsitefoldername
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/yourwebsitefoldername/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

</VirtualHost>


Then make the site available and restart the apache server
sudo a2ensite yoursite
sudo service apache2 restart

4/05/2013

Print array contents in log file of magento

In Magento You can print your log in Exception.log using
Mage::logException($e->getMessage());
If you want to put the log to your custom log file, then you can use write below line
Mage::log('Your Query', Zend_Log::DEBUG, custom.log);
But the problem is that , It will not print any Array data, If you want to print Array data in to log file then you can use any one of the below method
Mage::getModel('core/log_adapter', 'custom.log')->log($arrayvariable);
or
Mage::log('Your Data :'.print_r($arrayvariable, true), Zend_Log::DEBUG, 'custom.log');

12/11/2012

How to create custom cron job in magento

Magento default have a cron.php file located in the root directory. To set a cron in magento you have to create your own module first, and in the config.xml you have to declare the method which will fired and the time. Below is a dump of a etc/config.xml file of a custom cron module

<?xml version="1.0"?>
<config>
<modules>
<Wl_Cronset>
<version>0.1.0</version>
</Wl_Cronset>
</modules>
<crontab>
<jobs>
<Wl_Cronset>
<schedule>
<cron_expr>01 00 * * *</cron_expr>
</schedule>
<run>
<model>cronset/expired::productExpired</model>
</run>
</Wl_Cronset>
</jobs>
</crontab>
<global>
<models>
<cronset>
<class>Wl_Cronset_Model</class>
</cronset>
</models>
</global>
</config>

As per mentioned in this xml your cron will be fired every day @ night 00:01 min

Now you have to write your own functionality in your module Model file, Here my model file name is Expired and the function which will trigger is productExpired().

<?php
class Wl_Cronset_Model_Expired extends Mage_Core_Model_Abstract
{
public function productExpired()
{
// Your code goes here
}
}
?>

Now login to your server to call the magento root cron.php and set to be fired in every minute.

7/30/2012

How to Add country and state Dropdown in magento admin

I din't able to create this in proper way like magento does but if you will have all the state of all country then I think this is the perfect solution. If you don't have all state for all country then this module is not solved your problem. If any one knows the correct way then please add the solution via comment. I am describing here How I exactly did.

Open your form which is in Yournamespace/Modulename/Block/Adminhtml/Modulename/Edit/Tab/Form.php then add below fields

$country = $fieldset->addField('country', 'select', array(
            'name'  => 'country',
            'label'     => 'Country',
            'values'    => Mage::getModel('adminhtml/system_config_source_country') ->toOptionArray(),
            'onchange' => 'getstate(this)',
        ));

$fieldset->addField('state', 'select', array(
            'name'  => 'state',
            'label'     => 'State',
            'values'    => Mage::getModel('modulename/modulename')
                            ->getstate('AU'),
        ));

         /*
         * Add Ajax to the Country select box html output
         */
        $country->setAfterElementHtml("<script type=\"text/javascript\">
            function getstate(selectElement){
                var reloadurl = '". $this
                 ->getUrl('modulename/adminhtml_modulename/state') . "country/' + selectElement.value;
                new Ajax.Request(reloadurl, {
                    method: 'get',
                    onLoading: function (stateform) {
                        $('state').update('Searching...');
                    },
                    onComplete: function(stateform) {
                        $('state').update(stateform.responseText);
                    }
                });
            }
        </script>");

Now Create State Action in modulenamecontroller.php file which will be like this

    public function stateAction() {
        $countrycode = $this->getRequest()->getParam('country');
        $state = "<option value=''>Please Select</option>";
        if ($countrycode != '') {
            $statearray = Mage::getModel('directory/region')->getResourceCollection() ->addCountryFilter($countrycode)->load();
            foreach ($statearray as $_state) {
                $state .= "<option value='" . $_state->getCode() . "'>" . $_state->getDefaultName() . "</option>";
            }
        }
        echo $state;
    }

Wysiwyg Editor not working in custom module created by Module creator

When you create a module using module creator then if you try to turn on the Wysiwyg editor of magento then you need to add some function in the Edit.php file which resides under Namespace/Modulename/Block/Adminhtml/Modulename/Edit.php

protected function _prepareLayout() {
    parent::_prepareLayout();
    if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled()) {
        $this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
    }
}

Now turn on the Wysiwyg editor in your code which may be like below

$fieldset->addField('summary', 'editor', array(
            'name' => 'summary',
            'label' => Mage::helper('modulename')->__('Description'),
            'title' => Mage::helper('modulename')->__('Description'),
            'style' => 'height:100px;',
            'wysiwyg' => true,
            'required' => true,
        ));

7/24/2012

Join custom table to product collection in magento

For your custom module if you want to join your custom table data with Magento default product collection then you need to join tables with the entiry_id of the product and your product_id stored in your custom table. Here I have just used the resource model collection to join the table. I tried with getmodel feature of Magento don't know why it doesn't able to create the Product grid in the backend. So I used resource model and it worked. If you are only joining the table, not to generate the Product grid, then you can use getmodel instead of resource model.

$collection = Mage::getResourceModel('catalog/product_collection')
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('sku')
                ->addAttributeToSelect('price')
                ->addAttributeToSelect('status')
                ->addAttributeToSelect('visibility')
                ->addAttributeToFilter('type_id', array('eq' => 'simple'))
                ->addFieldToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
                ->addAttributeToFilter('visibility', array('neq' => 1));

$collection->getSelect()->join(array('mep' => "mage_brand_product"), "e.entity_id = mep.product_id", array('mep.*'));

5/17/2012

addFiledToFilter Condition In Magento

As I already mentioned on the previous post that we only can give the condition of attribute name to filter the result. There is another method to filter result that you can use in your customer Module. Let you are having a customer module which is responsible to show some banner on the homepage. For that let you have some place to upload image and also have feature to give sort order and enable disable feature. Here in the below code I will show how to filter the result with addFieldtoFilter

$banner = Mage::getModel('mymodulename/banner')->getCollection();

Now I wish to show banner whose status are active. I already have a field name/column name 'status' in my custom table.

$banner -> addFieldToFilter('status', array('eq' => 1));

Now I will sort the result according to sort order in my table. I already have a field name 'sort_order' in my table. To sort according to that column I will write

$banner -> setOrder('sort_order', 'DESC');