Updates, Tips & Tricks

Updates as Released, Plus My Personal Collection Of WordPress Best Practices

How to Hook Into WooCommerce to Trigger Something After an Order is Placed

14 April

It could be anything, but if you’re offering a service of some kind, rather than a simple downloadable product, you may need to trigger something to happen when an order is complete. Here’s a simple snippet illustrating how you can add an action to woocommerce_payment_complete. Add something like the following to your functions.php. You’ll need to grab the product_id from the admin side. I’ve also added in how you can retrieve the user’s information at the same time in case you need something from there as a part of your trigger. Enjoy!

add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($order_id) {
    $order = new WC_Order( $order_id );
    $myuser_id = (int)$order->user_id;
    $user_info = get_userdata($myuser_id);
    $items = $order->get_items();
    foreach ($items as $item) {
        if ($item['product_id']==24) {
          // Do something clever
        }
    }
    return $order_id;
}