Janusz Chudzynski Janusz Chudzynski - 1 year ago 71
PHP Question

Wordpress print_r or echo from within hook callback

I want to debug a function below. The print_r nor echo are not displaying anything though. How can I print the contents of:
$order or $paypal_args ?

function custom_override_paypal_email1( $paypal_args, $order ) {
// die();
global $woocommerce;

foreach ( $order->get_items() as $product ) {
$terms = get_the_terms($product['product_id'],'product_cat');
if (!is_null($terms)){
//get first one
$term = $terms[0];

$paypal_args['business'] = "xxx@xxx.example.com";
// $t = strval($term);
$paypal_args['business'] = $t ."yyy@yyy.example.com";
// break;
// $woocommerce->add_error( sprintf( "You must add a minimum of %s %s's to your cart to proceed." , 1, "T" ) );
return $paypal_args;
add_filter( 'woocommerce_paypal_args' , 'custom_override_paypal_email1', 10, 2 );

Whenever I run the function I am getting the following woocommerce notice:

SyntaxError: Unexpected token m in JSON at position 0

Answer Source

Because you are performing an Ajax request, the response will not be shown on the page. The echo or print_r() functions are working, but in order to see what is happening with Ajax responses you need to use the browser console.

You can access the console on most browsers by pressing F12. You will then be able to see the HTTP requests being made, and can examine what is happening with them.