Jump to content

Prestashop module with custom php page


Recommended Posts

Hello,

I am staring with in hours here: http://doc.prestashop.com/display/PS15/Creating+a+PrestaShop+module

 

My wish is to create Prestashop module that shows custom page with homepage hook where I can execute some PHP code (in this case reservation from that uses PHP mail function).

 

I managed to create and install a module, but nothing appears.

 

So if someone have good will and time to share his experience I will be very glad.

I believe that it will be useful for many others.

 

Thanks!

 
Link to comment
Share on other sites

Hello Milos :)

Here is the code from mymodule.php:

<?php
if (!defined('_PS_VERSION_'))
  exit;
 
class MyModule extends Module
{
  public function __construct()
  {
    $this->name = 'mymodule';
    $this->tab = 'front_office_features';
    $this->version = '1.0';
    $this->author = 'Firstname Lastname';
    $this->need_instance = 0;
    $this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.6');
    $this->dependencies = array('blockcart');
 
    parent::__construct();
 
    $this->displayName = $this->l('My module');
    $this->description = $this->l('Description of my module.');
 
    $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
 
    if (!Configuration::get('MYMODULE_NAME'))      
      $this->warning = $this->l('No name provided');
  }	
  public function hookDisplayLeftColumn($params)
{
  $this->context->smarty->assign(
      array(
          'my_module_name' => Configuration::get('MYMODULE_NAME'),
          'my_module_link' => $this->context->link->getModuleLink('mymodule', 'display')
      )
  );
  return $this->display(__FILE__, 'mymodule.tpl');
}
   
public function hookDisplayRightColumn($params)
{
  return $this->hookDisplayLeftColumn($params);
}
   
public function hookDisplayHeader()
{
  $this->context->controller->addCSS($this->_path.'css/mymodule.css', 'all');
}  
}
?>

mymodule.tpl:

<!-- Block mymodule -->
<div id="mymodule_block_left" class="block">
  <h4>Welcome!</h4>
  <div class="block_content">
    <p>Hello,
       {if isset($my_module_name) && $my_module_name}
           {$my_module_name}
       {else}
           World
       {/if}
       !       
    </p>   
    <ul>
      <li><a href="{$my_module_link}" title="Click this link">Click me!</a></li>
    </ul>
  </div>
</div>
<!-- /Block mymodule -->

mymodule.css:

div#mymodule_block_left p {
  font-size: 150%;
  font-style:italic;
}

Also there is the structure of the folders:

post-61615-0-17667100-1398598059_thumb.png

 

I am not much of a programmer so even if it does shows it's content to its hook I still don't know where to put my PHP code.

 

Thank you for your time!

 

Link to comment
Share on other sites

Alright ... this time I did it slowly and step by step.

Here it is the code for my module.php: 

<?php
if (!defined('_PS_VERSION_'))
  exit;
 
class MyModule extends Module
{
  public function __construct()
  {
    $this->name = 'mymodule';
    $this->tab = 'front_office_features';
    $this->version = '1.0';
    $this->author = 'Svetoslav Doikov';
    $this->need_instance = 1;
    $this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.6');
    //$this->dependencies = array('blockcart');
 
    parent::__construct();
 
    $this->displayName = $this->l('My module');
    $this->description = $this->l('Description of my module.');
 
    $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
 
    if (!Configuration::get('MYMODULE_NAME'))      
      $this->warning = $this->l('No name provided');
  }



	public function install()
	{
  	if (Shop::isFeatureActive())
    Shop::setContext(Shop::CONTEXT_ALL);
 
  	return parent::install() &&
    $this->registerHook('home') &&
    Configuration::updateValue('MYMODULE', 'my friend');
}



}

?>

The other files are /views/templates/front/mymodule.tpl, /views/templates/hook/mymodule.tpl.

 

Here it is mymodule.tpl

<!-- Block mymodule -->
<div id="mymodule_block_left" class="block">
  <h4>Welcome!</h4>
  <div class="block_content">
    <p>Hello,
       {if isset($my_module_name) && $my_module_name}
           {$my_module_name}
       {else}
           World
       {/if}
       !       
    </p>   
    <ul>
      <li><a href="{$my_module_link}" title="Click this link">Click me!</a></li>
    </ul>
  </div>
</div>
<!-- /Block mymodule -->

The module installs itself successfully and upon installing it creates 'home' hook. After that if the hook is removed it can't be 'hooked' manually again. In any cases nothing appears on homepage.

 

My complete goal is to create module which adds separate dynamic PHP page, and creates link to that page in category block.

Edited by IGNITER (see edit history)
Link to comment
Share on other sites

OK, here it is working installing/uninstalling hooks/unhooks successfully and adding the content from the tpl file to 'home' page.

 

mymodule.php

<?php
if (!defined('_PS_VERSION_'))
  exit;
 
class MyModule extends Module
{
  public function __construct()
  {
    $this->name = 'mymodule';
    $this->tab = 'front_office_features';
    $this->version = '1.0';
    $this->author = 'Svetoslav Doikov';
    $this->need_instance = 1;
    $this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.6');
    //$this->dependencies = array('blockcart');
 
    parent::__construct();
 
    $this->displayName = $this->l('My module');
    $this->description = $this->l('Description of my module.');
 
    $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
 
    if (!Configuration::get('MYMODULE_NAME'))      
      $this->warning = $this->l('No name provided');
  }



public function hookDisplayHome($params)
	{
		return $this->display(__FILE__, 'views/templates/hooks/mymodule.tpl');	
	}
		
		

	public function install()
	{
  	if (Shop::isFeatureActive())
    Shop::setContext(Shop::CONTEXT_ALL);
 
  	return parent::install() &&
    $this->registerHook('home') &&
    Configuration::updateValue('MYMODULE', 'my friend');
}



}

?>

They are two mysteries left ...

1. how do I create complete, separate page that executes PHP code (I guess that there must be a controller for that PHP code)?

2. how do I invoke it from link in the categories block?

 

I guess that I will need like forever for that on my own :)

Get in touch guys.

Edited by IGNITER (see edit history)
Link to comment
Share on other sites

1. you need to create module controller

<?php
class mymoduledisplayModuleFrontController extends ModuleFrontController
{
  public function initContent()
  {
    parent::initContent();
    $this->setTemplate('display.tpl');
  }
}

more info here: http://doc.prestashop.com/display/PS15/Creating+a+PrestaShop+module#CreatingaPrestaShopmodule-Embeddingatemplateinthetheme

 

 

2. you mean that you want to create link to your module page from block categories module?

Link to comment
Share on other sites

OK, now pushing over the controller.

 

For 2. yes, you got it right.

It is a reservation form on PHP so it came to my mind that it will look more natural if the reservation page will look more naturally next to the products.

It seems that the two modules must interact somehow, which might mean complicating the module and the code.

 

The other thing that came to my mind is to put the link for the reservation page in a separate .block.

 

Thanks for the assist!

Link to comment
Share on other sites

in blockcategories.tpl you can create <li></li> element with a href param based on Link object:

{$link->getModuleLink('yourmodule', 'yourController')}

something like:

<li><a href="{$link->getModuleLink('yourmodule', 'yourController')}">{l s='Your Controller' mod='blockcategories'}</a></li>
Link to comment
Share on other sites

I feel that I am very close, almost everything is done except the PHP Mail code.

 

post-61615-0-21751500-1399293021_thumb.png

 

It loads the content from the .tpl file, but the rest of the page is missing.

What I am missing here? Wrong path or what?

 

Link to comment
Share on other sites

I managed it to work -> http://urbain5.credo96.com/index.php?fc=module&module=reservation&controller=display&id_lang=2

Thanks to this -> http://www.prestashop.com/forums/topic/290064-solved-link-not-working-creating-a-prestashop-15-module/

When I placed the files accordingly and the content appear.

I am not sure it is the right way, but at least it works.

 

They are 2 things left:

1. to attach the CSS and JS properly (as far as I can see the CSS works strange)

2. to include somewhere that piece of PHP code that actually sends the mails.

 

Any help will be appreciated! :)

Link to comment
Share on other sites

1) to add css and js files use code like (example)

$this->addCSS(_THEME_CSS_DIR_.'product.css');

to add js:

$this->addJS(_THEME_JS_DIR_.'tools.js');

use this code in controller setMedia() function

Link to comment
Share on other sites

As far as I understand it ... it should be like:

public function setMedia()
	{
		$this->addCSS(_THEME_CSS_DIR_.'css/reservation.css');
		$this->addJS(_THEME_JS_DIR_.'js/reservation.js');
			
		return parent::setMedia();
	}

I have tried this in the bootstrap module file, it didn't work.

Am I wrong?

Link to comment
Share on other sites

The main module file ... this one:

reservation.php

<?php
if (!defined('_PS_VERSION_'))
  exit;
 
class Reservation extends Module
{
  public function __construct()
  {
    $this->name = 'reservation';
    $this->tab = 'front_office_features';
    $this->version = '0.1.0';
    $this->author = 'Svetoslav Doikov';
    $this->need_instance = 1;
    $this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.6');
    //$this->dependencies = array('blockcart');
 
    parent::__construct();
 
    $this->displayName = $this->l('Reservation');
    $this->description = $this->l('Simple reservation mailer.');
 
    $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
 
    if (!Configuration::get('MYMODULE_NAME'))      
      $this->warning = $this->l('No name provided');
  }




public function hookDisplayLeftColumn($params)
{
  $this->context->smarty->assign(
      array(
          'reservation' => Configuration::get('RESERVATION'),
          'my_module_link' => $this->context->link->getModuleLink('reservation', 'display')
      )
  );
  return $this->display(__FILE__, 'views/templates/hooks/reservation.tpl');
}

	public function install()
	{
  	if (Shop::isFeatureActive())
    Shop::setContext(Shop::CONTEXT_ALL);
 
  	return parent::install() &&
    $this->registerHook('LeftColumn') &&
	$this->registerHook('header') &&
    Configuration::updateValue('RESERVATION', 'my friend');
}

}

?>

It is described that way in the docks so I have decided to name it that way.

Link to comment
Share on other sites

I have put this code:

public function setMedia(){
	parent::setMedia();
	if ($this->assignCase == 1)
	$this->addJS(_THEME_JS_DIR_.'functions.js');
	$this->addJS(_THEME_JS_DIR_.'zebra_datepicker.js');
	$this->addCSS(_THEME_CSS_DIR_.'reservation.css');
	$this->addCSS(_THEME_CSS_DIR_.'zebra_datepicker.css');
}

in /controllers/front/display.php

 

It seems that it does something but comparing to what must be is not the same.

Don't know where the problem is.

Link to comment
Share on other sites

I think I have an idea what's going on.

Here is the code from one of the CSS files as is:

reservation.css

.form {
	width: 300px;
	font-size:15px;
	font-family:Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif; 
	padding-left: 5px;
	padding-right: 5px;
	background: url(../img/form.jpg);
	border: 1px solid #CCC;
}

.half_form {
	width: 140px;
	font-size:15px;
	font-family:Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
	padding-left: 5px;
	padding-right: 5px;
	background: url(../img/half_form.jpg);
	border: 1px solid #CCC;

}

.nano_form {
	width: 70px;
	font-size:20px;
	font-family:Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
	padding-left: 6px;
	padding-right: 6px;
	background: url(../img/nano_form.jpg);
	border: 1px solid #CCC;
}

.msg_form {
	width: 300px;
	height:120px;
	font-size:15px;
	font-family:Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
	padding-left: 6px;
	padding-right: 6px;
	background: url(../img/msg_form.jpg);
	border: 1px solid #CCC;
	resize: none;
}

Maybe the image path "background: url(../img/image.jpg);" can't be located for some reason.

The other thing is the CSS styles of the forms such as: width: 300px; height: 120px; resize: none; are not recognized properly.

The only thing that is OK is the "font-size" declaration.

 

The directory structure looks that way:

post-61615-0-21545500-1399379146_thumb.jpg

Edited by IGNITER (see edit history)
Link to comment
Share on other sites

There is a solution I found.

I put a static page in the module dir and in the file /views/templates/front/display.tpl I entered simple <iframe src="{$modules_dir}/... > ... </iframe> tag.

 

It's not beautiful, it is not nice, but at least is working.

Since it is static page there is no a translation.

 

I came to the conclusion that it is not easy to write fully functional module for prestashop.

 

In favor of the community I am uploading my progress of that module so far.

Feel free to use it for whatever purpose you want, modify it, improve it and make it fully functional if you can.

 

reservation.zip

Edited by IGNITER (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...