1

I have been trying to implement a custom end point that inserts data from my mobile application into this database. However, I cant seem to find the correct way to do it. Some places used #wpdp->insert, while others used

$wpdb->get_results($query) and query would be "INSERT INTO 'wp_woocommerce_vendor' .....

none has worked for me. I get an error

There has been a critical error on your website.

Learn more about debugging in WordPress.

if possible, is there a way for me to check responses? such as, if insert is succesfull.
return $data['status']='Database added Successfully.';

add_action( 'rest_api_init', 'my_register_route' );

function my_register_route() {
    register_rest_route( 'my-route', 'my-phrase', array(
                    'methods' => 'POST',
                    'callback' => 'custom_phrase',
                )
            );
}

function custom_phrase() {

  global $wpdb;
    $data = array();

    $parameters = $request_data->get_params();
    $vendor_vendor_id = $parameters['vendor_vendor_id'];
    $vendor_product_id = $parameters['vendor_product_id'];
    $vendor_product_name = $parameters['vendor_product_name'];
    $vendor_product_qty = $parameters['vendor_product_qty'];
    $vendor_product_unit_price = $parameters['vendor_product_unit_price'];
    $vendor_product_amount = $parameters['vendor_product_amount'];
    $vendor_percent = $parameters['vendor_percent'];
    $vendor_amount = $parameters['vendor_amount'];
    $vendor_order_id = $parameters['vendor_order_id'];
    $vendor_order_date = $parameters['vendor_order_date'];
    $vendor_send_money_status = $parameters['vendor_send_money_status'];
    $vendor_send_money_date = $parameters['vendor_send_money_date'];
    $vendor_product_delivared = $parameters['vendor_product_delivared'];

    $table = 'wp_woocommerce_vendor';
    $data = array(
      'vendor_vendor_id' => $vendor_vendor_id,
      'vendor_product_id' => $vendor_product_id,
      'vendor_product_name' => $vendor_product_name,
      'vendor_product_qty' => $vendor_product_qty,
      'vendor_product_unit_price' => $vendor_product_unit_price,
      'vendor_product_amount' => $vendor_product_amount,
      'vendor_percent' => $vendor_percent,
      'vendor_amount' => $vendor_amount,
      'vendor_order_id' => $vendor_order_id,
      'vendor_order_date' => $vendor_order_date,
      'vendor_send_money_status' => $vendor_send_money_status,
      'vendor_send_money_date' => $vendor_send_money_date,
      'vendor_product_delivared' => $vendor_product_delivared
    );
    $wpdb->insert( $table, $data );

    $data['status']='Database added Successfully.';
    return rest_ensure_response( 'Hello World! This is my first REST API' );
}

Sources: https://www.tychesoftwares.com/creating-custom-api-endpoints-in-the-wordpress-rest-api/ https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/

1 Answer 1

0

function custom_phrase($request_data)

$request_data was missing.

Fixed it :)

3
  • Note that the database insert may have not succeeded, so you can do $data['status'] = $wpdb->insert_id ? 'success' : 'error'; or something like that. :) Also, please format code in your post. That would make it more readable or easier for people to notice. ;)
    – Sally CJ
    Commented Mar 21, 2020 at 2:59
  • Thanks! then i just return $data right? Commented Mar 30, 2020 at 2:33
  • Yes, that's right, but it's up to you on what data should be returned.
    – Sally CJ
    Commented Mar 30, 2020 at 4:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.