All about Magento E-commerce Store.......MagentoForum: Magento Custom Module :cash on delivery

Saturday, July 2, 2011

Magento Custom Module :cash on delivery

So I've seen more and more people raising awareness about desire to create a new payment method that allows orders to be paid via "cash on delivery" or "on pickup" by customer. Is this helpful to companies who sell to local clients? Absolutely.

 

So in offering a solution, I'll go ahead and outline what files need to be created and why with hopes to help educate the intigued learner in how to create aMagento Custom Module as well. The benefit in knowing how to do this is modifying existing Magento functionality in a way that it will not be overwritten upon a successful Magento upgrade.

 

Thus, I'll jump in. The following 5 files will be created (relative to one's Magento root folder):
 

  • confix.xml
  • system.xml
  • PaymentMethod.php
  • mysql4-install-0.1.0.php
  • NewModule.xml

Here are their contents (with comments) and relative paths:

 

app/code/local/Mage/NewModule/etc/config.xml (below)

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?xml version="1.0"?>
<!--
/**
* Elias Interactive
*
* @title      Magento -> Custom Payment Module for Cash On Delivery
* @category   Mage
* @package    Mage_Local
* @author     Lee Taylor / Elias Interactive -> lee [at] eliasinteractive [dot] com
* @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*/
 
-->
 
<config>
<global>
<!-- declare model group for new module -->
<models>
<!-- model group alias to be used in Mage::getModel('newmodule/...') -->
<newmodule>
<!-- base class name for the model group -->
<class>Mage_NewModule_Model</class>
</newmodule>
</models>
 
<!-- declare resource setup for new module -->
<resources>
<!-- resource identifier -->
<newmodule_setup>
<!-- specify that this resource is a setup resource and used for upgrades -->
<setup>
<!-- which module to look for install/upgrade files in -->
<module>Mage_NewModule</module>
</setup>
<!-- specify database connection for this resource -->
<connection>
<!-- do not create new connection, use predefined core setup connection -->
<use>core_setup</use>
</connection>
</newmodule_setup>
<newmodule_write>
<use>core_write</use>
</newmodule_write>
<newmodule_read>
<use>core_read</use>
</newmodule_read>
</resources>
</global>
 
<!-- declare default configuration values for this module -->
<default>
<!-- 'payment' configuration section (tab) -->
<payment>
<!-- 'newmodule' configuration group (fieldset) -->
<newmodule>
<!-- by default this payment method is inactive -->
<active>1</active>
<!-- model to handle logic for this payment method -->
<model>newmodule/paymentMethod</model>
<!-- order status for new orders paid by this payment method -->
<order_status>1</order_status>
<!-- default title for payment checkout page and order view page -->
<title>Cash On Delivery</title>
</newmodule>
</payment>
</default>
</config>

app/code/local/Mage/NewModule/etc/system.xml (below)

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?xml version="1.0"?>
<!--
/**
* Elias Interactive
*
* @title      Magento -> Custom Payment Module for Cash On Delivery
* @category   Mage
* @package    Mage_Local
* @author     Lee Taylor / Elias Interactive -> lee [at] eliasinteractive [dot] com
* @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*/
 
-->
 
<config>
<sections>
<!-- payment tab -->
<payment>
<groups>
<!-- newmodule fieldset -->
<newmodule translate="label" module="paygate">
<!-- will have title 'Cash On Delivery' -->
<label>Cash On Delivery</label>
<!-- position between other payment methods -->
<sort_order>670</sort_order>
<!-- do not show this configuration options in store scope -->
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
<fields>
<!-- is this payment method active for the website? -->
<active translate="label">
<!-- label for the field -->
<label>Enabled</label>
<!-- input type for configuration value -->
<frontend_type>select</frontend_type>
<!-- model to take the option values from -->
<source_model>adminhtml/system_config_source_yesno</source_model>
<!-- field position -->
<sort_order>1</sort_order>
<!-- do not show this field in store scope -->
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</active>
<order_status translate="label">
<label>New order status</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_order_status</source_model>
<!--<source_model>adminhtml/system_config_source_order_status_new</source_model>-->
<!--<source_model>adminhtml/system_config_source_order_status_processing</source_model>-->
<sort_order>4</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</order_status>
<allowspecific translate="label">
<label>Payment from applicable countries</label>
<frontend_type>allowspecific</frontend_type>
<sort_order>50</sort_order>
<source_model>adminhtml/system_config_source_payment_allspecificcountries</source_model>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</allowspecific>
<specificcountry translate="label">
<label>Payment from Specific countries</label>
<frontend_type>multiselect</frontend_type>
<sort_order>51</sort_order>
<source_model>adminhtml/system_config_source_country</source_model>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</specificcountry>
 
<title translate="label">
<label>Title</label>
<frontend_type>text</frontend_type>
<sort_order>2</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</title>
</fields>
</newmodule>
</groups>
</payment>
</sections>
</config>

app/code/local/Mage/NewModule/Model/PaymentMethod.php (below)

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/**
* Elias Interactive
*
* @title      Magento -> Custom Payment Module for Cash On Delivery
* @category   Mage
* @package    Mage_Local
* @author     Lee Taylor / Elias Interactive -> lee [at] eliasinteractive [dot] com
* @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*/
 
class Mage_NewModule_Model_PaymentMethod extends Mage_Payment_Model_Method_Abstract
{
protected $_code  = 'newmodule';
//protected $_formBlockType = 'payment/form_checkmo';
//protected $_infoBlockType = 'payment/info_cod';
 
/**
* Assign data to info model instance
*
* @param   mixed $data
* @return  Mage_Payment_Model_Method_Checkmo
*/
public function assignData($data)
{
$details = array();
if ($this->getPayableTo()) {
$details['payable_to'] = $this->getPayableTo();
}
if ($this->getMailingAddress()) {
$details['mailing_address'] = $this->getMailingAddress();
}
if (!empty($details)) {
$this->getInfoInstance()->setAdditionalData(serialize($details));
}
return $this;
}
 
public function getPayableTo()
{
return $this->getConfigData('payable_to');
}
 
public function getMailingAddress()
{
return $this->getConfigData('mailing_address');
}
 
}

app/code/local/Mage/NewModule/sql/newmodule_setup/mysql4-install-0.1.0.php (below)

?
1
2
<?php
// here are the table creation/updates for this module

app/etc/modules/NewModule.xml (below)

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0"?>
<!--
/**
* Elias Interactive
*
* @title      Magento -> Custom Payment Module for Cash On Delivery
* @category   Mage
* @package    Mage_Local
* @author     Lee Taylor / Elias Interactive -> lee [at] eliasinteractive [dot] com
* @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*/
-->
<config>
<modules>
<!-- declare Mage_NewModule module -->
<mage_NewModule>
<!-- this is an active module -->
<active>true</active>
<!-- this module will be located in app/code/local code pool -->
<codePool>local</codePool>
<!-- specify dependencies for correct module loading order -->
<depends>
<mage_Payment />
</depends>
<!-- declare module's version information for database updates -->
<version>0.1.0</version>
</mage_NewModule>
</modules>
</config>

Again, this serves as a basic configuration for a new payment method that allowsCash On Delivery and is written as a Magento Custom Module. Modifying these files may be necessary, though this should be a good outline to get you started. Any help needed, post here and we'll get some dialogue going to find solutions for your needs.


No comments:

Post a Comment