1

Im trying to clean up some default database entries that WooCommerce adds to the postmeta table. The two primary entries that I do not need are _customers_ip_address & _customer_user_agent.

I found _customers_ip_address in create_order() function in file class-wc-checkout.php

do_action( 'woocommerce_checkout_create_order', $order, $data ); seems to be what is setting the data. Although I also found it was being set in wc-core-functions.php @ function wc_create_order()

Im not 100% sure how to edit this. Im thinking a simple do_filter, but unset seems to not work inside the do_filter, but obviously I am doing it all wrong. Im not that familiar with do_filter but seems like something simple like the code below.

function cleanup_woocommerce_checkout_create_order($order, $data) {
    unset($order->set_customer_ip_address());
    return $order;
}
add_filter('woocommerce_checkout_create_order', 'cleanup_woocommerce_checkout_create_order');

The code above gives a WordPress Error of :

Fatal error: Can't use method return value in write context

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
webbernaut
  • 303
  • 2
  • 11
  • Possible duplicate of [Can't use method return value in write context](https://stackoverflow.com/questions/1075534/cant-use-method-return-value-in-write-context) – ficuscr Aug 21 '19 at 21:24
  • Not a duplication, this is WooCommerce/WordPress specific. Im not asking about the error message, I just put in there to let anyone know the sample code I wrote doesn't work with do_filter as a WordPress function. – webbernaut Aug 21 '19 at 21:28

2 Answers2

1

First woocommerce_checkout_create_order is an action hook (but not a filter hook). Also you can not unset any method applied to an object as you are doing.

What you can do is to try setting an empty value, like:

add_action('woocommerce_checkout_create_order', 'cleanup_specific_order_metadata', 10, 2 );
function cleanup_specific_order_metadata( $order, $data ) {
    $order->set_customer_ip_address('');
    $order->set_customer_user_agent('');
}

It should work.

If it doesn't work, you can try to use woocommerce_checkout_update_order_meta action hook to remove this meta data afterwards once order data has been saved to database, this way:

add_action('woocommerce_checkout_update_order_meta', 'cleanup_specific_order_metadata', 10, 2 );
function cleanup_specific_order_metadata( $order_id, $data ) {
    delete_post_meta( $order_id, '_customer_ip_address' );
    delete_post_meta( $order_id, '_customer_user_agent' );
}

This last one should work anyways.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • The top add_action, didn't work. Your second method is a creative work around, and might be my last ditch effort if I can't find an official way to unset it from create order. As mentioned in a previous comment I actually found another place it's being set in wc_core_functions.php in function wc_create_order( ) which I will updated the original post to reflect. – webbernaut Aug 21 '19 at 22:13
0

As I can see in this article, https://docs.woocommerce.com/wc-apidocs/source-class-WC_Checkout.html#397

do_action( 'woocommerce_checkout_create_order', $order, $data );

so you should use add_action function

add_action('woocommerce_checkout_create_order', 'cleanup_woocommerce_checkout_create_order', 10, 2 );

function cleanup_woocommerce_checkout_create_order( $order, $data ) {
    $order->set_customer_ip_address(0);
}

Or updating the post meta

update_post_meta($order_id, '_customer_ip_address', 0);

or

delete_post_meta($order_id, '_customer_ip_address');
Ezequiel Fernandez
  • 954
  • 11
  • 18
  • I appreciate the response. I tried you idea but it's still populating the _customer_ip_address in the database upon checkout. I'll look over the apidocs link and see if maybe I can come up with something there. I also found it is getting set in wc_core_functions.php in function wc_create_order( ) so this one could be overwriting this add_action. – webbernaut Aug 21 '19 at 22:03
  • Have you tried updating the post meta after order is created? I have updated the answer – Ezequiel Fernandez Aug 21 '19 at 22:17
  • @ LoicTheAztec beat you to this one :) Both of you are thinking outside the box, which is awesome! And a very good approach that I might have to use if I don't find a way to unset on order creation. – webbernaut Aug 21 '19 at 22:21
  • haha yeah.. I had a look into the woocommerce class and set_customer_ip_address (0) should work but dunno! Updating the post meta is not something wrong. It's a wordpress funcion, you could use that! – Ezequiel Fernandez Aug 21 '19 at 22:24
  • Yes deleting after the fact is an option. It's an extra database query so would be nice to just unset it on the original database insert to save a little server processing. But so small I might have to end up using that approach. Thanks for your feedback. – webbernaut Aug 21 '19 at 22:27