Answers for "woocommerce hook after order placed"

PHP
2

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 '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';
 
        // Flag the action as done (to avoid repetitions on reload for example)
        $order->update_meta_data( '_thankyou_action_done', true );
        $order->save();
    }
}
Posted by: Guest on April-08-2021

Code answers related to "woocommerce hook after order placed"

Browse Popular Code Answers by Language