skip to Main Content

We’re writing a WordPress plugin in which we retrieve Product/Order/Customer data from WooCommerce websites and storing it in our server, and incrementally updating it periodically for the changes. We’ve done this already for Products and Orders.

Now, we want to get the customer who updates their data after a specific date for the incremental update. I have tried it like below based on https://stackoverflow.com/a/64029143/698072. While it’s working for products and orders, it’s not working for customers.

add_filter('woocommerce_rest_customer_query', function(array $args, WP_REST_Request $request) {
        $modified_after = $request->get_param('modified_after');        
        if (!$modified_after) {
            return $args;
        }
        $args['date_query'][0]['column'] = 'post_modified';
        $args['date_query'][0]['after']  = $modified_after;
        return $args;
}, 10, 2);

Any help on this would be greatly appreciated.

2

Answers


  1. In woocommerce, for products and orders, the last modified date is stored in post_modified column of posts table. But for customers, it’s stored in the usermeta table with the key as last_update. So you have to filter it on the meta data like this,

    public function add_modified_after_filter_to_rest_api( $prepared_args, $request )
    {
        if ($request->get_param('modified_after')) {
            $prepared_args['meta_query'] = array(
                array(
                    'key'     => 'last_update',
                    'value'   => (int) strtotime($request->get_param('modified_after')),
                    'compare' => '>='
                ),
            );
        }
        
        return $prepared_args;
    }
    
    add_filter('woocommerce_rest_customer_query', 'add_modified_after_filter_to_rest_api', 10, 2);
    

    For products and orders, you can retrieve it like this, WooCommerce REST API v3 – Filter products by modified date using woocommerce_rest_product_object_query filter.

    Login or Signup to reply.
  2. The prior answer was crashing on my environment so, in case someone need an alternative solution, here it is:

    function add_modified_after_filter_to_rest_api( $prepared_args, $request )
    {
    
        if ($request->get_param('modified_after')) {
    
            global $wpdb;
            $datetime = (int) strtotime($request->get_param('modified_after')); 
                
            $SQL = $wpdb->prepare("SELECT user_id from {$wpdb->usermeta} WHERE meta_key = 'last_update' AND meta_value >= %d", $datetime);
            $users_id = $wpdb->get_col($SQL);
                
            $prepared_args['include'] = (array) $users_id;
        
        }
    
        return $prepared_args;
    }
    
    add_filter('woocommerce_rest_customer_query','add_modified_after_filter_to_rest_api', 10, 2);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search