1

Currently, I'm an android developer(Java) and trying to add some custom endpoints to my Woocommerce site. I've wasted so much time to understand concepts of WP, but still can't get clear answer. So the problem is that I need to make CRUD operation on customer (current customer) and his orders. I tried some code and it was successfull (Get method with customer's fields), but editing and deleting wasn't. So how could I use functions from Woocommerce, for example edit_customer() and delete_customer() from WC_API_Customers class? Here is my code:

class Custom_Rest_Controller extends WP_REST_Controller {

public function register_routes(){
$version = '1';
$namespace = 'woo/v' . $version;
$base = 'customer';

register_rest_route($namespace, $base, array(
    array(
        'methods'        => WP_REST_Server::READABLE,
        'callback'        =>  array($this, 'woo_customer_details'),
        'permission_callback'=> array($this, 'is_user_loggeding_in'), 
        ),
         array(
        'methods'        => WP_REST_Server::DELETABLE,
        'callback'        =>  array($this, 'woo_delete_customer'),
        'permission_callback'=> array($this, 'is_user_loggeding_in'), 
        ), 
        ));
}

public function is_user_loggeding_in($request){
     return is_user_logged_in();
}

function woo_customer_id(){
$currentuserid_fromjwt = get_current_user_id();
return $currentuserid_fromjwt;
}


function woo_customer_details(){
    $customer = WC() -> customer;
    $data = [];
    $data['username'] = $customer -> get_username();
    $data['first_name'] = $customer -> get_first_name();
    $data['last_name'] = $customer -> get_last_name();
    $data['billing_address'] = $customer -> get_billing();
    $data['shipping_address'] = $customer -> get_shipping();
    return $data;
}

function woo_delete_customer(){
   $val =  delete_customer(get_current_user_id());

   if ( is_wp_error( $val ) ) {
        return $val;
    }

   return new WP_REST_Response( $val, 200 );
}

Thanks in advance!)) So here is what I got from my logs:

2019-11-15T05:20:49+00:00 CRITICAL Call to undefined function wp_delete_user() in /home/test_site.com/public_html/wp-content/plugins/woocommerce/includes/data-stores/class-wc-customer-data-store.php on line 235
  • Is there any error showing up in your debugger? It could help us to help you better. – Jesús Franco Nov 14 '19 at 15:46
  • Thanks for your response, I'll try to get logs tomorrow)) Thank you so much! – Johny Deph Nov 14 '19 at 16:01
  • Please let me know as soon as you can. I'm about to get into woocommerce to support some mobile apps too and while I'm proficient with REST API, helping you gonna help me in the near future. – Jesús Franco Nov 14 '19 at 16:30
  • Oh, no problem!)) – Johny Deph Nov 14 '19 at 16:36
  • I updated my question! – Johny Deph Nov 15 '19 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.