Are you looking to do something after the WooCommerce order is completed? You can use the woocommerce_order_status_completed
WooCommerce hook to accomplish that.
Here’s the usage of the hook:
/**
* Do something when the WooCommerce order is completed.
*
* @param int $order_id
*
* @return Return.
*/
function sa_wc_after_order_complete( $order_id ) {
// Do something.
return;
}
add_action( 'woocommerce_order_status_completed', 'sa_wc_after_order_complete' );
Example
The following example creates a new customer when the user’s order is completed.
/**
* Create a user when order is completed.
*
* @param int $order_id Order ID.
*/
function sa_create_user( $order_id ) {
$password = wp_generate_password( 16, true );
$user_name = $user_email = 'testuser@gmail.com';
$customer_id = wc_create_new_customer( $user_email, $user_name, $password );
}
add_action( 'woocommerce_order_status_completed', 'sa_create_user', 20, 2 );
The code snippets are usually added in the theme’s (preferably child theme’s) functions.php. Alternatively, an easier way is to use the Code Snippets plugin.
Details [For Geeks]
You can get the order instance by using $order_id
param.
$order = new WC_Order( $order_id );
Print of $order
[02-Mar-2022 03:21:31 UTC] WC_Order Object
(
[status_transition:protected] =>
[data:protected] => Array
(
[parent_id] => 0
[status] => completed
[currency] => NPR
[version] => 6.1.1
[prices_include_tax] =>
[date_created] => WC_DateTime Object
(
[utc_offset:protected] => 0
[date] => 2021-11-23 11:07:00.000000
[timezone_type] => 1
[timezone] => +00:00
)
[date_modified] => WC_DateTime Object
(
[utc_offset:protected] => 0
[date] => 2022-03-02 03:21:31.000000
[timezone_type] => 1
[timezone] => +00:00
)
[discount_total] => 0
[discount_tax] => 0
[shipping_total] => 0
[shipping_tax] => 0
[cart_tax] => 0
[total] => 40.00
[total_tax] => 0
[customer_id] => 0
[order_key] => wc_order_xKMLv3KtOfMa9
[billing] => Array
(
[first_name] => Do
[last_name] => explicabo
[company] => Meyer Estrada Trading
[address_1] => 901 South Cowley Street
[address_2] => Magnam deserunt ad a
[city] => Provident enim labo
[state] => Voluptas occaecat ip
[postcode] => RERUMVENIAMMINUS
[country] => IQ
[email] => xydima@mailinator.com
[phone] => +1 (138) 113-2018
)
[shipping] => Array
(
[first_name] =>
[last_name] =>
[company] =>
[address_1] =>
[address_2] =>
[city] =>
[state] =>
[postcode] =>
[country] =>
[phone] =>
)
[payment_method] => prabhu-pay
[payment_method_title] => Prabhu Pay
[transaction_id] =>
[customer_ip_address] => 127.0.0.1
[customer_user_agent] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36
[created_via] => checkout
[customer_note] => Dolores quo saepe es
[date_completed] => WC_DateTime Object
(
[utc_offset:protected] => 0
[date] => 2022-03-02 03:21:31.000000
[timezone_type] => 1
[timezone] => +00:00
)
[date_paid] => WC_DateTime Object
(
[utc_offset:protected] => 0
[date] => 2022-03-02 03:21:31.000000
[timezone_type] => 1
[timezone] => +00:00
)
[cart_hash] => 47a8328a6f65c2127cc857d7102090c3
)
[items:protected] => Array
(
)
[items_to_delete:protected] => Array
(
)
[cache_group:protected] => orders
[data_store_name:protected] => order
[object_type:protected] => order
[id:protected] => 1595
[changes:protected] => Array
(
)
[object_read:protected] => 1
[extra_data:protected] => Array
(
)
[default_data:protected] => Array
(
[parent_id] => 0
[status] =>
[currency] =>
[version] =>
[prices_include_tax] =>
[date_created] =>
[date_modified] =>
[discount_total] => 0
[discount_tax] => 0
[shipping_total] => 0
[shipping_tax] => 0
[cart_tax] => 0
[total] => 0
[total_tax] => 0
[customer_id] => 0
[order_key] =>
[billing] => Array
(
[first_name] =>
[last_name] =>
[company] =>
[address_1] =>
[address_2] =>
[city] =>
[state] =>
[postcode] =>
[country] =>
[email] =>
[phone] =>
)
[shipping] => Array
(
[first_name] =>
[last_name] =>
[company] =>
[address_1] =>
[address_2] =>
[city] =>
[state] =>
[postcode] =>
[country] =>
[phone] =>
)
[payment_method] =>
[payment_method_title] =>
[transaction_id] =>
[customer_ip_address] =>
[customer_user_agent] =>
[created_via] =>
[customer_note] =>
[date_completed] =>
[date_paid] =>
[cart_hash] =>
)
[data_store:protected] => WC_Data_Store Object
(
[instance:WC_Data_Store:private] => WC_Order_Data_Store_CPT Object
(
[internal_meta_keys:protected] => Array
(
[0] => _parent_id
[1] => _status
[2] => _currency
[3] => _version
[4] => _prices_include_tax
[5] => _date_created
[6] => _date_modified
[7] => _discount_total
[8] => _discount_tax
[9] => _shipping_total
[10] => _shipping_tax
[11] => _cart_tax
[12] => _total
[13] => _total_tax
[14] => _customer_id
[15] => _order_key
[16] => _billing
[17] => _shipping
[18] => _payment_method
[19] => _payment_method_title
[20] => _transaction_id
[21] => _customer_ip_address
[22] => _customer_user_agent
[23] => _created_via
[24] => _customer_note
[25] => _date_completed
[26] => _date_paid
[27] => _cart_hash
[28] => _customer_user
[29] => _order_key
[30] => _order_currency
[31] => _billing_first_name
[32] => _billing_last_name
[33] => _billing_company
[34] => _billing_address_1
[35] => _billing_address_2
[36] => _billing_city
[37] => _billing_state
[38] => _billing_postcode
[39] => _billing_country
[40] => _billing_email
[41] => _billing_phone
[42] => _shipping_first_name
[43] => _shipping_last_name
[44] => _shipping_company
[45] => _shipping_address_1
[46] => _shipping_address_2
[47] => _shipping_city
[48] => _shipping_state
[49] => _shipping_postcode
[50] => _shipping_country
[51] => _shipping_phone
[52] => _completed_date
[53] => _paid_date
[54] => _edit_lock
[55] => _edit_last
[56] => _cart_discount
[57] => _cart_discount_tax
[58] => _order_shipping
[59] => _order_shipping_tax
[60] => _order_tax
[61] => _order_total
[62] => _payment_method
[63] => _payment_method_title
[64] => _transaction_id
[65] => _customer_ip_address
[66] => _customer_user_agent
[67] => _created_via
[68] => _order_version
[69] => _prices_include_tax
[70] => _date_completed
[71] => _date_paid
[72] => _payment_tokens
[73] => _billing_address_index
[74] => _shipping_address_index
[75] => _recorded_sales
[76] => _recorded_coupon_usage_counts
[77] => _download_permissions_granted
[78] => _order_stock_reduced
)
[meta_type:protected] => post
[object_id_field_for_meta:protected] =>
[must_exist_meta_keys:protected] => Array
(
)
)
[stores:WC_Data_Store:private] => Array
(
[coupon] => WC_Coupon_Data_Store_CPT
[customer] => WC_Customer_Data_Store
[customer-download] => WC_Customer_Download_Data_Store
[customer-download-log] => WC_Customer_Download_Log_Data_Store
[customer-session] => WC_Customer_Data_Store_Session
[order] => WC_Order_Data_Store_CPT
[order-refund] => WC_Order_Refund_Data_Store_CPT
[order-item] => WC_Order_Item_Data_Store
[order-item-coupon] => WC_Order_Item_Coupon_Data_Store
[order-item-fee] => WC_Order_Item_Fee_Data_Store
[order-item-product] => WC_Order_Item_Product_Data_Store
[order-item-shipping] => WC_Order_Item_Shipping_Data_Store
[order-item-tax] => WC_Order_Item_Tax_Data_Store
[payment-token] => WC_Payment_Token_Data_Store
[product] => WC_Product_Data_Store_CPT
[product-grouped] => WC_Product_Grouped_Data_Store_CPT
[product-variable] => WC_Product_Variable_Data_Store_CPT
[product-variation] => WC_Product_Variation_Data_Store_CPT
[shipping-zone] => WC_Shipping_Zone_Data_Store
[webhook] => WC_Webhook_Data_Store
[report-revenue-stats] => Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore
[report-orders] => Automattic\WooCommerce\Admin\API\Reports\Orders\DataStore
[report-orders-stats] => Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore
[report-products] => Automattic\WooCommerce\Admin\API\Reports\Products\DataStore
[report-variations] => Automattic\WooCommerce\Admin\API\Reports\Variations\DataStore
[report-products-stats] => Automattic\WooCommerce\Admin\API\Reports\Products\Stats\DataStore
[report-variations-stats] => Automattic\WooCommerce\Admin\API\Reports\Variations\Stats\DataStore
[report-categories] => Automattic\WooCommerce\Admin\API\Reports\Categories\DataStore
[report-taxes] => Automattic\WooCommerce\Admin\API\Reports\Taxes\DataStore
[report-taxes-stats] => Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats\DataStore
[report-coupons] => Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore
[report-coupons-stats] => Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats\DataStore
[report-downloads] => Automattic\WooCommerce\Admin\API\Reports\Downloads\DataStore
[report-downloads-stats] => Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats\DataStore
[admin-note] => Automattic\WooCommerce\Admin\Notes\DataStore
[report-customers] => Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore
[report-customers-stats] => Automattic\WooCommerce\Admin\API\Reports\Customers\Stats\DataStore
[report-stock-stats] => Automattic\WooCommerce\Admin\API\Reports\Stock\Stats\DataStore
)
[current_class_name:WC_Data_Store:private] => WC_Order_Data_Store_CPT
[object_type:WC_Data_Store:private] => order
)
[meta_data:protected] => Array
(
[0] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 4890
[key] => is_vat_exempt
[value] => no
)
[data:protected] => Array
(
[id] => 4890
[key] => is_vat_exempt
[value] => no
)
)
)
)
That’s all. Are you also looking for a WooCommerce hook after the order is refunded from an admin panel?
WooCommerce hook after the order is completed
Hi bro,
I think this line have a problem, last 2 should be 1 I think as you pass only one parameter order ID, Right?
[php] add_action( ‘woocommerce_order_status_completed’, ‘sa_create_user’, 20, 2 ); [/php]
this should be
[php] add_action( ‘woocommerce_order_status_completed’, ‘sa_create_user’, 20, 1 );[/php]
Hi Andrita,
Thanks fo reaching out. Looks like you’re correct. 🙂
.
What specific data do you want? The idea is to take only what you need ignoring others.
‘order_id’ => $order->get_id(),
‘order_number’ => $order->get_order_number(),
‘order_date’ => date(‘Y-m-d H:i:s’, strtotime(get_post($order->get_id())->post_date)),
‘status’ => $order->get_status(),
‘shipping_total’ => $order->get_total_shipping(),
‘shipping_tax_total’ => wc_format_decimal($order->get_shipping_tax(), 2),
‘fee_total’ => wc_format_decimal($fee_total, 2),
‘fee_tax_total’ => wc_format_decimal($fee_tax_total, 2),
‘tax_total’ => wc_format_decimal($order->get_total_tax(), 2),
‘cart_discount’ => (defined(‘WC_VERSION’) && (WC_VERSION >= 2.3)) ? wc_format_decimal($order->get_total_discount(), 2) : wc_format_decimal($order->get_cart_discount(), 2),
‘order_discount’ => (defined(‘WC_VERSION’) && (WC_VERSION >= 2.3)) ? wc_format_decimal($order->get_total_discount(), 2) : wc_format_decimal($order->get_order_discount(), 2),
‘discount_total’ => wc_format_decimal($order->get_total_discount(), 2),
‘order_total’ => wc_format_decimal($order->get_total(), 2),
‘order_currency’ => $order->get_currency(),
‘payment_method’ => $order->get_payment_method(),
‘shipping_method’ => $order->get_shipping_method(),
‘customer_id’ => $order->get_user_id(),
‘customer_user’ => $order->get_user_id(),
‘customer_email’ => ($a = get_userdata($order->get_user_id() )) ? $a->user_email : ”,
‘billing_first_name’ => $order->get_billing_first_name(),
etc.
thank you so much 🙏
but lastly how can i get the transaction id?
You’re welcome. For transaction ID use:
$order->get_transaction_id();
How to get only few data from $order?
This looks fine… but where should the code go? A few more step by step instructions please… 🙂
Code Snippets should usually be inserted in theme’s (preferbly child theme’s) functions.php. Alternatively, the Code Snippets plugin (https://wordpress.org/plugins/code-snippets/) can be used.
I appreciate your feedback, though. I’ll note to make it more clear.