Send WooCommerce Email on Address Save using woocommerce_email_actions - wordpress

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.

Related

Wordpress Woocommerce plugin mail triggering

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.

user_register hook doesn't fire

I have designed a plugin and in a part of its functionalities I'm trying to save some data to a custom table in MySQL based on the ID of new registered user in Wordpress registration form. I used user_register hook as follow:
function my_func($user_id){
// some DB Query code based on $user_id
}
add_action('user_register', 'my_func', 10, 1);
I already tried solution in Wordpress user_register hook not executing?
and used profile_update hook as described there
but it couldn't help. I also tried changing priority value and removed it but the problem still exists.
The only thing I want is to have access to ID of the new registered user in registration form.
I appreciate if someone could help me.
I finally could find the solution. Actually in the body of myfunc function in the question I was trying to output the $user_id using javascript alert and for some reason that script doesn't run at that point but my query runs successfully and the way that I used the user_register hook is correct.
I just want to share it in case anybody encountered such a problem.
Thanks.

Which hooks does WooCommerce used to initiate saving post details and meta details?

I am struggling to know how WooCommerce initiate the saving of post and post meta from the dashboard? Please help me with this.
I was able to find the insert code here:
includes/class-wc-order.php
public function save() {
//statement
}
Thanks
May be this one can be helpful to others..
WooCommerce use post meta to add extra information on default WordPress post. Hence every time we update or create order from WordPress admin, WordPress initiate from this action...
add_action( 'save_post', array( $this, 'save_meta_boxes' ), 1, 2 );
Location: woocommerce/includes/class-wc-admin-meta-boxes.php
Everything happens from this hooks.
Note: As this is not related to aboven question but the best hooks to process any action on custom meta box is:
woocommerce_order_object_updated_props
Thanks

simple hook function not working

This is a function that fires after the user submits a gravity form in wordpress. Ive talked to their support team, and have gone thru adding a custom log inside the function, and from the systems report, the can tell the action is firing fin, but not the log inside the function. and i cant seem to find what is wrong with it.
I've tried adding the action inside an mu plugin, I've tried uninstalling all plugins, even tried a fresh install with only the gravity form plugin, and my modifications to the functions file.
Its a pretty straight forward function:
function survey_done( $entry, $form ) {
$user_id = get_current_user_id();
add_user_meta( $user_id, 'survey_complete', 'control' );
}
add_action( 'gform_after_submission_2', 'survey_done', 10, 2 );
If you are trying to insert the function you have shown then it might be due to the plugin loading first then gravity form plugin.
So, your hook will not be fired until the Gravity form plugin is loaded.

wordpress - catching save event with a plugin

i'm writing my first plugin for wordpress and i have some doubts regarding the hooks.
So i want an action to be executed when admin saves a post, and i'm using (inside a class):
add_action( 'save_post', array($this, 'save_post'));
function save_post(){
global $wp_query;
var_dump($wp_query);
}
the problem is that it prints the global variable when admin opens the "create a new post" and it doesn't when the user saves a post.
I want it to happen the other way around, but i can't find anything in the docs, and i'm completely alone here.
Any help? thanks
I think you're looking for the 'publish_post' action. There's also a 'draft_post' if you need it.

Resources