Third party modules in mailalert on prestashop - php

I have a custom module that I'm using on prestashop 1.5.4.1 and I need to show some information on the new_order email.
I'm quite new to prestashop and it's the first time I'm messing with it.. I wanna know how I can get the variable of that module and show it on the email template.
I already found where the new_order mail variables are set, but don't know how I can add my new custom variable to it.
Any help will be appreciated. Thanks!

You should override the PaymentModule class and add your custom variable to the email template.
Copy the validateOrder method from classes/PaymentModule.php
Create override/classes/PaymentModule.php and paste the validateOrder method:
<?php
class PaymentModule extends PaymentModuleCore
{
//paste validateOrder here
}
Find the following line in validateOrder:
'{delivery_other}' => $delivery->other,
After that line assign your custom variable. For example:
'{delivery_date}' => $myDeliveryDate,
Remove /cache/class_index.php to clear the cache.
Copy order_conf.html and order_conf.txt from mails/en to themes/YOURTHEME/mails/en if they are not already there. This will prevent PrestaShop updates to overwrite your changes.
Add your custom variable to the new order_conf.html and order_conf.txt. For example:
<tr>
<td align="left">Delivery date: {delivery_date}</td>
</tr>

Related

What is the cleanest way to override a Wordpress plugins' hook instantiated with $this

I am trying to modify a Wordpress plugins' method without changing the plugin core code itself. I need to set some status flag in a different part of the website.
The method is defined in the plugin class like so:
add_action('plugins_action_name', [$this, 'local_method_name']);
The plugin method does not offer any actions or filters to hook into.
If this wasn't Wordpress but plain PHP I would write my own class, which extends the plugins' original class but has it's own method by the same name - but in Wordpress this gets me nowhere since I just move the problem from one plugin file to the next.
Is there a way to remove the plugins action altogether? And then redefine my own version of it?
Since the add_action has been called with $this - how would I remove it in some functions.php file?
Thanks
UPDATE
Indeed I want to learn how to handle different scenarios. Not expecting a one size fits all solution, but I do need to know how to avoid core changes, as each core-change complicates my deploy/update process exponentially.
Sure I will add some more details. My example plugin is WooCommerce Germanized Pro which comes with a WC_GZDP_Download_Handler::download() method which I needed to modify, in order to set some sort of "Warehouse downloaded the invoice" flag for each order. Which is later used to highlight new orders, filter orders that have been handed over to fulfilment etc.
There seems to be a Invoice Helper class which does in fact have a singleton pattern.
my modification in the download class is trivial:
public static function download( $pdf, $force = false ) {
... /* original code above, now my modifications */
if ($check_some_codition) {
$invoice = $pdf->get_invoice();
update_post_meta($invoice->get_id(), 'some-flag', 'some-value');
}
/* then resume original function (return the file to the browser) */
self::out( $filename, $file, $force );
}

How to use hooks in child theme to override a plugin?

I want to add some HTML code to a plugin but I want to do that in a child theme, the function that I wanna override it is in a file named as admin-functions.php
I want to add extra buttons to booked_render_custom_fields function
admin-functions.php:
function booked_render_custom_fields($calendar = false) { ?>
<button class="button">Text</button>;
<?php }
any suggestion? thanks
you can't really override a function like that. you can only use the filters and hooks that the plugin implements. but you got some options:
a) make a copy of the plugin, change it's name and make the changes you need. after that, it wont get updates anymore. you'll have to implement them manually.
b) you could add just one line to the original plugin and define a filter or hook to which you subscribe in your functions.php . so you only have to fix this one line after updating the plugin.
c) since the newer versions of wordpress, you have the possibilites to run your own code after any shortcode. so you could add or inject your own html into the output of the shortcode. https://developer.wordpress.org/reference/hooks/do_shortcode_tag/

How to override the fncatelog.php function in cscart

I need to override the fncatelog.php in cscart in my new addon.
My addon files :-
1. app/addons/newaddon/frontend/controllers/frontend/categories.post.php
2. design/themes/response/templates/addons/newaddon/overrides/blocks/product_filters/original.tpl
In func.php
I need to override the fn_get_filters_products_count() this function in my newaddon.
How can I do . Please help me.
You can not override the whole function. But you can use hooks to modify input arguments and returning argument.
In your case hooks are
get_filters_products_count_pre to modify input args,
get_filters_products_count_before_select_filters to change query param
To know more about cscart hooks please use this link - http://docs.cs-cart.com/4.3.x/developer_guide/core/coding_standards/hooks.html
You can use one of the following hook to modify data returning by this function:
get_filters_products_count_pre
get_filters_products_count_before_select_filters
get_product_filter_fields
You can read more about hooks system in CS-Cart at the http://docs.cs-cart.com/4.2.x/addons/hooking/index.html page.

How to extend Admin Cart rules controller?

I am working on a custom module that will allow to generate N of vouchers at the same time.
I need all the functionality of the current Cart Rules. I am looking at the AdminCartRulesController and ofcourse all the code is there.
How can I "extend" or copy and modify it so that I will add one more input to the form, and the loop adding to database by form value? Is it possible ?
You need to create the file:
override\controllers\admin\AdminCartRulesController.php
overriding the Core file the following way:
<?php
class AdminCartRulesController extends AdminCartRulesControllerCore
{
}
and there to override the methods you need.
Do not forget the clear the classes index (deleting cache/class_index.php) after adding the file.

widget within module in Yii

I'm trying to create a widget within the module and then load that widget from 'outside' of the module. More particularly I'm using user module written by someone else. I don't want to have a separate page for displaying a login form, therefore I tried to make a CPortlet/widget (confusion) displaying the login form. Basically, I've moved the code from LoginController into that widget. Then I try to display the widget on some random page by
<?php $this->widget('user.components.LoginForm'); ?>
However, I get an error
CWebApplication does not have a method named "encrypting".
in UserIdentity class in this line:
else if(Yii::app()->controller->module->encrypting($this->password)!==$user->password)
This happens, because I'm basically trying to execute this code within context of the app and not the module. Thus the "Yii::app()->controller->module" trick doesn't really work as expected.
What am I doing wrong:-\
Is there a better way to achieve this. I.e. display that login form in some other page, which is normally displayed by accessing login controller within user module (user/login) or is a widget the right way of doing it?
Thanks.
The quick solution
Ok, so I simply ended up doing
Yii::app()->getModule('user')->encrypting($this->password)
instead of
Yii::app()->controller->module->encrypting($this->password)
Notice that now the module must be called 'user' in the main config, but I think this allows for more flexibility. I.e. we're not bound to only use module functionality within the module.
Additional insight on displaying widget outside of the module scope
After playing more with it that's what I did. In the UserModule.php I've created a method
public static function id() {
return 'user';
}
Then everywhere where I need the module I use
Yii::app()->getModule(UserModule::id())->encrypting($this->password)
I don't like having many imports related to the module like:
'application.modules.user.models.*',
'application.modules.user.components.*',
Because we already have those imports in the UserModule.php:
public function init()
{
// this method is called when the module is being created
// you may place code here to customize the module or the application
// import the module-level models and components
$this->setImport(array(
'user.models.*',
'user.components.*',
));
}
Therefore whenever you know that some piece of functionality will be used outside of the module it's important to make sure the module is loaded. For example, in the LoginForm widget that I am trying to display NOT in one of the module controllers, I have this line of code:
$model = new UserLogin;
However, UserLogin is a model inside of the User module, and in order to be able to autoload this model we first have to make sure the module was initialised:
$module = Yii::app()->getModule(UserModule::id());
$model = new UserLogin;
I hope this will be helpful if you were stuck with the whole modules concept the way I was.
http://www.yiiframework.com/forum/index.php?/topic/6449-access-another-modules-model/ was useful but hard to find =)
You better move that encrypting() into a MyUserIdentiy class which extends CUserIdentity. Whatever the code you take to use, they putting the method in controller is a bad idea and as a result you cannot reuse that code.
The login form should still post to User/Login controller but I guess they use Yii's standard login code and you might want to modify it to use the MyUserIdentity.

Categories