when woocommerce order all hooks 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;
 
    // Allow code execution only once 
    if( ! get_post_meta( $order_id, '_thankyou_action_done', true ) ) {
 
        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );
 
        // Get the order key
        $order_key = $order->get_order_key();
 
        // Get the order number
        $order_key = $order->get_order_number();
 
        if($order->is_paid())
            $paid = __('yes');
        else
            $paid = __('no');
 
        // Loop through order items
        foreach ( $order->get_items() as $item_id => $item ) {
 
            // Get the product object
            $product = $item->get_product();
 
            // Get the product Id
            $product_id = $product->get_id();
 
            // Get the product name
            $product_id = $item->get_name();
        }
 
        // Output some data
        echo '

Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '

'; // Flag the action as done (to avoid repetitions on reload for example) $order->update_meta_data( '_thankyou_action_done', true ); $order->save(); } }

Tags:

Misc Example