Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandeepjain2015/47e265ee6a36ecd2cf1fb1c908dbf232 to your computer and use it in GitHub Desktop.
Save sandeepjain2015/47e265ee6a36ecd2cf1fb1c908dbf232 to your computer and use it in GitHub Desktop.
WordPress rest api for get all products by sorting like latest, price, rating or popularity
<?php
/**
* All Products By Sorting.
*
* @package :ewpapi
*/
/**
*
* Add an endpoint to show all products by sorting.
**/
class Ewpapi_All_Products_By_Sorting_REST_Controller {
/**
* Add an endpoint to show all products by sorting.
*/
public function __construct() {
add_action( 'rest_api_init', array( $this, 'products_by_sorting' ) );
}
/**
* Show all products by sorting.
*/
public function products_by_sorting() {
$route_namespace = apply_filters( 'ewpapi_route_namespace', 'ewpapi/v1' );
register_rest_route(
$route_namespace,
'/products-by-sorting',
array(
'methods' => 'POST',
'callback' => function( $data ) {
if ( empty( $data['number'] ) ) {
$post_per_page = 12;
} else {
$post_per_page = $data['number'];
}
$order = ! empty( $data['order'] ) ? $data['order'] : 'desc';
$orderby = ! empty( $data['orderby'] ) ? $data['orderby'] : 'date';
$args = array(
'post_status' => 'publish',
'post_type' => 'product',
'orderby' => $orderby,
'order' => $order,
'posts_per_page' => $post_per_page,
);
switch ( $orderby ) {
case 'price':
$args['meta_key'] = '_price';
$args['orderby'] = 'meta_value_num';
$order = ! empty( $data['order'] ) ? $data['order'] : 'asc';
$args['order'] = $order;
break;
case 'rating':
$args['orderby'] = 'meta_value_num';
$args['meta_key'] = '_wc_average_rating';
$order = ! empty( $data['order'] ) ? $data['order'] : 'desc';
$args['order'] = $order;
break;
case 'popularity':
$args['orderby'] = 'meta_value_num';
$args['meta_key'] = 'total_sales';
$order = ! empty( $data['order'] ) ? $data['order'] : 'desc';
$args['order'] = $order;
break;
}
$products_by_sorting = get_posts( $args );
if ( is_array( $products_by_sorting ) && ! empty( $products_by_sorting ) ) {
foreach ( $products_by_sorting as $products_by_sorting ) {
$product_id = $products_by_sorting->ID;
$image = get_the_post_thumbnail_url( $product_id );
$_product = wc_get_product( $product_id );
$regular_price = $_product->get_regular_price();
$sale_price = $_product->get_sale_price();
$price = $_product->get_price();
$short_description = wp_strip_all_tags( $_product->get_description() );
$items[] = array(
'id' => $products_by_sorting->ID,
'title' => $products_by_sorting->post_title,
'image' => $image,
'price' => $_product->get_price(),
'regular_price' => $_product->get_regular_price(),
'sale_price' => $sale_price,
'short_description' => $short_description,
'average_rating' => $_product->get_average_rating(),
'rating_count' => $_product->get_rating_count(),
);
}
return $items;
}
},
'args' => array(
'number' => array(),
'order' => array(
'validate_callback' => function( $param, $request, $key ) {
return is_string( $param );
},
'description' => 'Order of the products like asc or desc',
),
'orderby' => array(
'validate_callback' => function( $param, $request, $key ) {
return is_string( $param );
},
'description' => 'Order by the products like latest, price, rating or popularity',
),
),
'permission_callback' => function() {
return true;
},
)
);
}
}
$ewpapi_products_by_sorting = new Ewpapi_All_Products_By_Sorting_REST_Controller();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment