1

I need to manipulate the data Woocommerce sends to PayPal in the 'Custom' Field. When Woocommerce creates the order and sends the data this can be found in

$order['custom']

This string is json encoded and I've got a filter I'm trying to use but it doesn't seem to be working. Here's what I've got:

add_filter('woocommerce_paypal_args', 'send_custom_paypal_data');
function send_custom_paypal_data($order) {
    error_log(print_r($order['custom'], true)); //log variable
    $order['custom'] = wp_json_encode(
        array(
            'field1' => 'some value',
            'field2' => 'more values'
        )
    );
    error_log(print_r($order['custom'], true)); //log value after updating variable
    return $order;
}

The first time I dump the $order['custom'] variable to the error_log I get the expected value from the Woocommerce order. But the 2nd time around, it's the same value, nothing has changed.

dstana
  • 350
  • 2
  • 10

1 Answers1

0

$order is the second parameter and not the first.

Also you should set the hook priority and number of parameters.

The woocommerce_paypal_args hook is defined in: /woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php.

Try with this:

add_filter( 'woocommerce_paypal_args', 'send_custom_paypal_data', 10, 2 );
function send_custom_paypal_data( $args, $order ) {

    error_log( print_r( $args['custom'], true) ); //log variable

    $args['custom'] = wp_json_encode(
        array(
            'field1' => 'some value',
            'field2' => 'more values'
        )
    );
    
    error_log( print_r( $args['custom'], true) ); //log value after updating variable

    return $args;

}

The code has been tested and works for me. Add it to your active theme's functions.php.

LOG REPORT

The first log returns:

[10-Apr-2021 08:54:04 UTC] {"order_id":145,"order_key":"wc_order_8FSKEF2hg2UVu"}

and the second:

[10-Apr-2021 08:54:04 UTC] {"field1":"some value","field2":"more values"}

It works well for me.

IF IT WAS NOT TO WORK

  • You're using the PayPal gateway included in the WooCommerce plugin, right?
  • Have you checked if there are any other functions overriding yours? Or that they have a higher priority?
  • Have you installed another PayPal plugin? In this case, deactivate it.

RELATED ANSWERS (using the woocommerce_paypal_args hook)

Vincenzo Di Gaetano
  • 3,892
  • 3
  • 13
  • 32
  • Thanks, will test and see if it works. The $order variable had the array of data I wanted, i still don't understand why it wouldn't update the value of that array field locally within the function – dstana Apr 11 '21 at 05:33
  • You are using `$order` as an array and not as an order object, **so correctly**. I advised you to add all the arguments to the function because, as you have seen, there really is a second `$order` parameter which is the order object. To avoid confusion it would be better to rename the `$order` variable if this is used to access PayPal order arguments and not the order object. – Vincenzo Di Gaetano Apr 11 '21 at 13:35
  • It might also be useful to show the logs in your file (first and second) as recommended by @gautamgolakiya. Post them in your question. – Vincenzo Di Gaetano Apr 11 '21 at 13:37