order status hooks woocommerce code example
Example: woocommerce hook after order success
add_action('woocommerce_thankyou', 'enroll_student', 10, 1);
function enroll_student( $order_id ) {
if ( ! $order_id )
return;
if( ! get_post_meta( $order_id, '_thankyou_action_done', true ) ) {
$order = wc_get_order( $order_id );
$order_key = $order->get_order_key();
$order_key = $order->get_order_number();
if($order->is_paid())
$paid = __('yes');
else
$paid = __('no');
foreach ( $order->get_items() as $item_id => $item ) {
$product = $item->get_product();
$product_id = $product->get_id();
$product_id = $item->get_name();
}
echo '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';
$order->update_meta_data( '_thankyou_action_done', true );
$order->save();
}
}