最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rest api - Woocommerce custom endpoints

programmeradmin1浏览0评论

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
发布评论

评论列表(0)

  1. 暂无评论