skip to Main Content

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

2

Answers


  1. 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');
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search