Prestashop 1.6 create admin module - module

Hi i try to create an admin module to prestashop 1.6 but i find some problems.
I would create ( after module installation ) a new tab in main menù and after click on it i would to show a template (.tpl) where the admin user can do some thing on Db.
I have create a folder and insert it in modules folder, in this folder there are this files:
Controller > admin > AdminGcbulkController.php
gcbulk.tpl
gcbulk.php
gcbulk.php:
<?php
if (!defined('_PS_VERSION_'))
exit;
require(dirname(__FILE__) . '/gcbulk_header.class.php');
class gcbulk extends Module {
private $page_name = '';
public function __construct() {
$this->name = 'gcbulk'; // il nome del modulo (lo stesso del file principale)
$this->tab = 'others'; // sezione in cui va inserito
$this->version = 0.1;
$this->author = 'Autore';
$this->need_instance = 0;
/*
* need_instance specifica se un istanza del modulo deve essere caricata
* quando viene visualizzata la lista dei moduli
(di norma può essere lasciato a 0)
*/
parent::__construct();
$this->displayName = $this->l('Bulk');
$this->description = $this->l('Modulo Bulk');
$this->context->controller->addCSS(($this->_path).'gcbulk.css', 'all');
//$this->context->controller->addJS(($this->_path).'js/gc_bulk_script.js');
}
public function install() {
// Install Tabs
$parent_tab = new Tab();
// Need a foreach for the language
$parent_tab->name[$this->context->language->id] = $this->l('Bulk tab');
$parent_tab->class_name = 'AdminGcbulk';
$parent_tab->id_parent = 0; // Home tab
$parent_tab->module = $this->name;
$parent_tab->add();
if (!parent::install()
|| !$this->registerHook('admingc')
);
}
public function uninstall() {
// Uninstall Tabs
$tab = new Tab((int)Tab::getIdFromClassName('AdminGcbulk'));
$tab->delete();
// Uninstall Module
if (!parent::uninstall())
return false;
return true;
}
public function hookadmingc(){
$this->smarty->assign(array(
'ciao' => 'hola'
));
return $this->display(__FILE__, 'gcbulk.tpl');
}
}
?>
Controller > admin > AdminGcbulkController.php
<?php
class AdminGcbulkController extends ModuleAdminController
{
}
gcbulk.tpl containe simple html.
The creation of tab work fine but i don't know how to show .tpl after clik on it. Actualy return a blank page on the right of admin contents ( on left there is the menù )
Thanks for any help !

the controller should implement renderList as following :
public function renderList() {
$return = $this->context->smarty->fetch(_PS_MODULE_DIR_ . '/gcbulk/gcbulk.tpl');
return $return;
}
I recommend you to move your tpl in this folder : /gcbulk/views/templates/admin/gcbulk.tpl

Related

Prestashop 1.7 : How to create a basic custom admin controller?

I was able to create a menu tab in the back office but when I click on it, I get
Page not found.
The controller is missing or invalid.
Here's the code for my controller -
<?php
class AdminModuleNameConvert extends ModuleAdminController {
public function __construct() {
$this->bootstrap = true;
parent::__construct();
}
}
Using the solution provided by ethercreation, I get the controller to load, but it shows me
Try width :
In your module : modulenameconverter
class modulenameconverter extends Module
{
public function __construct(Context $context = null)
{
$this->name = 'modulenameconverter';
$this->version = '1';
$this->bootstrap = true;
$this->author = 'Stackoverflow';
$this->displayName = $this->l('modulenameconverter');
$this->description = $this->l('Module name converter');
parent::__construct();
}
public function install()
{
$tab = new Tab();
$tab->class_name = 'Adminmodulenameconverter';
$tab->module = 'modulenameconverter';
$tab->name[1] = 'modulenameconverter';
$tab->id_parent = 2;
$tab->active = 1;
if (!$tab->save()) {
return false;
}
return parent::install();
}
public function uninstall()
{
$id_tab = (int)Tab::getIdFromClassName('Adminmodulenameconverter');
$tab = new Tab($id_tab);
if (Validate::isLoadedObject($tab)) {
if (!$tab->delete()) {
return false;
}
} else {
return false;
}
return parent::uninstall();
}
}
In module/controllers/admin/AdminModulenameconverterController.php
class AdminNameconverterController extends ModuleAdminController
{
public function __construct()
{
parent::__construct();
$this->bootstrap = true;
$this->id_lang = $this->context->language->id;
$this->default_form_language = $this->context->language->id;
}
public function initContent()
{
parent::initContent();
}
}
I was having the exact same issue and it appears that I was not configuring the tab parameters correctly... I was trying to pass an array for the "$this->module" parameter so Prestashop could not find the module because in the database, the module linked to the tab was equal to ""...
So my best advice in this case is to always check your database fields to see if they are correctly filled.
To conclude : It's always the silliest issues that makes the biggest headaches... -_-'

How to add a custom input field in Admin Product page

How to add custom input field in Product image form using hooks.I am trying to add a new check box in product image form but i do not know how to create it by using modules as well as i am not able to override core product page template.I am createing directory structure inside themes/classic/module/module_name/.....
If some can write the main module php file of Prestashop 1.7 I am very thankful to you.
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class youmodule extends Module
{
protected $config_form = false;
public function __construct()
{
$this->name = 'youmodule';
$this->tab = 'administration';
$this->version = '1.0.0';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('youmodule');
$this->description = $this->l('youmodule');
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
}
public function install()
{
return parent::install() &&
$this->registerHook('header') &&
$this->registerHook('displayAdminProductsExtra');
}
public function uninstall()
{
return parent::uninstall();
}
public function hookDisplayAdminProductsExtra($params)
{
$id_product = Tools::getValue('id_product');
//YOURCODE
$this->smarty->assign(array(
'yourvariable' => $yourvariabl
));
return $this->display(__FILE__, '/views/templates/admin/product.tpl');
}
}

Prestashop 1.7 admin module show template

I have problem with my first module.
I create a module modules/fashion/fashion.php
<?php
class Fashion extends Module
{
function __construct()
{
$this->name = 'fashion';
$this->tab = 'administration';
$this->version = 1.0;
$this->bootstrap = true;
parent::__construct(); // The parent construct is required for translations
$this->page = basename(__FILE__, '.php');
$this->displayName = $this->l('Block Fashion');
$this->description = $this->l('Add a fashion block');
}
public function install()
{
if (!parent::install() ||
!$this->registerHook('header')) {
return false;
}
if (Shop::isFeatureActive()) {
Shop::setContext(Shop::CONTEXT_ALL);
}
$tab = new Tab();
$tab->active = 1;
$tab->class_name = 'AdminFashionController';
$tab->name = array();
foreach (Language::getLanguages(true) as $lang) {
$tab->name[$lang['id_lang']] = "Fashion";
}
$tab->id_parent = (int)Tab::getIdFromClassName('Fashion');
$tab->module = $this->name;
return $tab->add();
}
public function uninstall()
{
// Uninstall Tabs
$tab = new Tab((int)Tab::getIdFromClassName('Fashion'));
$tab->delete();
// Uninstall Module
if (!parent::uninstall())
return false;
}
/**
* Returns module content
*
* #param array $params Parameters
* #return string Content
*/
}
?>
What is more i create modules/fashion/controller/admin/FashionAdminController.php
<?php
class FashionAdminController extends ModuleAdminController
{
public function initContent(){
parent::initContent();
$this->setTemplate('fashion.tpl');
}
}
?>
And modules/fashion/views/templates/admin/fashion.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>Click me!</li>
</ul>
</div>
</div>
<!-- /Block mymodule -->
So, when i click in my Admin Panel the link Fashion the shows "The page is no found" Why? What i did wrong? Can someone help me?
At first, check if the menu(tab) has been created in the backend or not.
I also think that the admin controller name should start with ADMIN : AdminFashionController and use this name for tab class :
$tab->class_name = 'AdminFashion';
Sample from default module:
public function installTab()
{
$tab = new Tab();
$tab->active = 1;
$tab->class_name = "AdminLinkWidget";
$tab->name = array();
foreach (Language::getLanguages(true) as $lang) {
$tab->name[$lang['id_lang']] = "Link Widget";
}
$tab->id_parent = (int)Tab::getIdFromClassName('AdminParentThemes');
$tab->module = $this->name;
return $tab->add();
}
You have to add "active" and remove "position"(is automatic)

how the Prestashop code will flow?

I am new to prestashop. I wanted learn how the code will flow .
ex. how product are inserting in database ?
how product data is fetching and displaying in store page ?
please help me .
you should open PrestaShop Product class.
you can get all product data using this:
$product = new Product($id_product);
if add a new product:
$product = new Product();
write all parameters like:
$product->name = 'test';
$product->reference = '35GH';
$product->save();
If u print out product array you will see all parameters. GOod luck, have fun. And even lot of information in stackoverflow how to php add product and etc.
If you want to work on products like you want to add products,delete products,want some customization then first you have to create a new module. For that you have to create a new folder in that location with you module name
C:\wamp\www\prestashop\modules\your_module_name
Then after you have to create a file named your_module_name.php in that folder
C:\wamp\www\prestashop\modules\your_module_name\your_module_name.php
After then you have to code in that file like:
your_module_name.php
class your_module_name extends Module
{
public function __construct()
{
$this->name = 'your_module_name';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'abc';
$this->need_instance = 0;
//$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Your Module Name');
$this->description = $this->l('This is the module for facilitate user to purchase particular product.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
if (!Configuration::get('your_module_name')) {
$this->warning = $this->l('No name provided');
}
}
public function install()
{
if (Shop::isFeatureActive()) {
Shop::setContext(Shop::CONTEXT_ALL);
}
if (!parent::install())
{
return false;
}
$defaultsettings = $this->getDefaultSettings();
$defaultsettings = serialize($defaultsettings);
Configuration::updateValue('your_module_name', $defaultsettings);
return true;
}
public function uninstall()
{
if (!parent::uninstall()) {
return false;
}
return true;
}
public function getContent()
{
$output = null;
$product = new Product($id_product);
$product->name = 'test';
$product->reference = '35GH';
$product->save();
}
Now in that getContent method you can perform operations on products.I hope it would help you.

How can i add content to the head section of prestashop so it appears where the hookHeader is located?

I am new to Prestashop and programming and I am trying to add some content to Prestashop 1.4.5.
I made a new simple module that hooks to the hookHeader. I got it working and it shows Hello World in the top of the shop. But when i open the site and open the source code I see it is added before the the doctype:
Hello World
My module php looks like this - I am not using a template:
if ( !defined( '_CAN_LOAD_FILES_' ) )
exit;
class primanetskintop extends Module {
function __construct()
{
$this->name = "skintop";
$this->tab = 'front_office_features';
$this->version = '0.1.0';
parent::__construct();
$this->displayName = $this->l('Insert skin top');
$this->description = $this->l('Skin - ikke slettes');
}
function install()
{
if (!parent::install() OR !$this->registerHook('header'))
return false;
return true;
}
function uninstal()
{
if (!parent::uninstall())
return false;
return true;
}
public function hookHeader($params)
{
echo "Hello World!";
}
Why does the hello world not show where the hookHeader is located? What am i doing wrong?
Thank you :D
It's because you can't use echo to add content to the header. Try:
public function hookHeader($params)
{
return "Hello World!";
}
You may want to analyze FrontControler.php from the classes dir to understand how this works.