5

I would like to extend the woocommerce rest api to include data of its 'booking' extension plugin. Currently this extension does not have default endpoints provided by the rest api.

So far I have created a plugin, and I've added the following code;

add_filter( 'woocommerce_rest_prepare_product', 'custom_data');

function custom_data($response, $object) {
        if( empty( $response->data ) )
            return $response;

        $response->data['meta_data'] = get_post_meta( $object[ID], 'availability', true);
        return $response;
    }

When I call the end point /products only the default data outlined by woocommerce is still called my little add on is no where to be found.

I don't even know where to find the above filter as I just saw this posted on a webpage and I tried to get it to do what I wanted, don't know if this is the correct direction to go down either. Webpage: https://francescocarlucci.com/woocommerce/woocommerce-api-custom-data-default-endpoints/#more-96

The above was me trying to extend the api but I also decided to try making a custom endpoint to see if I can get my desired outcome but so far I've just made a endpoint which calls but I have no idea what to write to retrieve the data I want.

custom end point code:

function register_custom_route() {
            register_rest_route( 'ce/v1', '/bookable',
                  array(
                    'methods' => 'GET',
                    'callback' => 'get_bookable'
                  )
          );
        }


        function get_bookable( ) {
            return array( 'custom' => 'woocommerce here' );
//What code do I write here :(

        }

Is there anyway I can achieve what I want under one of the above methods? I'm quite new to dev and I'm familiar with javascript not PHP hence my need to want to use the rest api as I would like to use wordpress/woocommerce as a headless cms.

So far the closets example I've come to has been shown on this question Creating WooCommerce Custom API

kadddeee
  • 498
  • 8
  • 20

3 Answers3

5

Alternatively you can try the below code to extend product response of WooCommerce REST API without doing additionally, as your above hook "woocommerce_rest_prepare_product" is for v1 but currently it was v3 so the hook for the latest is below one(the below hook is from v2 controller of rest api which is extended by v3).

add_filter('woocommerce_rest_prepare_product_object', 'so54387226_custom_data', 10, 3);

function so54387226_custom_data($response, $object, $request) {
    if (empty($response->data))
        return $response;
    $id = $object->get_id(); //it will fetch product id 
    $response->data['booking_meta_data'] = get_post_meta($id, 'availability', true);
    return $response;
}

I tested the above code it works perfectly fine. Hope it may helps to someone who search for similar solution.

Vignesh Pichamani
  • 7,950
  • 22
  • 77
  • 115
  • 1
    May I know how and where do you find the hook name 'woocommerce_rest_prepare_product_object' ? I am not able to find a list of available WC Rest API hooks for me to extend its capabilities. – Sem Aug 08 '21 at 12:17
2

this is only part of my code. some variable not defined. and this just concept. hopefully, you can modify as per your requirement.

    public function __construct() {     
    $this->template_url = apply_filters( 'woocommerce_template_url', 'woocommerce/' 
);
    $this->api_namespace = 'wc/v';
    $this->base = 'home';
    $this->api_version = '2';       
    add_action( 'woocommerce_loaded', array( $this, 'register_hooks' ) );
}

    $namespace = $this->api_namespace . $this->api_version;     
         register_rest_route(
            $namespace, '/wclogin/',
            array(
                'methods'  => 'GET',
                'callback' => array( $this, 'wc_login'),
        )
    );



   function wc_login($request){


    $user = get_user_by('email', $request["email"]);

    //bad email
    if(!$user){
        $error = new WP_Error();
        $error->add('invalid', __('<strong>ERROR</strong>: Either the email or password you entered is invalid.'));
        return $error;
    }
    else{ //check password
        if(!wp_check_password($request["password"], $user->user_pass, $user->ID)){ //bad password
            $error = new WP_Error();
            $error->add('invalid', __('<strong>ERROR</strong>: Either the email or password you entered is invalid.'));
            return $error;
        }else{
            return $user; //passed
        }
    }

} 
Shameem Ali
  • 181
  • 2
  • 7
  • Thank you for getting back to me! you see `get_user_by()` is that function a wordpress one or one you created? – kadddeee Feb 03 '19 at 22:05
  • Also where is your `register_hooks` function living? – kadddeee Feb 03 '19 at 22:23
  • `get_user_by()` wp function. – Shameem Ali Feb 07 '19 at 07:13
  • 1
    `public function register_hooks() { // REST API. add_action( 'rest_api_init', array( $this, 'rest_api_register_routes' ) ); add_filter( 'woocommerce_rest_prepare_product', array( $this, 'rest_api_prepare_brands_to_product' ), 10, 2 ); // WC 2.6.x add_filter( "woocommerce_rest_prepare_shop_order_object", array( $this, "get_product_order_image"), 10, 3 ); }` – Shameem Ali Feb 07 '19 at 07:20
  • Hey @shameem Ali P.K I was wondering if you could shed some insight into my new problem https://stackoverflow.com/questions/54673763/how-to-extend-woocommerce-api-to-show-bookable-product-availability-rules . You was of such great help before much appreciated! – kadddeee Feb 13 '19 at 23:57
0

Just an update for those that come to this question. Now, there is a rest api for the booking extension: https://docs.woocommerce.com/document/bookings-rest-api-reference/

Manuel
  • 9
  • 5
  • Links are great, but it would be even more helpful if you could summarize a possible way to correct the problem outlined in the question. – Shanteshwar Inde Jul 03 '19 at 07:03