[Special Summer Sale] 40% OFF All Magento 2 Themes

Cart

Woocommerce which hook to use for order status changes

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #9452
    ruvee
    Participant

    "I need to catch every time an order status changes from wc_on_hold to wc_completed"

    You would need to change your hook to woocommerce_order_status_changed.

    "I couldn’t get on Chrome’s Console that echo giving me the order price"

    You can’t use javascript and console.log in this hook. You would need to either use the die function or the error_log function to log it to your debug.log file.


    1- Using die function

    add_action('woocommerce_order_status_changed', 'so_status_completed', 10, 3);
    
    function so_status_completed($order_id, $old_status, $new_status)
    {
    
        $order = wc_get_order($order_id);
    
        //$order_total = $order->get_formatted_order_total();
        $order_total = $order->get_total();
    
        die($order_total);
    }
    

    2- Using error_log function

    add_action('woocommerce_order_status_changed', 'so_status_completed', 10, 3);
    
    function so_status_completed($order_id, $old_status, $new_status)
    {
    
        $order = wc_get_order($order_id);
    
        //$order_total = $order->get_formatted_order_total();
        $order_total = $order->get_total();
    
        error_log(print_r('order total: ' . $order_total, true));
    }
    

    Note:

    • In order for this to work, you would need to activate "debugging" mode in your wp-config.php file. So, navigate to your wp-config.php file and add the following lines to it!
      (NOT RECOMMENDED IN THE PRODUCTION)
    define('WP_DEBUG', true);
    define('WP_DEBUG_LOG', true);
    

    3- Creating a simple logger using file_put_contents function!

    add_action('woocommerce_order_status_changed', 'so_status_completed', 10, 3);
    
    function so_status_completed($order_id, $old_status, $new_status)
    {
    
      $order = wc_get_order($order_id);
    
      //$order_total = $order->get_formatted_order_total();
      $order_total = $order->get_total();
    
      // Simple logger 
      file_put_contents(dirname(__FILE__) . '/my_logger.txt', $order_total);
    }
    
    • If "my_logger.txt" does not exist, it’ll be created. If it already exists, then it’ll be overwritten!

    You could use "append" mode with your logger, if you need to! So your logger file won’t get overwritten.

    add_action('woocommerce_order_status_changed', 'so_status_completed', 10, 3);
    
    function so_status_completed($order_id, $old_status, $new_status)
    {
    
      $order = wc_get_order($order_id);
    
      //$order_total = $order->get_formatted_order_total();
      $order_total = $order->get_total();
    
      // Logger with "append" mode
      file_put_contents(dirname(__FILE__) . '/my_logger.txt', $order_total, FILE_APPEND);
    }
    

    Second question

    "since I am just here, I want to ask you if $order-get_total() is the right one to retrieve order amount without fee applied?"

    You might want to take a look at the $order->get_subtotal() for the totals before shipping, coupons and taxes:

    Please see these related answers:

    #9451
    luigino
    Participant

    In my custom plugin I need to catch every time an order status changes from wc_on_hold to wc_completed so I tried to write down:

    function so_status_completed( $order_id, $old_status, $new_status ) {       
        
        // if user is active , then get order amount and do all other personal calculations
        
        global $wpdb;
        $order = wc_get_order( $order_id );
        //$payment_method = $order->get_payment_method(); //returns payment method bacs,cheque,cod etc
        $user_id = $order->get_user_id();
        $total = $order->get_total();
        $order_data = $order->get_data(); 
        //$order_total = $order->get_formatted_order_total();
        $order_total = $order->get_total();
        
        echo '<script>console.log("Debug Objects: Check order_total ' . $order_total. '");</script>';
    
    }
    add_action('woocommerce_order_payment_status_changed','so_status_completed',10,1);
    

    But when I tried to change an order test from suspended to completed, I couldn’t get on Chrome’s Console that echo giving me the order price…..maybe using add_action isn’t the right way to put a listener to that event?

    Plus, since I am just here, I am using $order-get_total() which I searched on the net about his functionality but no deep docs found so I want to ask you if that method is the right one to retrieve order amount without fee applied?

    Thanks! Cheers!!!

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.