Search code examples
wordpresshook-woocommerce

Woocommerce hook triggered when the order is received and thanks the user


In my plugin I'd like to know how to "intercept" when the user completes an order paying by wire transfer because I need to know the amount of the order to calculate something else and write into another table. I thought that would be:

add_filter('woocommerce_thankyou_order_received_text', 'fn_payment_complete');

would that be the right way or is there a different way? Thanks! Cheers


Solution

  • woocommerce_thankyou_order_received_text is for returning msg. You can still use it to access order but i would recommend using woocommerce_thankyou instead.

    Using woocommerce_thankyou_order_received_text hook

    function wired_order_thank_you_msg($msg,$order) {
        //$order is object
        $payment_method = $order->get_payment_method(); //returns payment method bacs,cheque,cod etc
        if($payment_method === 'bacs'):
            //Do stuff
            $status = $order->get_status();
            $order_total = $order->get_formatted_order_total();
    
            //Return custom msg if you want
            $msg = array();
            $msg[] .= $status;
            $msg[] .= __('Custom msg','text');
            $msg[] .= $order_total;
    
            return sprintf('%s',implode($msg));
        else:
            return $msg;
        endif;
    
       
    }
    add_action('woocommerce_thankyou_order_received_text','wired_order_thank_you_msg',10,2);
    

    Using woocommerce_thankyou hook

    function wired_order_thank_you($order_id) {
        //$order is object
        $order = wc_get_order( $order_id );
        $payment_method = $order->get_payment_method(); //returns payment method bacs,cheque,cod etc
    
        if($payment_method === 'bacs'):
            //Do stuff
            $order_total = $order->get_formatted_order_total();
        endif;
    }
    add_action('woocommerce_thankyou','wired_order_thank_you',10,1);