How to create a smarty variable in prestashop 1.5 - variables

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 {}.

Related

Prestashop 1.7 How to Display data from database to tpl

i'm new on Prestashop, i have a little problem on prestashop 1.7, i just bought a module for my orders, on the page of details when i launch smarty i can see the array $order_reference with some information, but when i go to the page of my history i can't access to this information, i don't know how to add this information to access it with my page history, do i have to create a function on history controller or class with dbclass or do u have any others solutions to add my details information of my order ?
The problem is my information is in a another table who call eo_orderreference and i can't access to it with my orders object. I think i need to write a function but as i see on internet it is wrong to write it on a tpl file, i have to do it on my class but i don't know how.
This is the information i want to add
SELECT 'order_reference' FROM eo_orderreference
Sorry i can't upload image because it contains sensible informations.
Thank for your time.
Smarty templates are meant to display data,
you should perform your query in the PHP file that recall your TPL and then use the Prestashop
assign()
method to return data to your TPL as smarty variables that you can show at front office.
See here for more details.
you can create the sql query within a function either within a module or a controller.
For example:
public function getAccessories($id_product)
{
// Your code
}
into a hook or initcontent, you can call the function on the query sql and assign it a template:
public function hookDiplayAccessoryExtraProduct($params)
{
$accessories = $this->getAccessories((int)$params['id_product']);
$this->context->smarty->assign(array(
'accessories_custom' => $accessories,
)
);
return $this->display(__FILE__, 'views/templates/front/accessory.tpl');
}

How to pass parameters to script file under shopify using php?

I want to pass parameters to script.js file that is included in the script_tags.json file of shopify.
I have mentioned below the code to add script file in script_tags.json file.
$script_array = array(
'script_tag' => array(
'event' => 'onload',
'src' => 'https://www.softwareprive.com/shopifyapps/ngdesk/js/script.js'
)
);
$scriptTag = shopify_call($token, $shop, "/admin/api/2019-10/script_tags.json", $script_array, 'POST');
You don't pass parameters to scripts. They execute after the DOM has loaded. So your script code can access data that was prepared for your script to use. That would mean providing some Liquid and/or other theme code. Otherwise, you are left with doing scripting by introspection of the DOM and hoping you sniff out the gold you are looking for.

Prestashop 1.6 Create Module to Display Carrier Filter

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.

Prestashop hook and override module

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

Opencart display one module conents into another module

I'm having custom footer module in my template. Also i'm using testimonial module in the position content bottom. I'm trying to display testimonial inside custom footer.
To do this i simply copied testimonial.tpl and testimonial.php contents and pasted into customfooter.tpl and customfooter.php
After this i'm getting errors stating
undefined variable and class name already assigned error
Did you know how to do this?
See this answer for how to do use a module in a separate modification/controller
opencart - How to manually display a module inside a template file?
You simply need to change from the common/home to your module's controller and view files
please define variable.
how can you define i will show you.
catalog > controller > header.php
for static variable
$this->load->language('common/header');
$data['text_home'] = $this->language->get('text_home');
// Where language file in assign this variable with different language foler
$data['text_home'] = $this->url->link('information/contact');
and you can tpl file in use this varible.
<?php echo $text_home; ?>
make sure 100% erorr has not facing
for dynamic variable