rest api for custom post types in MarketPress

Hi,

It would be really useful if you could enable MarketPress CPTs for access from the REST API.

According to: http://developer.wordpress.com/2013/04/26/custom-post-type-and-metadata-support-in-the-rest-api/

you need to use the rest_api_allowed_post_types filter to allow access to them via the REST api.

This would be very useful for writing apps that interface with MarketPress.

Please consider implementing it.

Thanks

  • Vinod Dalvi

    Hi @jasonblewis,

    Greetings and thanks for posting on the forums.

    I think the rest_api_allowed_post_types is a Jetpack REST API filter which you can use to white list MarketPress eCommerce products post type if you want to by using the following code in the functions.php file of your child theme and there isn't any need to white list it by default by adding the code in the MarketPress plugin.

    function allow_marketpress_post_types($allowed_post_types) {
        $allowed_post_types[] = 'product';
        return $allowed_post_types;
    }
    add_filter( 'rest_api_allowed_post_types', 'allow_marketpress_post_types');

    Please advise.

    Kind Regards,
    Vinod Dalvi

    • David Thibault

      Hello,

      I tried this filter and it doesn't work for me. I added this to my plugin's functions.php:

      add_filter('rest_api_allowed_post_types',  'enableMpRestApi', 10, 1);
              function enableMpRestApi($allowedPostTypes) {
                  $allowedPostTypes[] = 'product';
                  return $allowedPostTypes;
               }

      But it doesn't add the products to the list of available REST API types. I can confirm this by going to this url:
      https://my-dev-box/wp-json/wp/v2/types
      and seeing the following types listed:
      post
      page
      attachment

      No product. Any advice?
      Thanks,
      Dave

      • David Thibault

        OK, according to this page:
        https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-rest-api-support-for-custom-content-types/

        I can add this code to my plugin:

        add_action( 'init', 'my_custom_post_type_rest_support', 25 );
        function my_custom_post_type_rest_support() {
          global $wp_post_types;
        
          //be sure to set this to the name of your post type!
          $post_type_name = 'planet';
          if( isset( $wp_post_types[ $post_type_name ] ) ) {
            $wp_post_types[$post_type_name]->show_in_rest = true;
            // Optionally customize the rest_base or controller class
            $wp_post_types[$post_type_name]->rest_base = $post_type_name;
            $wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
          }
        }

        and it should expose products to the REST API. It does. If I go to /wp-json/wp/v2 on my server I see the routes added for /wp/v2/products and subroutes. :

        /wp/v2/products
        namespace	"wp/v2"
        methods
        0	"GET"
        1	"POST"
        endpoints
        0	Object
        1
        methods
        0	"POST"
        args	Object
        _links
        self	"https://my-dev-box/wp-json/wp/v2/products"
        /wp/v2/products/(?P<id>[\d]+)	Object
        /wp/v2/products/(?P<parent>[\d]+)/revisions	Object
        /wp/v2/products/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)	Object

        The main /wp/v2/products route shows methods GET and POST. However, when I run my code it gives me the error "No route was found matching the URL and request method."

        Here's the request object I'm sending (printed with print_r):

        [12-Jul-2017 00:55:49 UTC] Running REST request WP_REST_Request Object
        (
            [method:protected] => POST
            [params:protected] => Array
                (
                    [URL] => Array
                        (
                        )
        
                    [GET] => Array
                        (
                        )
        
                    [POST] => Array
                        (
                            [art_piece_title] => title
                            [art_piece_desc] => asdf
                            [artist] => artist
                            [original_media] => oil on canvas
                            [original_qty] => 1
                            [original_height] => 1
                            [original_width] => 1
                            [original_depth] => 1
                            [original_weight] => 1
                            [original_currently_framed] => yes
                            [offer_unframed] => yes
                            [current_frame_width] => 1
                            [current_frame_depth] => 1
                            [current_frame_weight] => 1
                            [original_currently_matted] => no
                            [current_mat_width] =>
                            [current_mat_weight] =>
                            [original_price_unframed] => 1
                            [current_frame_price] => 1
                            [current_mat_price] =>
                            [original_price_framed] =>
                            [original_price_framed_matted] =>
                            [img1_image_hidden] =>
                            [img2_image_hidden] =>
                            [img3_image_hidden] =>
                            [original_price] => 1
                            [unframed_image_hidden] => 3870
                            [framed_image_hidden] =>
                            [corner_image_hidden] =>
                            [post_type] => product
                            [product_type] => art_piece
                            [title] => title
                            [content] => asdf
                            [excerpt] => asdf
                        )
        
                    [FILES] => Array
                        (
                        )
        
                    [JSON] =>
                    [defaults] => Array
                        (
                        )
        
                )
        
            [headers:protected] => Array
                (
                )
        
            [body:protected] =>
            [route:protected] => /wp/v2/products
            [attributes:protected] => Array
                (
                )
        
            [parsed_json:protected] =>
            [parsed_body:protected] =>
        )

        and here's the response I get:

        [12-Jul-2017 00:55:49 UTC] Rest response: WP_REST_Response Object
        (
            [links:protected] => Array
                (
                )
        
            [matched_route:protected] =>
            [matched_handler:protected] =>
            [data] => Array
                (
                    [code] => rest_no_route
                    [message] => No route was found matching the URL and request method
                    [data] => Array
                        (
                            [status] => 404
                        )
        
                )
        
            [headers] => Array
                (
                )
        
            [status] => 404
        )

        Any thoughts on why the POST method is not working? Overall, I'm trying to make an "easy dashboard" for my users and make a simpler interface for adding MP Products. Therefore, I need a way to create products via code. I thought the REST API might be the easiest way, but if you're aware of an easier way to call one of your functions to create the products, I'm all ears.

        Best,
        Dave

  • Vinod Dalvi

    Hi @jasonblewis,

    Thank you for your reply.

    I also think this can be added as an MarketPress option that will be disabled by default but can be enabled easily anytime.

    I will move this over to the features & feedback section. This way it can be considered for future development, and we can also see what other community members think about the feature request.

    Also, please include the Order type too - i imagine that 3rd party apps would find it very useful to be able to query order status

    Could you please explain it a bit more in detail?

    Best Regards,
    Vinod Dalvi

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.