I tried about 5 hooks to get the order hook for completed and the function doesn’t run at all but woocommerce_add_to_cart for example is working!
1. woocommerce_order_status_changed
2. woocommerce_new_order
I just make the alert to know if the function runs but the fucntion is quite large and I make order manullay as a test before deploying the plugin in this function in the plugin in index.php
function SP_order_token($order_id)
{
?>
<script>
alert("hello");
alert('<?php echo $order_id; ?>');
</script>
<?php
echo "hello";
global $woocommerce, $post;
echo $order_id;
$order = wc_get_order($order_id);
$order_data = $order->get_data(); // The Order data
var_dump($order_data);
}
// the final hook is when an order successfully paid
add_action('woocommerce_order_status_changed', 'SP_order_token',10,1);
2
Answers
Please replace with this and check again.
Your approach for debugging php is wrong. You can’t alert or do JS things on the server side. Also,
var_dump
andecho
will work but you don’t know where they gonna echo or dump the output.The correct way for php debugging will be to write your output in external files or in error logs. but I prefer writing in files on the root of wordpress.
Here is the code snippet that you can use:
Once your action will run in wordpress root you’ll find
debug.txt
with your expected output.