Step 1. Create folder.
Just create a folder with the name of the module. We will call this module cashondelivery so the folder name should be “cashondelivery” with small letters.
Step 2. Create class.
Create a new file called cashondelivery.php
In the top of the file, write the following
class CashOnDelivery extends PaymentModule
{
}
The class is now named CashOnDelivery and is an extension of the PaymentModule class (classes/paymentmodule.php) which means that all functions inside the PaymentModule class is available.
Step 3. Create Constructor
function __construct() {
$this->name = 'cashondelivery';
$this->tab = 'Payment';
$this->version = 0.1;
$this->currencies = false;
parent::__construct();
/* The parent construct is required for translations */
$this->page = basename(__FILE__, '.php');
$this->displayName = $this->l('Cash on delivery (COD)');
$this->description = $this->l('Accept cash on delivery payments');
}
What does it do?
$this->name = 'cashondelivery';
This names the module, it’s important that it’s all small letters, so it matches folder and file name.
$this->tab = 'Payment';
This sets the tab under which the module is visible, since this is a Payment module we write Payment, but you can also name it Blocks and it will be visible in the list of the blocks modules on the modules page, or you can create your own list by naming it to anything you want.
$this->version = 0.1;
Version number of the module, it’s a good idea to always increase this as you make bugfixes or new additions, so the users now what version they have.
$this->currencies = false;
Does the payment module allow currencies restriction to be set?
parent::__construct();
/* The parent construct is required for translations */
$this->page = basename(__FILE__, '.php');
$this->displayName = $this->l('Cash on delivery (COD)');
This is the name of the module that will be displayed in the admin
$this->description = $this->l('Accept cash on delivery payments');
A short description about the module, also visible in the admin.
Installation function
function install()
{
parent::install();
$this->registerHook('payment');
$this->registerHook('paymentReturn');
}
This function is executed when the user clicks on “Install” button in admin. It’s possible to add Database modifications if needed, like configuration pre-sets or similar commands that is needed to run the module.
The code above will register the hook to be possible to execute on hook payment and paymentReturn, but you can select your own hooks here.
Some hooks that are available are
• payment (Executed when payment step I admin is reached)
• paymentReturn (Don’t know, but should be when a return is made)
• rightColumn (The Right column of the site, this is used to place blocks)
• leftColumn (Same as rightColumn, but on the left side)
• top (Top of the page)
• adminStatsModules (Start page of Stats tab)
• footer (Footer of the page)
• home (Home page, same as homefeatured products)
• orderConfirmation
• extraLeft (on product details page)
• header (Header of the html)
Now if you do some kind of database alterations in the install function, it’s a good idea to clean up these changes when the user clicks uninstall, do do this, add a function called “uninstall”
It could look like this
function uninstall()
{
if (!Configuration::deleteByName('NAME OF CONFIG VARIABLE') OR !parent::uninstall())
return false;
return true;
}
What you need to add now, is the code that is executed when the hook is called, different hooks use different names on the functions, for paymentHook, it should be named hookPayment($params) so the code we add next is
function hookPayment($params)
{
global $smarty;
foreach ($params['cart']->getProducts() AS $product)
if (Validate::isUnsignedInt(ProductDownload::getIdFromIdProduct(intval($product['id_product']))))
return false;
$smarty->assign(array( 'this_path' => $this->_path,
'this_path_ssl' => (Configuration::get('PS_SSL_ENABLED') ?'https://' :'http://').htmlspecialchars($_SERVER['HTTP_HOST'], ENT_COMPAT, 'UTF-8').__PS_BASE_URI__.'modules/'.$this->name.'/')
);
return $this->display(__FILE__, 'payment.tpl');
}
The variable $params that is passed to the function call is used to get information from the system, like cart, cookie information and such.
global $smarty; lets you access the smarty commands inside the function i.e. call the tpl file and passing information to the tpl compilation.
$smarty->assign(array(‘VARIABLE’ => value, ‘VARIABLE 2’ => value2));
This is used to pass information to the tpl file, like special variable to display to the user.
return $this->display(__FILE__,’payment.tpl’); this is the call that will execute the tpl file inside the folder and return the html to the page renderer.
Since we also added the hook paymentReturn, we need a function for displaying that data also, so we add
function hookPaymentReturn($params)
{
return $this->display(__FILE__, 'confirmation.tpl');
}
Now the class file is completed, and we move on to the next step.



Back to top











