Run PHP Code After Payment Complete On WooCommerce Store
WooCommerce is a popular WordPress plugin to add eCommerce functionality on a WordPress base site. The functionality and features of this plugin can be extended as there are lots of action hook available for this plugin.
In this post, I am going to tell you how to run your own PHP code after a successful payment on WooCommerce.
MSG91 WooCommerce SMS Integration Plugin
Protect Your PDF eBook Using This PDF Stamper WordPress Plugin
There is an action hook available for WooCommerce which is woocommerce_payment_complete. now create your own PHP function and add this function into the woocommerce_payment_complete action hook.
Below is a code example which implies the process of running your own PHP code after a successful payment:
<?php add_action( 'woocommerce_payment_complete', 'code_for_payment' ); function code_after_payment( $order_id ){ // Write your own PHP code that rns after payment success } ?>
In the above code, we have created a function code_after_payment and add this in the action hook woocommerce_payment_complete.
Protect Your PDF eBook Using This PDF Stamper WordPress Plugin
Now you can place your own PHP code at inside the function code_after_payment. After a successful payment for an order on WooCommerce store, the PHP code will be run.
Now I want to tell you that the above code snippets may be so useful for WordPress plugin developers to develop a WordPress plugin that works with WooCommerce plugin.
Hi, I want to ask you if that code will run automatically when the gateway send the update, and too where I get the $order_id, if that code runs in background?
Thank you
I have the same question as sebastain
Yes it should run
It might help if the `code_for_payment` call actually matched the `code_after_payment` function.
Hi, was anyone able to get this working? I need the same function, as I need to run PHP code that makes updates on an external database. Any help would be much appreciated.