- This topic is empty.
-
AuthorPosts
-
February 6, 2022 at 4:31 am #9452ruveeParticipant
"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
andconsole.log
in this hook. You would need to either use thedie
function or theerror_log
function to log it to yourdebug.log
file.
1- Using
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); }
2- Using
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:
- In order for this to work, you would need to activate "debugging" mode in your
wp-config.php
file. So, navigate to yourwp-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:
February 6, 2022 at 8:32 am #9451luiginoParticipantIn my custom plugin I need to catch every time an order status changes from
wc_on_hold
towc_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!!!
- In order for this to work, you would need to activate "debugging" mode in your
-
AuthorPosts
- You must be logged in to reply to this topic.