Autoload module class in Prestashop - prestashop

I've created a module that has an override for the FrontControllerCore class to add additional Smarty variables.
The issue I'm running into is trying to autoload a class that is referenced in the controller that is in my module. The class isn't being loaded and I don't know how to add it to the autoloader.

When you install the module the FrontController.php file should be located in:
override\classes\controller\
so from the FrontController.php you can "include" manually that file like:
require_once(dirname(__FILE__).'/../../../modules/servicecharges/classes/ServiceCharge.php');
There's no autoload for such includes.

Also you can use this free tiny module that override Prestashop autoload. After that all your module custom class will be autoloaded.
Exemple path: /modules/my_module/libs/classes/MyClass.php
Extended Api

I've been able to solve a similar problem, with Composer's autoload. Way to require an autoload in one file on a Prestashop module?
Instead of overriding a controller (leading to conflicts with other plugins or installations of Prestashop already using the same overrides) you could invoke the hook moduleRoutes.
This way, you could call your autlooader always before the controllers:
<?php
public function hookModuleRoutes() {
require_once __DIR__.'/vendor/autoload.php'; // And the autoload here to make our Composer classes available everywhere!
}

Related

Prestashop 1.7.4.0 cannot load css and js files from hook, in custom module

I'm running Prestashop 1.7.4.0 and have built a custom module for posting comments.
I'm trying to load a css file and a javascript file each time the user visits the product page, where the module is being loaded.
public function hookDisplayProductComments($params) {
//...code
$this->assignProductComments();
return $this->display(__FILE__, 'displayProductTabContent.tpl');
}
In the function assignProductComments() i have:
public function assignProductComments() {
//...code
$this->context->controller->addCSS($this->_path.'views/css/mymodcomments.css');
$this->context->controller->addJS($this->_path.'views/js/mymodcomments.js');
//...code
}
The files are not only not loaded, but there is no trace of them in the page source either.
I mention that:
both of the files have some content in them
The directory structure is: /views/css/mymodcomments.css, and /views/js/mymodcomments.js, in the folder of the module
Either i leave these assets in the directory, or i delete them, the script still doesn't seem to try to load them
I tried:
cleaning the cache
forcing file compilation
uninstalling and reinstalling the module
Alright, so the answer was that the assets (js and css files) need to be loaded in the hookDisplayHeader() function.
So, you have to add a registerHook('displayHeader') in the custom module in install() function, and then uninstall and reinstall the custom module.

How to override prestashop core files

Can you please explain how to override following core files properly in presatashop. Prestashop 1.7.1.2 allowed only controllers, modal and tpl files in override folder. We can not override src folders in same way.
src/PrestaShopBundle/Controller/Admin/ProductController.php src/PrestaShopBundle/Resources/views/Admin/Product/form.html.twig
I don't believe this is possible in Prestashop 1.7. See their post here : it seems like they want you to use hooks, ie extending not overriding...
From PS 1.7.3, you can override src views and controllers in a module.
To override this template file:
src/PrestaShopBundle/Resources/views/Admin/Product/form.html.twig
You can add it in a module:
modules/<MY-MODULE>/views/PrestaShop/Admin/Product/form.html.twig
Doc PrestaShop for views in a module
And to override this controller file:
src/PrestaShopBundle/Controller/Admin/ProductController.php
You can add it in global override folder:
override/PrestaShop/Controllers/Admin/ProductController.php
Or in a module:
modules/<MY-MODULE>/override/PrestaShop/Controllers/Admin/ProductController.php
Doc PrestaShop for controllers in a module
PrestaShop give you the ability to override core files, you can check the : officiel doc

Prestashop 1.7 issue with "reset" "Exception thrown by module [module name] on install" and cannot find class_index.php,

I read many similar issues about "reset", or "uninstall" and "install" of a module being in development in Prestashop 1.7 (like here or there).
Basicaly I have overridden files in [prestashop project]/modules/[custom module]/override/classes/ and [prestashop project]/modules/[custom module]/override/controllers/front/. All my files respect convention:
<?php
class [Class to be overridden] extends [Class to be overridden]Core {
....
}
?>
In [prestashop project]/override/classes/ or [prestashop project]/override/controllers/front/ my files are copied from [prestashop project]/modules/[custom module]/override/... often with mistakes: like bracket {} mitakes (not existing in the source file) that are not opened/closed correctly making the class useless.
So, I delete manually the miscopied files in [prestashop project]/override/... and launch some "reset" action.
I'll add, that after manual delete I often see this issue in the Symfony log when trying to "reset" the custom module:
Exception thrown by module [module name] on install. Class [overriden file]OverrideOriginal_remove[some char chain] does not exist.
As explained in [prestashop project]/override/readme_override.txt:
Frequently Asked Questions
Q: I added an override file but it seems to be ignored by PrestaShop
A: You need to trigger the regeneration of the /cache/class_index.php file. This is done simply by deleting the file. It is the same when manually removing an override: in order to reinstate the default behavior, you must delete the /cache/class_index.php file.
But [prestashop project]/cache/class_index.php does not exists in Prestashop 1.7.
In Prestashop 1.7, class_index.php files are located in:
[prestashop project]/app/cache/dev or
[prestashop project]/app/cache/prod according if the project is set either in "dev" or "prod" mode.
For overriding a class in Prestashop 1.7 you have to copy the class file in [module_name]/override/classes directory during the module installation, and remove it on uninstall.
This is explained in further detail here.

Prestashop:Create a module that override another module?

I create a module and this module should also change another module (mailalerts) , I temporarily overridden this module into the folder /override/ , but I wish it was all in my module.
If you have
/modules/mymodule/override/modules/mailalerts/mailalerts.php,
then this file will be copied to
/override/modules/mailalerts/mailalerts.php,
after mymodule installation.
Be aware that making a module that overrides other modules is not good practice. You should only use module overrides for your own or your client website and place them directly in the /override/modules/ folder.

PrestaShop Using Default Module Instead of Theme Module

Today I starting design theme for prestashop and i'm Beginner on that.
i have a Question about Theme Module
i have Some module for my new Theme Located here:
prestashop/theme/mytheme/Module
and i want customize that for my new theme.
but the prestashop not using My Module instead using default module Located here:
Prestashop/Module/
What's Wrong and how to Fix this?
From the PrestaShop documentation:
Overriding a module's behavior
The modules are usually in the following format:
/modules/my_module/my_module.tpl
/modules/my_module/my_module.css
/modules/my_module/my_module.js
Since PrestaShop 1.5, they can and should also be in the following format:
/modules/my_module/views/templates/front/my_module.tpl
/modules/my_module/views/templates/front/my_module.css
/modules/my_module/views/templates/front/my_module.js
PrestaShop allows you to override or replace certain front-office module files
with new ones within the same theme. The override is governed by the
theme: once it contains a /modules folder (or more!), PrestaShop will
browse its content for files which have the same name and path as
those of existing modules, and replace these with the new ones.
This means, for PrestaShop 1.4-compatible modules:
/themes/my_theme/modules/my_module/my_module.tpl
/themes/my_theme/css/modules/my_module/my_module.css
/themes/my_theme/js/modules/my_module/my_module.js
Since PrestaShop 1.5, the path is slightly longer
/themes/my_theme/modules/my_module/views/templates/front/my_module.tpl
/themes/my_theme/css/modules/my_module/views/templates/front/my_module.css
/themes/my_theme/js/modules/my_module/views/templates/front/my_module.js
In general, the proper path to override a .tpl, .js or .css file
depends on the module's own path. That is the reason why if PrestaShop
1.5 has to work with a module without a view folder, it will need the same override path.
In short, you can keep overriding code in 1.5 just
as you did in 1.4.
The new files will be used when the customer loads
your shop.
Contrary to the override code that is to be placed manually
in the /override folder, module overrides are enabled as soon as the
module is installed. During installation, overriding code is merge
with those already in place (if any), otherwise they are copied to the
/override folder at the root of the PrestaShop folder.