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!!!
"I need to catch every time an order status changes from
wc_on_hold
towc_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.
die
functionadd_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);
}
error_log
functionadd_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:
wp-config.php
file. So, navigate to your wp-config.php
file and add the following lines to it!define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
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);
}
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);
}
"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: