Passa ai contenuti principali

Develop Prestashop Module - puntata 1 creazione di un modulo prestashop



la creazione o lo sviluppo di un modulo Prestashop funziona sia per il front-end che il back-end del sistema
la struttura di un modulo è fissa che si compone con una serie di step:

  • file di configurazione
  • file di installazione
  • files controllers per l'amministrazione
  • files controllers per il frontend
  • files delle view

creazione dell'infrastruttura base del modulo per Frontend

all'interno della cartella modules di prestashop creare una cartella 
esempio
    <root>
         |---modules
                   |-----miomodulo

naturalmente il nome del modulo lo potete personalizzare
all'interno della cartella miomodulo creare i seguenti file
  1. config.xml
  2. index.php
  3. miomodulo.php   <-- inserite il vostro nome modulo al posto di mio modulo

index.php

il codice di index php, file che dovrà essere inserito in ogni cartella:
<?php
/*
* 2007-2013 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-2013 PrestaShop SA
*  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

header("Location: ../");
exit;

config.xml

il file contiene delle semplici configurazioni per installare il modulo, anche se la vera installazione sarà eseguita con un'altro file
<?xml version="1.0" encoding="UTF-8" ?>
        <module>
            <name>miomodulo</name>
            <displayName><![CDATA[Il mio modulo]]></displayName>
            <version><![CDATA[1.0]]></version>
            <description><![CDATA[Descrizione del mio modulo]]></description>
            <author><![CDATA[Bwlab - www.bwlab.it - info@bwlab.it]]></author>
            <tab><![CDATA[others]]></tab>
<confirmUninstall>Sicuro che vuoi disinstallare il modulo?</confirmUninstall>
            <is_configurable>1</is_configurable>
            <need_instance>1</need_instance>
<limited_countries></limited_countries>
        </module>

il tag tab inserisce il modulo nella categoria che trovate nella lista dei moduli in back-end
e le categorie sono
  1. admnistration
  2. advertising_marketing
  3. analytics_stats
  4. billing_invoicing
  5. checkout
  6. front_office_features
  7. migration_tools
  8. payments_gataweys
  9. payment_security
  10. pricing_promotion
  11. search_filter
  12. seo
  13. shiping_logistics
  14. smart_shoppig
  15. market_place
  16. others
  17. mobile

miomodulo.php

questo è il contenuto standard di un file di installazione modulo
<?php
if (!defined('_PS_VERSION_')) {
    exit;
}

class MioModulo extends Module
{

    /* @var boolean error */
    protected $_errors = false;

    public function __construct()
    {

        $this->name = 'miomodulo';   <-- inserite il nome del vostro modulo
        $this->tab = 'others';             <-- la categoria del modulo
        $this->version = '1.0';      
        $this->author = 'Bwlab - www.bwlab.it - info@bwlab.it';
        $this->displayName = $this->l('Il mio modulo');
        $this->description = $this->l('Descrizione del mio modulo.');
        $this->confirmUninstall = $this->l('sicuro della disinstallazione?');

        parent::__construct();
    }

     //questo metodo viene richiamato per 'installazione
    public function install()
    {
        if (!parent::install()) {
            return false;
        }

        $sql = array();

      //in questo momento si imposta l'sql per la creazione di una o più tabelle
      //   notate il _DB_PREFIX_
        $sql[] = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'miomodulo` (
 `id` int(10) NOT NULL AUTO_INCREMENT,
 `title` text,
 PRIMARY KEY  (`id`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;';

        //creazione effettiva tabelle
        foreach ($sql as $s)
            if (!Db::getInstance()->Execute($s))
                return false;

       return true;
    }

   //questo è il metodo di disinstallazione del modulo
    public function uninstall()
    {
        if (!parent::uninstall()) {
            return false;
        }
        $sql = array();
        $sql[] = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'bwlab_miomodulo`;';

        foreach ($sql as $s)
            if (!Db::getInstance()->Execute($s))
                return false;

        return true;
    }
  
}

abbiamo settato il modulo e lo possiamo installare

basta andare sulla lista dei moduli nel back-end e lo vedremo elencato nella categoria assegnata
ma ora che ce ne facciamo?
alla prossima puntata

Commenti

Post popolari in questo blog

install language on osTicket

this simple guide aims you to install and configure your language on osTicket i written this guide based on forum post http://osticket.com/forum/discussion/76252/installing-language-pack

Vue.js inline component

title: Componente Vue.js inline browser tags: vue.js, single file component vue.js Come creare un componente con Vue.js? Come crearlo e come usarlo direttamente nella pagine? Mi trovo in diversi progetti a bisticciare con jquery che ritengo un ottimo tool, ma non supremo. In ogni progetto possono esistere più tecnologie per risolveere problemi diversi. La SPA la preferisco con React o Angular, ma per creare dei componenti ad hoc o uso Polymer o Vue. Senza scomodare l’intero scaffolding di progetto è possibile creare un semplice componente ed usarlo nella pagina del browser senza fare il transpiling. Come iniziare. Preparo la base di folder. Come server uso quello integrato in php. mkdir vuewjs cd vuejs php -S localhost:8080 touch index.html google-chrome index.html Io uso Ubuntu e quelli sopra sono semplici comandi shell che creano un folder vuejs e dentro creano un file html e avviano il server presente in php7; infine apro con chrome la pagina index.hml. Pagina base Ora prep...