0

I'm creating a custom endpoint, that's basically this:

register_rest_route( 'wc/v3', '/custom-product-list(?:/(&P<custom_page>\d+))?(?:/(?P<custom_colors>\d+))?', array(
            'method' => 'GET',
            'callback' => 'custom_product_list',
            'args' => [
                'custom_page',
                'custom_colors'
            ],
        ));

What am trying to achieve is this: website.com/shop?page=2&colors=red,green,blue You know, like a normal URL that you can add stuff to it, and also have these things optional.

This however, doesn't work.

I decode it like this:

function custom_product_list( $args ) {
        global $product;


        $offset = 0;
        $limit = 9;

        $page = $args['custom_page'];
        $colors = $args['custom_colors'];

How should I go about doing this?

  • This should help: https://stackoverflow.com/questions/43986513/how-do-you-add-custom-fields-defined-in-posts-to-the-rest-api-responses-in-wordp – Fresz Jun 01 '20 at 10:31
  • Am not using custom fields. This is WooCommerce and I need these values so I can put them inside the WP WooCommerce Query. That there is totally unrelated to this. –  Jun 01 '20 at 10:53

1 Answers1

1

try add another parametr for args, like

'args' => array(
            'custom_page' => array(
                'required'          => true,

            ),
            'custom_colors' => array(
                'required'          => true,
            )
        ),
Eredmonkey
  • 53
  • 1
  • 6