Wordpress Woocommerce plugin mail triggering - wordpress

Mail not sending when status changed from processing to on-hold and processing to failed.
Kindly tell me how to achieve this. Thanks in advance

Even that kind of questions (directly asking the need without any research or any part of code) are not welcomed here, I want to help you about that by giving the basic idea.
In the file "wp-content/plugins/woocommerce/includes/class-wc-emails.php", search for the "public static function init_transactional_emails()" and check "$email_actions" array there.
$email_actions = apply_filters(
'woocommerce_email_actions', array(
'woocommerce_low_stock',
'woocommerce_no_stock',
'woocommerce_product_on_backorder',
'woocommerce_order_status_pending_to_processing',
'woocommerce_order_status_pending_to_completed',
'woocommerce_order_status_processing_to_cancelled',
'woocommerce_order_status_pending_to_failed',
'woocommerce_order_status_pending_to_on-hold',
'woocommerce_order_status_failed_to_processing',
'woocommerce_order_status_failed_to_completed',
'woocommerce_order_status_failed_to_on-hold',
'woocommerce_order_status_on-hold_to_processing',
'woocommerce_order_status_on-hold_to_cancelled',
'woocommerce_order_status_on-hold_to_failed',
'woocommerce_order_status_completed',
'woocommerce_order_fully_refunded',
'woocommerce_order_partially_refunded',
'woocommerce_new_customer_note',
'woocommerce_created_customer',
)
);
Since after every update of Woocommerce plugin any changes you made on those files will be gone, you need to add your email trigger for status changes you mentioned by either using a hook or overriding the files using your child theme.
About your request, for "from processing to on-hold" you need to add:
'woocommerce_order_status_processing_to_on-hold',
About overriding a file (or function) from includes folder of Woocommerce you may check this post: Override woocommerce files from includes folder
I hope this will help you to solve it. Have a good day.

Related

Send WooCommerce Email on Address Save using woocommerce_email_actions

I've been struggling with this for a couple days now and have been through every post/comment/discussion/etc... I could find trying to find a working solution.
I want to send an email via a custom class that extends WC_Email whenever a woocommerce user updates their address. I've found various resources explaining how to create custom wc emails (skyverge was most useful) and I have successfully done that. I have a plugin that adds a custom email in WP-Admin->WooCommerce->Settings->Emails.
If I use an action that is already part of the woocommerce_email_actions such as add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'trigger' ) ); and manually change the order status in the backend everything works just fine.
Problem is I want to use add_action( 'woocommerce_customer_save_address', array( $this, 'trigger' ) ); and unfortunately it never fires.
Based on some other threads I've tried adding the following to my main plugin file
function new_woocommerce_email_actions( $actions ){
$actions[] = 'woocommerce_customer_save_address';
return $actions;
}
add_filter( 'woocommerce_email_actions', 'new_woocommerce_email_actions' );
Supposedly this should allow me to use the action in my custom class, but no luck. I've also tried adding other actions without any success. For instance using the filter to add woocommerce_order_status_cancelled won't fire when manually changing the order to cancelled. I'm struggling to figure out why this isn't working and most of the the threads I've found are 2+ years old and dead, so here I am. Any help or pointers would be greatly appreciated.
Posting as an answer in case others are trying to get this functionality working.
I found this article from Tyche Softwares after extensive digging and was able to create a plugin that will send a custom WC_Email whenever a customer updates their address information.
I'm still not 100% as to why the add_filter('woocommerce_email_actions'... wasn't working, and if anyone can tell me why I'd still be interested.
This plugin works by calling a custom action defined in my extended WC_Email class via do_action whenever the existing woocommerce_customer_save_address action happens.

How to attach files to an email sent by a WordPress booking plugin?

I'm using a WordPress plugin for accepting online bookings (Appointment Hour Booking) and I need to attach a file to the emails sent after submitting the booking request (a PDF file with the general booking terms). I already applied a solution by editing the calls to the wp_mail() function in this way:
wp_mail(trim($payer_email), $subject, $message,
"From: ".$from."\r\n".
$content_type. "X-Mailer: PHP/" . phpversion(),
array(WP_CONTENT_DIR . '/uploads/agreement.pdf'));
The above works but everytime the plugin updates the file is overwritten and I've to reapply the code modification again. There is a better way to do that without being affected by the plugin updates or there is a way to prevent partially or completely a plugin update in WordPress?
Thank you in advance for any help.
Disabling the plugin update isn't a good idea, you may lost important compatibility or security updates. The way the call to the wp_mail() was modified also causes other attachment-related features stop working. The plugin you mention has a filter that can be used to modify the list of attached files, you can put the following code for example into your theme’s functions.php file:
add_filter( 'cpappb_email_attachments', 'my_attach_function', 10, 3 );
function my_attach_function( $attachments, $params, $form_id )
{
$attachments[] = WP_CONTENT_DIR . '/uploads/agreement.pdf';
return $attachments;
}
With the above code located out of the plugin files your file is added to the list of attachments without removing other attachments and locating the code out of the plugin files will prevent being overwritten by the plugin updates.
Your options are:
Fork the plugin and customize it to your needs.
Ask the team behind the plugin to implement a filter hook to allow customizing the headers passed to the wp_mail() function (so you can then attach files to e-mails).
Keep doing what you have been doing until now.
I like option two the best because:
It allows you to customize the behavior of the plugin from the outside, and,
Your changes will survive plugin updates.

Risks about edit the source code of a Wordpress plugin

I am making my first steps coding. I made some courses on Internet, and now I started to make a Wordpress theme to continue learning from the practice.
I find that there is a lot of Plugins that can help me to achieve the goals that I want, and I also found a plugin that makes almost everything that I want.
I started to modify the source code of that plugin so it could fits in my design scheme. Now I don't know if it is a good idea.
I didn't find a way to make a "child plugin" so at this moment I don't know if continue editing the source of this plugin, (that means that I would never update my plugin because I would lose all the modifications) or simply make everything by my own that would take me a lot more of time.
Do you have some suggestion?
You should take a look into world of hooks and filters.
This is nice place to learn from. Here is list of WP hooks where you can hook your code.
And Woocommerce hook example how to cusom validate field.
First part: woocommerce_checkout_process is place where to hook code and my_custom_checkout_field_process is name of your function.
/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['my_field_name'] )
wc_add_notice( __( 'Please enter something into this new shiny field.' ), 'error' );
}
Some plugins have template files, that you can put into child theme.
But sadly not all plugins are well customizable.

WordPress: Stop spam posts programmatically

I have an image sharing site built on WordPress and recently I've had a lot of bots registering a user and creating a spam post with links to various sites.
After installing WP-reCAPTCHA the numbers have reduced but there are still 'attacks' every hour or so.
I'm trying to handle this programmatically now, by hooking into wp_insert_post_data (which is called whenever a post/revision is saved). I inspect the post data and if it contains a link I remove the post's content and set the status to draft so that it isn't published.
But it's still a nuisance to delete spam users and posts from the back end.
Is there a better hook I can use to stop the saving of the post even happening? i.e. can I reject the call to save the post?
Here is the code I'm currently using:
function block_spam_posts($data, $postarr) {
// if the post contains a link, set it to draft status
$post_content = $data['post_content'];
if (strpos($post_content,'http') !== false) {
$data['post_content'] = 'Post data removed by anti-spam measures.';
$data['post_status'] = 'draft';
}
}
add_filter('wp_insert_post_data', 'block_spam_posts',1,2);
Thanks for your help.
I found the answer here:
https://wordpress.stackexchange.com/questions/82354/how-can-i-hook-into-creating-a-new-post-and-execute-wp-die-before-the-post-is
I'm using the right hook. All I need to do is call wp_die() once the criteria for a spam post has been met.
Hope this helps others.

How to properly modify 2 lines of another plugins

I am coding some permalinks rewrite for WooCommerce awesome plugin.
Everything is well packed in a plugin and work well... except for one thing.
WooCommerce use get_term_link() to display HTML link in default template. A lot of custom templates use it too. If I want to display the good link on my website, I must change one line of code in woocommerce plugin. This is dirty, since basic user would not do it. Since I want to list it on Wordpress plugin repertory, I would like some help here...
What I need to do :
Open woocommerce.php and replace line 767 :
OLD
$product_category_slug = empty( $permalinks['category_base'] ) ? _x( 'product-category', 'slug', 'woocommerce' ) : $permalinks['category_base'];
NEW
$product_category_slug = '';
Very easy to do by editing the file, but it is dirty. It is non user friendly, and it depends a lot of woocommerce next version... So my question here: how can I do it from my own plugin, without asking people to open their woocommerce plugin with Notepad++... Is there any way ?
I hope you understand my need and could provide me with an answer :)
I already did same thing in Past.
Please Don't Modify WooCommerce Core files, Because it will be gone in your next upgrade.
Use this Custom Permlink Plugin, You will get Better Options there,

Resources