Products_Controller::get_items( WP_REST_Request $request )

Summary

Retrieves a collection of products.


Parameters

$request

(WP_REST_Request) (Required) Full details about the request.


Return

(WP_REST_Response|WP_Error) Response object on success, or WP_Error object on failure.


Source

File: src/BigCommerce/Rest/Products_Controller.php

	public function get_items( $request ) {
		$mapper = new Query_Mapper();
		$args   = $mapper->map_rest_args_to_query( $request->get_params() );

		/**
		 * Filters rest products query.
		 *
		 * @param array            $args    Arguments.
		 * @param \WP_REST_Request $request request.
		 */
		$query_args = apply_filters( 'bigcommerce/rest/products_query', $args, $request );

		$query_args['post_type']      = Product::NAME;
		$query_args['post_status']    = 'publish';
		$query_args['posts_per_page'] = 12;
		if ( ! empty( $query_args['bigcommerce_id__in'] ) ) {
			$query_args['posts_per_page'] = - 1;
		}

		$channel_filter = $this->get_channel_filter( $request->get_param( Channel::NAME ) );
		add_action( 'pre_get_posts', $channel_filter, 9, 1 ); // run before Query_Filter::set_tax_query()

		$posts_query  = new \WP_Query();
		$query_result = $posts_query->query( $query_args );

		$posts = [];

		foreach ( $query_result as $post_id ) {
			$bcid = get_post_meta( $post_id, Product::BIGCOMMERCE_ID, true );
			$data = $this->prepare_item_for_response( get_post( $post_id ), $request );
			// ensure that we only have one result per BCID, no matter how many channels it's in
			$posts[ $bcid ] = $this->prepare_response_for_collection( $data );
		}

		$page        = (int) $query_args['paged'];
		$total_posts = $posts_query->found_posts;

		if ( $total_posts < 1 ) {
			// Out-of-bounds, run the query again without LIMIT for total count.
			unset( $query_args['paged'] );

			$count_query = new \WP_Query();
			$count_query->query( $query_args );
			$total_posts = $count_query->found_posts;
		}

		remove_action( 'pre_get_posts', $channel_filter, 9 );

		if ( $posts_query->query_vars['posts_per_page'] === - 1 ) {
			$max_pages = 1;
		} else {
			$max_pages = ceil( $total_posts / (int) $posts_query->query_vars['posts_per_page'] );
		}

		if ( $page > $max_pages && $total_posts > 0 ) {
			return new \WP_Error( 'rest_post_invalid_page_number', __( 'The page number requested is larger than the number of pages available.', 'bigcommerce' ), [ 'status' => 400 ] );
		}

		$response = rest_ensure_response( array_values( $posts ) );

		$response->header( 'X-WP-Total', (int) $total_posts );
		$response->header( 'X-WP-TotalPages', (int) $max_pages );

		$request_params = $request->get_query_params();
		$base           = add_query_arg( $request_params, rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );

		if ( $page > 1 ) {
			$prev_page = $page - 1;

			if ( $prev_page > $max_pages ) {
				$prev_page = $max_pages;
			}

			$prev_link = add_query_arg( 'page', $prev_page, $base );
			$response->link_header( 'prev', $prev_link );
		}
		if ( $max_pages > $page ) {
			$next_page = $page + 1;
			$next_link = add_query_arg( 'page', $next_page, $base );

			$response->link_header( 'next', $next_link );
		}

		return $response;
	}


User Contributed Notes

You must log in before being able to contribute a note or feedback.