call a function (actually an API call) whenever user place order - woocommerce

I need to call a function (actually an API call) whenever user place order(i.e after checkout).
I searched woo-commerce doc (https://docs.woothemes.com/wc-apidocs/hook-docs.html) but could not find proper hook.
Already i have wasted lot of time for this and client need this to be done.
If anyone can suggest anything then it will very helpful. I am new to woo-commerce.
Thanks for your time.

In your theme's function.php or in your custom plugin Place this:
add_action('woocommerce_payment_complete', 'my_function');
function my_function(){
// Whatever I want to do when payment is completed
// Like api call to external server etc
}
This action triggers when payment is completed. There are other actions available instead of woocommerce_payment_complete like woocommerce_order_status_pending , woocommerce_thankyou etc.
Hope that helps.

Related

Does wordpress clean up your action hooks?

I've written a Wordpress plugin. I hook to some actions from another plugin and everything works, but I want to know, when I deactivate my plugin, do these actions still get called and executed, or does Wordpress remove them automatically?
I tried removing actions on deactivate hook, but something doesn't make much sense to me. I'm using OOP.
When adding an action, I add the callable with an array like this in a registerActions function:
add_action('gform_validation_' . $form_id, array($this, 'formValidate'));
and remove it in unregisterActions function:
remove_action('gform_validation_' . $form_id, array($this, 'formValidate'));
My problem:
The registerActions and unregisterActions functions are called to different instances of the same class. Is remove_action actually removing the same action I've created?
I couldn't find anything discussing this on the internet and it really left me wondering.
Edit: I guess I could store the instances and actually call the registerActions and unregisterActions functions on the same instance. But how do I do it properly? I could also make action functions static.

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.

Notify real time wordpress

I have a wordpress site and I want to make a notify function. My issue is how to show the notification in realtime?
For example, when someone post a comment, it will show a popup for the owner immediately.
I am thinking of using ajax and recalling the request after x seconds but I don't know if this is a good idea.
For triggering a comment is done in WordPress, you can use this hook.
add_action( 'comment_post', 'show_message_function', 10, 2 );
function show_message_function( $comment_ID, $comment_approved ) {
//function logic goes here for showing your popup
}
Hope this will helps you.
for, more information.
Plugin API/Action Reference/comment post
Notification
Better Notifications for WordPress

Drupal - Load page on hook_cron run

I'm developing a drupal module. I'm using only 2 hooks (hook_menu, hook_cron)
In hook_menu, I create a menu callback that does a certain function.
In hook_cron, the problem resides here. I wanna execute the path I created in hook_menu every time hook_cron runs!
How can I do that?!!!
You can use something like this.
drupal_http_request(url('your/path', array('absolute' => TRUE));
It's however not clear to me why you can't simply call an API function in your cron hook, another page request has quite an overhead.

WordPress functions.php: how to apply update_option()?

I'm trying to set the default Image Link URL for my WP users so that it doesn't include the url link as a default. I've done some research, and I know the function is in the wp-admin/options.php:
update_option('image_default_link_type','file');
Rather than mess with the core files, I'd like to put this into the functions.php, but never know the proper way to implement stuff like this! This is what I have so far in my functions.php:
<?php
update_option('image_default_link_type','none');
?>
This obviously doesn't work: it needs the proper setup! What is the correct way to implement this in functions.php?
Also: I'd like to know the strategy for figuring out how to implement functions like this in the future by myself? For example, I never know whether or not I'm supposed to use add_filter or do_action, and how I need to pass the parameters. I've yet to find a book or post out there that explains this very well, and can show me by example. Any good leads on this would be awesome too!
Start with the Wordpress codex. Visit the plugin API (which is really what you are doing) that explains Hooks, Actions and Filters. Then see the Action Reference which provides your list of hooks.
Here you will find the hook update_option_OPTIONNAME. Description from codex:
Runs after a WordPress option has been update by the update_option
function. Action function arguments: old option value, new option
value. You must add an action for the specific options that you want
to respond to, such as update_option_foo to respond when option "foo"
has been updated.
Adding code from asker's comment:
function inventory_linkurl_setting() {
update_option('image_default_link_type','none');
}
add_action('admin_init', 'inventory_linkurl_setting'); ?>

Resources