Create a prestashop module from scratch.

10:09:00 1 Comments

Create a prestashop module



Creating a module is really easy in prestashop in 5 minutes

Below are some easy quick tips that could help you..get an idea about the basics...
1. See the modules folder in the root folder.
2. You can make a copy of any one module inside the many folders you see inside modules folder.
3.now rename the folder you created acoourding to your module name.
4.learn the structure of smarty frameworks php
5.write code that matches your functionality needs and assign the smarty variables and set the template.
6. Remove bugs.
7.here is your awesome created module!

https://www.youtube.com/watch?v=17zYI02Rb64&index=3&list=PLjP3OafnSLJPpTt01JKsKMpHE6PEfwlWz

If you have any doubts you can post below i will explain.Thanks.I will even send you the code joeljfernandes@yahoo.com is my email


But here below are some more detail steps to get this done




Hi this is a post where I will tell you in detail about creating a prestashop 1.6 module..You may have read my other posts but this in this post I will tell you in detail the procedure to create a prestashop module.
First of all you will need to understand the prestashop software and some of its important tables and some other things ..At the end of this post I will also provide you with the files that you can easily use to start creating your own prestashop module.

Creating a module is really easy if you know what you are doing.

First you can go to validator.prestashop.com and you will see an option to generate a module over there!.


Now once you have done this you can Start editing the module.Obviously you will get a file downloaded to your computer once the module is properly generated.

Once this module is generated you can now start adding new code to the module!

You will need to extract the .zip file and then you can copy this file to your prestashop 1.6 directory module folder.

Now this is the one of the most important part...writing the code.

You will need to decide what code you will want to write .This is also important because it will determine for what purpose are you creating this module for.

In this example will teach you how to make a simple slider module in prestashop.Please note that this module is just to show you how the code works (keep in mind that there are a lot of bugs in the code)

The module that I created is called Awesomeslider

here is the link
https://drive.google.com/open?id=0B8Uhxk8DKNMqa0ZVWS1IT1RIaWs&authuser=0

Now let us see the main file below ...This is the awesomeslider.php main module file

<?php
/**
* 2007-2014 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author    PrestaShop SA <contact@prestashop.com>
*  @copyright 2007-2014 PrestaShop SA
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/

if (!defined('_PS_VERSION_'))
exit;

class Awesomeslider extends Module
{
protected $config_form = false;

public function __construct()
{
$this->name = 'awesomeslider';
$this->tab = 'administration';
$this->version = '1.0.0';
$this->author = 'JoelWebsites';
$this->need_instance = 0;

/**
* Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6)
*/
$this->bootstrap = true;

parent::__construct();

$this->displayName = $this->l('awesomeslider');
$this->description = $this->l('awesomeslider will display an awesome slider on your website');

$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
}

/**
* Don't forget to create update methods if needed:
* http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update
*/
public function install()
{
Configuration::updateValue('AWESOMESLIDER_LIVE_MODE', false);

return parent::install() &&
$this->registerHook('header') &&
$this->registerHook('backOfficeHeader') &&
$this->registerHook('displayTop');
}

public function uninstall()
{
Configuration::deleteByName('AWESOMESLIDER_LIVE_MODE');

return parent::uninstall();
}

/**
* Load the configuration form
*/
public function getContent()
{
/**
* If values have been submitted in the form, process.
*/
$this->_postProcess();

$this->context->smarty->assign('module_dir', $this->_path);

$output = $this->context->smarty->fetch($this->local_path.'views/templates/admin/configure.tpl');

return $output.$this->renderForm();
}

/**
* Create the form that will be displayed in the configuration of your module.
*/
protected function renderForm()
{
$helper = new HelperForm();

$helper->show_toolbar = false;
$helper->table = $this->table;
$helper->module = $this;
$helper->default_form_language = $this->context->language->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);

$helper->identifier = $this->identifier;
$helper->submit_action = 'submitAwesomesliderModule';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
.'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');

$helper->tpl_vars = array(
'fields_value' => $this->getConfigFormValues(), /* Add values for your inputs */
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);

return $helper->generateForm(array($this->getConfigForm()));
}

/**
* Create the structure of your form.
*/
protected function getConfigForm()
{
return array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
),
'input' => array(
array(
'type' => 'switch',
'label' => $this->l('Live mode'),
'name' => 'AWESOMESLIDER_LIVE_MODE',
'is_bool' => true,
'desc' => $this->l('Use this module in live mode'),
'values' => array(
array(
'id' => 'active_on',
'value' => true,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => false,
'label' => $this->l('Disabled')
)
),
),
array(
'col' => 3,
'type' => 'text',
'prefix' => '<i class="icon icon-envelope"></i>',
'desc' => $this->l('Enter a valid email address'),
'name' => 'AWESOMESLIDER_ACCOUNT_EMAIL',
'label' => $this->l('Email'),
),
array(
'type' => 'password',
'name' => 'AWESOMESLIDER_ACCOUNT_PASSWORD',
'label' => $this->l('Password'),
),
),
'submit' => array(
'title' => $this->l('Save'),
),
),
);
}

/**
* Set values for the inputs.
*/
protected function getConfigFormValues()
{
return array(
'AWESOMESLIDER_LIVE_MODE' => Configuration::get('AWESOMESLIDER_LIVE_MODE', true),
'AWESOMESLIDER_ACCOUNT_EMAIL' => Configuration::get('AWESOMESLIDER_ACCOUNT_EMAIL', 'contact@prestashop.com'),
'AWESOMESLIDER_ACCOUNT_PASSWORD' => Configuration::get('AWESOMESLIDER_ACCOUNT_PASSWORD', null),
);
}

/**
* Save form data.
*/
protected function _postProcess()
{
$form_values = $this->getConfigFormValues();

foreach (array_keys($form_values) as $key)
Configuration::updateValue($key, Tools::getValue($key));
}

/**
* Add the CSS & JavaScript files you want to be loaded in the BO.
*/
public function hookBackOfficeHeader()
{
$this->context->controller->addJS($this->_path.'js/back.js');
$this->context->controller->addCSS($this->_path.'css/back.css');
}

/**
* Add the CSS & JavaScript files you want to be added on the FO.
*/
public function hookHeader()
{
$this->context->controller->addCSS($this->_path.'/css/style.min.css');
$this->context->controller->addCSS($this->_path.'/css/style.css');
}

public function hookDisplayTop()
{
   $dir = $this->_path;
$path = "$dir/views/img/";
$this->context->smarty->assign(array(
'path' => $path,
));
            $path1 =  $this->_path.'/views/templates';
   return $this->display(__FILE__, 'awesomeslider.tpl');
}
}


As we see in the above file.The code starts with a <?php opening tag and the tag does not close..I will focus on only the code that is the most important ..And I will use //( comments to make it easier for you to understand the purpose of the line of code..


Below are the function for install and uninstall you will need to insert the code for inserting the tables inside these functions so when a user clicks the install button in the admin panel this is the code that actually runs!...You would most likely insert code to create tables using this and as well as delete tables or other important things that you would like to initialize.In this module there was no such thing so important so except for the register Hooks statements there is nothing else.Registering hooks are really important since they will help you display data on the prestashop front office pages!

public function install()
{
Configuration::updateValue('AWESOMESLIDER_LIVE_MODE', false);

return parent::install() &&
$this->registerHook('header') &&
$this->registerHook('backOfficeHeader') &&
$this->registerHook('displayTop');
}

public function uninstall()
{
Configuration::deleteByName('AWESOMESLIDER_LIVE_MODE');

return parent::uninstall();
}

Now lets see the next part of code that was written by me

public function hookHeader()
{
$this->context->controller->addCSS($this->_path.'/css/style.min.css');
$this->context->controller->addCSS($this->_path.'/css/style.css');
}

public function hookDisplayTop()
{
    $dir = $this->_path;
$path = "$dir/views/img/";
  $this->context->smarty->assign(array(
'path' => $path,
));
            $path1 =  $this->_path.'/views/templates';
    return $this->display(__FILE__, 'awesomeslider.tpl');
}
}

The files that contain the css style as well as the javascript files can be used by using the above statements inside the hookHeader().Note that header hook was registered in the install function.
Similarly the hookDisplayTop() was registered in the installation function.Now using this registered hook you will able to display a awesomeslider.tpl by using this function
 return $this->display(__FILE__, 'awesomeslider.tpl');


The .tpl file will have all the html code that you will need to display the slider.Similarly you will be able to display data in the other hooks that are present in prestashop.In this fashion you will need to use some smarty code and place the code in the correct places and you will create a module easily.Thanks for reading the article.



JoelWebsites

Joel Fernandes,I am a Very Smart,Innovative Creative and One of the Best Webdevelopers.I am an expert in Webdevelopment.My other interest include Cars and Vehicles.My desire is to be the best and serve you with the best information I have .I will always share all my tips and tricks on this website and provide you with the best themes so that your life may be simpler.Keep visiting www.joelwebsites.com for the best website related ,tips and tricks and news.Meet me on FaceBook

1 comment:

  1. Hi Joel Many thanks for your videos.they are very much helpful for new comer like me.
    Appreciate your hard work.

    I am building my Prestashop store.I need multi vendor module.I don't know the coding language.I cant buy paid modules.Please suggest the free module.

    Also let me know what are the basic requirement of modules for multi vendor store.
    you can mail to indrakantiviswanatha@gmail.com
    Please help me.

    ReplyDelete