I created a hook and I want to move there a module. I override the FrontController.php within the override folder (override/clases/controller/FrontController.php).
self::$smarty->assign(array(
‘HOOK_HEADER’ => Module::hookExec(‘header’),
‘HOOK_TOP’ => Module::hookExec(‘top’),
‘HOOK_NEWHOOK’ => Module::hookExec(‘DisplayNewHook’),
‘HOOK_LEFT_COLUMN’ => Module::hookExec(‘leftColumn’)
));
Then I edited the php file module to add:
function hookNewHook($params) { return $this->hookTop( $params ); }
And it works correctly. But is not there another way to do this? Because if the module is updated, the changes will be lost.
Thank you very much.
Module overriding is not possible until now. What you can do is to create a duplicate module (copying pasting :) ) and use that module with your changes. Any core module(s) you modify may lost the changes when you update the prestashop.
You'll find some help for creating a PrestaShop module there : http://doc.prestashop.com/display/PS14/Creating+a+PrestaShop+module
Related
I want to use an existing template in prestashop (product-list.tpl), i use this code in my module controller (method: initContent() ):
$this->setTemplate(_PS_THEME_DIR_ . 'templates/catalog/listing/product-list.tpl');
But prestashop return me that error :
[PrestaShopException]
No template found for C:\wamp64\www\prestashop/themes/classic/templates/catalog/listing/product-list.tpl
at line 68 in file classes/Smarty/TemplateFinder.php
Thank's you very much for you'r help !
The problem lies with the method setTemplate(), which is calling getTemplatePath(), a method that check if the desired template is located either in the module directory or in the module directory inside the current theme.
To make it work i would simply copy the product-list.tpl inside the directory :
"YourTheme"/modules/"Your module"/views/templates/front/
My Prestashop-based site is currently having an override for AdminOrdersController.php, I have placed it in override folder.
From the link provided below, it is perfectly working fine to add a Carrier filter which is not available in Prestashop 1.6 now. I have tried the solution and it is working perfectly.
Reference: Adding carrier filter in Orders page.
Unfortunately, for production site, I have no access to core files and unable to implement as such. Thus, I will need to create a custom module. Do take note that I already have an override in place for AdminOrdersController.php. I would like to tap on this override and insert the filter.
I have managed to create a module and tried placing an override (with the code provided in the URL) in mymodule/override/controller/admin/AdminOrdersController.php with the carrier filter feature.
There has been no changes/effect, I am baffled. Do I need to generate or copy any .tpl file?
Any guidance is greatly appreciated.
Thank you.
While the answer in the linked question works fine the same thing can be achieved with a module alone (no overrides needed).
Admin controllers have a hook for list fields modifications. There are two with the same name however they have different data in their params array.
actionControllernameListingFieldsModifier executes before a filter is applied to list.
actionControllernameListingFieldsModifier executes before data is pulled from DB and list is rendered.
So you can add fields to existing controller list definition like this in your module file:
public function hookActionAdminOrdersListingFieldsModifier($params) {
if (isset($params['select'])) {
$params['select'] .= ', cr.name';
$params['join'] .= ' LEFT JOIN `'._DB_PREFIX_.'carrier` cr ON (cr.`id_carrier` = a.`id_carrier`)';
}
$params['fields']['carrier'] = array(
'title' => $this->l('Carrier'),
'align' => 'text-center',
'filter_key' => 'cr!name'
);
}
Because array data is being passed into $params array by reference you can modify them in your hook and changes persist back to controller. This will append carrier column at the end of list.
It is prestashop best practice to try and solve problems through module hooks and only if there is really no way to do it with hooks, then do it with overrides.
Did you delete /cache/class_index.php ? You have to if you want your override to take effect.
If it still does not work, maybe you can process with the hook called in the AdminOrderControllers method with your new module.
I'm trying to figure out the best way to load a class into PrestaShop that I can use on overriding controllers.
I started off by creating a module that has an override of the Controller class with my files required at the top. Is there a better way to do this?
If you do not want to put class to override folder you can change class_index.php
Who add minus it is his problem but I'm right. If you do not want to use override folder you can set module folder with class
'SomeClass' =>
array (
'path' => 'modules/yourmodulename/class/class_name.php',
It is not normal but you can override without using override folder in particular circumstance.
And if you will read with carfull you will see: If you do not want to put class to override folder
EDITED 2015/10/19
It is craizy to add minus for what you do not know.
1) First look to PrestaShopAutoload class
2) all your class paths in array in the public $index in PrestaShopAutoload
3) what you need to do? just change this array
4) how to change this array? in your module you can get this instance using this PrestaShopAutoload::getInstance()
5) after you got instance you have full controll of your class paths!!! and you can add eny path what you need
6) what we need now? just make our module work maximum befour other class used. How we can do? Just hook in dispacher.
I am working on a button that switches view with onClick. I wish to store the last/default position in a variable in order to prevent switching to the default view state on each page refresh or navigation.
I read that I can do the following in a php file:
$myVar= -1;
$smarty->assign('myVar', $myVar);
and then use $myVar in the tpl file. But it does not work for me.
The tpl file I am working on is not part of a module and has no .php file in the prestashop root folder.
Can anyone educate me a little on smarty/php and how to create variables and use them to store button's state?
Thanks
Smarty is a PHP template engine for PHP, which facilitates the separation of presentation (XHTML/CSS) from the PrestaShop's core functions/controllers.
A template file (usually with a .tpl extension in PrestaShop) is always called by a PHP controller file (it can be a Front-end core controller or a module controller).
Example: /prestashop/controllers/front/ContactController.php
$this->context->smarty->assign(array(
'contacts' => Contact::getContacts($this->context->language->id),
'message' => html_entity_decode(Tools::getValue('message'))
));
$this->setTemplate(_PS_THEME_DIR_.'contact-form.tpl');
We can see that this file is retrieving information from the database and assigning it to Smarty.
Then, the 'contact-form.tpl' template will display it to the visitors.
The syntax is pretty similar for modules,
example:/prestashop/modules/blocklink/blocklink.php
public function hookLeftColumn($params)
{
$this->smarty->assign('blocklink_links', $this->getLinks());
return $this->display(__FILE__, 'blocklink.tpl');
}
Also, to store values in Smarty variables, you can use the 'assign' function in two ways:
$this->context->smarty->assign('my_smarty_variable_name', $my_value);
or if you have several variables:
$this->context->smarty->assign(array('my_smarty_variable_name1' => $my_value1), ('my_smarty_variable_name2' => $my_value2));
And then in the Smarty template:
The value of my variable is {$my_smarty_variable_name|escape:'htmlall':'UTF-8'}.
The 'escape' modifier is used to avoid XSS security issues.
In order to use variables in your smarty file, you need to use for example :
$this->context->smarty->assign(
array(
'myVar' => $myvar,
'otherVar' => $otherVar
)
);
Then to use it in your tpl file you simply need to use :
<div>my var = {$myVar}</div>
To use a variable in your smarty you need to write it inside {}.
I am new to spree. I want to make some changes in the views of spree and for that i found two methods:
1. Using Deface
2. By overriding the view
Currently, i am overriding the views but it was recommended that this approach is not very good. I want to use deface but unable to apply it:
Deface::Override.new(:virtual_path => "spree/checkout/registration",
:insert_before => "div#registration",
:text => "<p>Registration is the future!</p>",
:name => "registration_future")
Please help me how could i optimize my views?
Thanks in advance
You can use Deface to replace content and insert new content by creating a ruby file with your code inside the app/overrides folder in your project.
/app
/overrides
future_registration.rb
The future_registration.rb will have the code you pasted. If you visit the url spree/checkout/registration after restarting the server, you should see the message in the page.