What is the hook for the list of products? - prestashop

I need a hook that I can't find in the documentation, but logically it should exist.
In my module, I need to add some html in category page (or search results page) after the tile with the products but before pagination.
Which hook is best to use?

There is no default hook for your goal, but you can create your own one and use it within your module. The inconvenience is that you will be able to use it only in modified themes or you will need to add it manually to all new ones. To create the desirable hook you need to put
{hook h='displayYourHook' info=$someData}
in the top of your_theme/templates/catalog/_partials/products-top.tpl
and then just use it like a default hook within your module
public function hookDisplayYourHook($params)
{
// $params can be some information. ID of category for example
do all necessary stuff here
}
and also do not forget to register your hook during the module installation
public function install()
{
....
&& $this->registerHook('addproduct')
....
}
Also, I presume that you use prestashop 1.7.* if not - some code can be different

Related

docusaurus 2: run custom script for every page

Is it possible to run some custom script for every page?
E.g., I want to run alert(1); on every page. How can I do that without the sizzling of any components?
I know it can be done by creating jsx component and using it in every .mdx file (but every doc then should be a .mdx file). So it's not the thing I'm looking for.
Docusaurus 2 user here! 👋
Docusaurus is Server-Side Rendered and then hydrated to function as a Single Page Application. Without knowing more about what you want to achieve, I can only try to give you a general advice.
One way of achieving this is to create your own plugin, it gives you access to the execution context, such as router events.
I currently use this for analytics reporting when the user changes page. It's not yet documented, but there's a good example in the Docusaurus 2 repository in the docusaurus-plugin-google-analytics package.
Here's a fragment of what I use, this only executes when a new page is loaded, which fits my use case perfectly. There may be another lifecycle hook called when the page is hydrated that I haven't found yet.
analytics-module.js
import ExecutionEnvironment from "#docusaurus/ExecutionEnvironment";
export default (function () {
if (!ExecutionEnvironment.canUseDOM) {
return null;
}
return {
onRouteUpdate({ location }) {
_paq.push(["setCustomUrl", location.pathname]);
_paq.push(["setDocumentTitle", document.title]);
_paq.push(["trackPageView"]);
},
};
})();

How to display theme's .tpl in module (in hook)?

I am making a module, that suposed to use templates from my custom theme.
The problem is that I don't understand the function Module::display(), it gets two arguments "file" and "template", what is the file? What is it for?
If I do this:
public function hookDisplayHome{
return $this->display(__FILE__, '../../templates/my_custom_theme/mb_templates/aboutus.tpl');
}
It displays error "Not found template file" in module "my_module". Of course if I change the template path to one of the templates of my module, then it works. But I need to access theme's template, not my module's.
With this I have also another additional question. Is there any documentation on Prestashop's functions? I really tried to analyse the function $this -> display() but couldn't find any place where is described what are the arguments of this function etc. There is Prestashop documentation https://devdocs.prestashop.com but it is very general, and has no description of the functions.
I've checked the class Module.php, the function has no comments :(
What is the official way when you want to check what the function does, returns, and what parameters accepts? It has to be somewhere, right?
So for those who also struggle with this problem, based on this answer https://stackoverflow.com/a/53576139/2796533
The easiest solution seems to be using module's template in $this -> display() and then in that template include the theme's template:
{include file='../../../themes/my_custom_theme/mb_templates/aboutus.tpl'}

Vuejs have multiple components sharing variables and functions

Right this is more about practice, I haven't really got a working scenario but I can provide an example. Imagine Facebook messenger, you have a chat widget in the header where you can quickly view and respond to messages. You can also click view all to take you off to the messages page.
So to me I can see myself having two Vue components, they're more sibling than parent and child as one would be used on all pages and the other simply on the messages page.
<chat></chat> and <chat-widget></chat-widget>
Now from what I can see, across the widget and the chat window itself is functions that would operate in the same way, maybe they'll have slightly different templates because of where they are loaded but somethings straight of the bat would be:
messages() {}
compose() {}
validate() {}
These are just some examples as I use Laravel as my backend I would be using axios to send requests between my Vue frontend and my backend (database).
Ideally, I wouldn't want these components to duplicate it's functions, so should I simply duplicate them or is there a way where i can store some sort of parent functions?
One problem is because of async ajax requests I can't simply call a function that returns say the messages for me to bind, at least, I don't think I can.
Just looking for some guidance on how I can best do this with Vue so that I'm not duplicating identical functionality within my components.
You can use composition to create a "base/abstract" component from which other components can extend:
https://v2.vuejs.org/v2/api/#extends
var CompA = { ... }
// extend CompA without having to call `Vue.extend` on either
var CompB = {
extends: CompA,
...
}
You can inherit the functionality from the "base/abstract" class or override parts of the functionality of the parent.
Another thing you could do is to create mixins:
https://v2.vuejs.org/v2/guide/mixins.html#ad
This is helpful if your components need to share functionality but are not largely the same. If you were to use extend in those cases you would likely override the majority of base functionality to these are a better in that case.
Of course nothing would stop you from simply extracting commonly used methods into a separate file and importing them in your components directly.

Extend products with custom module in Prestashop

I am writing a module, I know how to build a global module, but I would like to do the following:
Have global properties (already covered)
Have the same properties in a tab in the products interface in the admin where the user can override the global properties (still to do)
Now my question:
How do I add an additional tab to the products interface in the prestashop admin?
I guess I can easily call these in my module templates and check if global is overridden?
Thanks for the feedback,
Eric
PS. Using prestashop 1.5.8, will worry later to extend to 1.6
To add an additional tab to product edit page, you can use hookDisplayAdminProductsExtra.
First, you will have to register this hook inside install() method:
public function install() {
...
$this->registerHook('displayAdminProductsExtra')...
...
}
after that you define this:
public function hookDisplayAdminProductsExtra($params) {
...
return $this->display(__FILE__, 'views/admin/yourtemplatefile.tpl');
}
File yourtemplatefile.tpl defines the contents of your extra tab. Extra variables used in this file can be assigned inside hookDisplayAdminProductsExtra function
Here you can find additional information about creating a module:
http://doc.prestashop.com/display/PS15/Creating+a+PrestaShop+module
And here you can find information about hooks:
http://doc.prestashop.com/display/PS15/Hooks+in+PrestaShop+1.5

Prestashop - override function in existing prestashop module

I would like to change existing prestashop module without copying it and creating new one. I know that it is possible to override .tpl files in prestashop, but is it possible to do same thing with php classes? For instance I would like to change blockcart so that it can be hooked on top. Since original version doesnt have that hook I need to change install() function! I can`t change original source (it would be bad idea isn't it...) file I need to override install() function by inheriting blockcart module. Is it possible to do so and where I can find example?
I use my own override to the FrontController class to allow the display of module output at arbitrary points in tpl files - this means that the module doesn't need to support a particular hook. It is implemented via a smarty plugin, so you can for example use:
{plugin module='blockcart' hook='rightColumn'}
The above will force the module to output what it would display if hooked to the right column where the above is tag inserted (which can be anywhere in any tpl file). You can "unhook" the module from the right column so that it only displays where you want it to using this technique. I have used it on a production site with great success.
There's a series of articles describing how it works (with the required code) available at:
Prestashop 1.4 Plugins
In Prestashop 1.4 you can override core classes and module templates
Today this is not possible to override a module php file but we are working on it.
in override\modules\blockcart\blockcart.php (create it if it does not exist yet)
<?php
class BlockCartOverride extends BlockCart
{
public function hookDisplayTop($params)
{
return parent::hookTop($params);
}
}
?>
like this you can override any module to be hookable on any default or custom hook.
don't forget to delete cache/class_index.php for the override to work :)
Since version 1.6.0.11 of PrestaShop, there is a new feature that allows developers to override a module’s instance classes.
Override a module’s instance class by extending it
To override a module’s instance class, you have to extend it, giving the extended class the same name and adding Override suffix:
<?php
if (!defined('_PS_VERSION_'))
exit;
class BlockUserInfoOverride extends BlockUserInfo
{
public function hookDisplayNav($params)
{
return '<div class="header_user_info"><a>Test</a></div>';
// return $this->display(__FILE__, 'nav.tpl');
}
}
Source: http://build.prestashop.com/howtos/module/how-to-override-modules/
Keep in mind that in 1.7.x era - nowadays - you can override module main classes but not controllers. To be able to override controllers you have to override the core classes (to detect any possible overrides) and then do whatever you like. Alternatively, you have to get the original files as backup and put the modified in the same place on install and the reverse procedure on uninstall.