rest api - Woocommerce custom endpoints

Currently, I'm an android developer(Java) and trying to add some custom endpoints to my Woocommerce site. I've

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/public_html/wp-content/plugins/woocommerce/includes/data-stores/class-wc-customer-data-store.php on line 235

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745002676a4605594.html

相关推荐

  • rest api - Woocommerce custom endpoints

    Currently, I'm an android developer(Java) and trying to add some custom endpoints to my Woocommerce site. I've

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信