Prestashop - How to override another module in own module - php

I create own module. This module should override the BlockCart class.
I created a file /mymodule/override/blockcart/blockcart.php but this file is not executed.
I can not place this file in / override because the module must work on several shops and the instalation must works.
How I can do this?

You have to put your file in the right path of your module:
mymodule/override/modules/blockcart/blockcart.php
Then your blockcart.php should be like this:
class BlockCartOverride extends BlockCart
{
/* your stuff */
}
During the installation PrestaShop install your override.
Useful link:
- How to override modules

Related

PrestaShop 1.7 override src file in own module

I create own module and I want to override some file so in views folder I create folder PrestaShop and in it I add complete path to file like Admin/Catalog/Product/List/somefile.php and this override works, but when I want override file from src folder there is nothing happend. For example I want to override file public_html/src/Adapter/Product/PriceCalculator.php so in my module I have public_html/modules/mycustommodule/views/PrestaShop/src/Adapter/Product/PriceCalculator.php and this is not work. There is any possibility to override those files?
I think that is not possible to override this folder, you can't overrides of other things than classes/controllers/modules with /override/ directory is not available anymore.
You can reed informations about that in their post : planned to ovverride

Override core files using module prestashop

Actually I'm a newbie to prestashop,
and I have changes in the following file in these locations
classes/Product.php
src/PrestaShopBundle/Controller/Admin/ProductController.php
src/PrestaShopBundle/Resources/views/Admin/Product/form.html.twig
I have created a module named as My Kit while this module is configured all these changes should work..If the module is set to be disabled these changes should not affect in front end(I mean in admin panel)
For this I've created an override folder in my_kit module and place all those above mentioned files into this my_kit\override path..
like
my_kit\override\classes/Product.php
my_kit\override\src/PrestaShopBundle/Controller/Admin/ProductController.php
my_kit\override\src/PrestaShopBundle/Resources/views/Admin/Product/form.html.twig
But it won't work for me :(
how should I do this one..
Someone help me out of this..
You can use php default copy function in your main file install function like below.
public function install()
{
copy(_PS_ROOT_DIR_."/modules/your module name/classes/Product.php",_PS_ROOT_DIR_."/override/classes/Product.php");
unlink(_PS_ROOT_DIR_."/cache/class_index.php");//because you need to delete this file after override
//Do same like this all other file.
}
Thanks,
I'm not really sure how you will work around the part with the custom module (maybe when the module is not configured, you will leave the override classes only defined, and when its configured, you will put the changes there)... still changes in the core files should be done like this - for the Product class:
Make a new file in (basefolder)/override/classes/Product.php
Define the class ass:
class Product extends ProductCore
Do your changes. (override functions, or adding new ones)
You can't override classes from src folder
PrestaShop 1.7 introduces the use of namespaces with its new
architecture, and in short, anything that has namespaces cannot be
overridden. The legacy architecture can still be overridden, though.
But in general, we advise against overriding code. It is better to
extend it. Also, overrides are currently forbidden in the
Symfony-based pages (namely, the Product page and the Modules page).
http://build.prestashop.com/news/prestashop-1-7-faq/#is-there-any-change-planned-to-the-override-system
If you want to override PrestaShop's classes and controllers, you can do it as in previous versions
http://doc.prestashop.com/display/PS16/Overriding+default+behaviors

Override Prestashop 1.6.1.4

I am doing a module in Prestashop and I need override the method update in the class CartCore. I have created a class inside the folder override (I have tried in modules/name_of_module/override also) Cart extends CartCore (the file is Cart.php), and I have the next code.
public function update($null_values = false, $hook = true)
{
if (isset(self::$_nbProducts[$this->id])) {
unset(self::$_nbProducts[$this->id]);
}
if (isset(self::$_totalWeight[$this->id])) {
unset(self::$_totalWeight[$this->id]);
}
$this->_products = null;
$return = parent::update($null_values);
if($hook) Hook::exec('actionCartSave');
return $return;
}
I have deleted the file cache/class_index.php and I have activated the overrides in the back-office Performance but it does not work. Only works if I change the original Cart class, but I donĀ“t want do this.
Thanks!
When you create a new Override in a module Prestashop isn't aware of this new file. Deleting the /cache/class_index.php will only force Prestashop to look for new files in the root /overrides/ folder but not in your module /modules/your_module/overrides/ folder.
Your file needs to be placed under the root /overrides/ folder. This process is done automatically when you install your module. Every override files in your module is placed under the root /overrides/ folder. If you create a new override while your module is already installed, Prestashop will not move it for you.
You then have two possibilities. Uninstall and install your module. Or copying this file into the root /overrides/ folder and deleting the /cache/class_index.php file.
Also take into account Niclas Larsson advise to put this file under /overrides/classes/Cart.php.
Overriding default behaviors
Overriding a class
In order to override the Product class, your file needs to be called Product.php and must feature a Product class that then extends ProductCore class.
The file can be placed in either of these locations:
/override/classes/Product.php
/modules/my_module/override/classes/Product.php
Override files a separated in folders based on their types (classes, controllers, modules, ..)
So i think you are doing things right, but you need to add the folder 'classes' in override, and move your file into it.

Drupal's webform module isn't using the webform.api.php

I'm working with the Webform module of Drupal 7 and I'm trying to modify the hook_webform_submission_presave in the webform.api.php , but it seems that the module is not using this file because I've made modifications but doesn't change anything.
Do I have to say to Drupal in any place to use this file? Or what do I have to do?
First, I hope that you know that you shouldn't change module files directly, but to add hook function to your module and change that "hook" at beginning of function name with your module machine name.
Second, you have to clear all the caches so Drupal will re-scan your module and figure out that there is new hook function and start using it.
So, you have to create your own module first:
https://www.drupal.org/developing/modules/7
Don't be scared - It's just a folder with an info file describing your module and module file it self (in minimal case).
Then, if your module is called "anna" you should create a function inside your module file and name it:
anna_webform_submissions_presave()
And clear the cache - after that Drupal should start calling your hook function.
Modules don't use their .api.php files. These file are there for documentation purpose. That's the standard way for documenting hook definitions.

How to override controller in PrestaShop 1.5.x from a theme?

I want to override modules\blockwishlist\controllers\front\mywishlist.php
and more specifically
{
$this->display_column_left = false; (to be true)
parent::initContent();
$this->assign();
}
However i need to make it in a theme, so when users install the theme they don't need to place override file anywhere.
You can't override module controller in PrestaShop 1.5
Workaround: Clone the module with different name and do the necessary changes. When you're ready with the theme export it via the "Import/export a theme" module and pick that module from the "Modules" block ("Select the modules that you wish to export"). This way that module will be packaged along with the theme and will be installed when the theme is installed.
Note that that module is creating 4 tables during the installation and dropping them when unistalled.

Categories