0

I have created custom REST API fields using register_rest_field. I have added a column called basePrice and returns 0 on callback.

register_rest_field(
    'hotels',
    'basePrice',
    array('get_callback' => function () {
      return 0;
    })

I'm trying to add value from textbox whenever a user clicks "save" using AJAX. But the value still remains 0.

import $ from "jquery";
import Editor from "./Editor";

class Hotels {
  constructor() {
    this.editor = new Editor();
    this.events();
  }

  events() {
    $("form#addNewHotel").on("submit", this.addHotel.bind(this));
  }

  //Methods
  addHotel(e) {
    e.preventDefault();
    console.log("i'm here");
    let basePrice = $("#basePrice").val();

    console.log(deluxePrice);

    var newHotel = {
      title: $("#title").val(),
      content: "hello, it's working",
      basePrice: basePrice,
      status: "publish"
    };

    $.ajax({
      beforeSend: xhr => {
        xhr.setRequestHeader("X-WP-Nonce", travelData.nonce);
      },
      url: travelData.root_url + "/wp-json/wp/v2/hotels/",
      type: "POST",
      data: newHotel,
      success: response => {
        console.log("new hotel added");
        console.log(response);
      },
      error: response => {
        console.log("sorry");
        console.log(response);
      }
    });
  }
}

export default Hotels;

Please help

0 Answers0