Skip to content

Commit

Permalink
feat(API): added top_rated, best selling, latest, featured product an…
Browse files Browse the repository at this point in the history
…d featured vendor API
  • Loading branch information
sabbir1991 committed Oct 5, 2018
1 parent d704dbb commit 2a9e0d0
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 9 deletions.
228 changes: 221 additions & 7 deletions includes/api/class-product-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ class Dokan_REST_Product_Controller extends Dokan_REST_Controller {
protected $post_status = array( 'publish', 'pending', 'draft' );

/**
* Load autometically when class initiate
* Constructor function
*
* @since 2.8.0
* @since 2.7.0
*
* @return void
*/
public function __construct() {

# code...
}

/**
Expand Down Expand Up @@ -113,6 +115,96 @@ public function register_routes() {
),
) );

register_rest_route( $this->namespace, '/' . $this->base . '/(?P<id>[\d]+)/related', array(
'args' => array(
'id' => array(
'description' => __( 'Unique identifier for the object.', 'dokan-lite' ),
'type' => 'integer',
'required' => true,
),
'number' => array(
'description' => __( 'Number of product you want to get top rated product', 'dokan-lite' ),
'type' => 'integer',
'default' => 10
)
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_related_product' ),
),
) );

register_rest_route( $this->namespace, '/' . $this->base . '/top_rated', array(
'args' => array(
'number' => array(
'description' => __( 'Number of product you want to get top rated product', 'dokan-lite' ),
'type' => 'integer',
'default' => 8
),
'seller_id' => array(
'description' => __( 'Top rated product for specific vendor', 'dokan-lite' ),
'type' => 'integer',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_top_rated_product' ),
),
) );

register_rest_route( $this->namespace, '/' . $this->base . '/best_selling', array(
'args' => array(
'number' => array(
'description' => __( 'Number of product you want to get top rated product', 'dokan-lite' ),
'type' => 'integer',
'default' => 8
),
'seller_id' => array(
'description' => __( 'Top rated product for specific vendor', 'dokan-lite' ),
'type' => 'integer',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_best_selling_product' ),
),
) );

register_rest_route( $this->namespace, '/' . $this->base . '/featured', array(
'args' => array(
'number' => array(
'description' => __( 'Number of product you want to get top rated product', 'dokan-lite' ),
'type' => 'integer',
'default' => 8
),
'seller_id' => array(
'description' => __( 'Top rated product for specific vendor', 'dokan-lite' ),
'type' => 'integer',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_featured_product' ),
),
) );

register_rest_route( $this->namespace, '/' . $this->base . '/latest', array(
'args' => array(
'number' => array(
'description' => __( 'Number of product you want to get top rated product', 'dokan-lite' ),
'type' => 'integer',
'default' => 8
),
'seller_id' => array(
'description' => __( 'Top rated product for specific vendor', 'dokan-lite' ),
'type' => 'integer',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_latest_product' ),
),
) );
}

/**
Expand Down Expand Up @@ -301,13 +393,134 @@ public function get_product_summary( $request ) {
return rest_ensure_response( $data );
}

/**
* Prepare objects query.
/**
* Get related product
*
* @since 2.9.1
*
* @return void
*/
public function get_related_product( $request ) {
$related_ids = wc_get_related_products( $request['id'], $request['number'] );
$response = array();

if ( ! empty( $related_ids ) ) {
$objects = array_map( array( $this, 'get_object' ), $related_ids );

foreach ( $objects as $object ) {
$data = $this->prepare_data_for_response( $object, $request );
$data_objects[] = $this->prepare_response_for_collection( $data );
}

$response = rest_ensure_response( $data_objects );
}

return $response;
}

/**
* Top rated product
*
* @since 2.9.1
*
* @return void
*/
public function get_top_rated_product( $request ) {
$result = dokan_get_top_rated_products( $request['number'], $request['seller_id'] );
$data = array();
$response = array();

if ( $result->posts ) {
$objects = array_map( array( $this, 'get_object' ), $result->posts );

foreach ( $objects as $object ) {
$data = $this->prepare_data_for_response( $object, $request );
$data_objects[] = $this->prepare_response_for_collection( $data );
}

$response = rest_ensure_response( $data_objects );
}

return $response;
}

/**
* Best selling product
*
* @since 2.9.1
*
* @since 3.0.0
* @param WP_REST_Request $request Full details about the request.
* @return array
*/
public function get_best_selling_product( $request ) {
$result = dokan_get_best_selling_products( $request['number'], $request['seller_id'] );
$data = array();
$response = array();

if ( $result->posts ) {
$objects = array_map( array( $this, 'get_object' ), $result->posts );

foreach ( $objects as $object ) {
$data = $this->prepare_data_for_response( $object, $request );
$data_objects[] = $this->prepare_response_for_collection( $data );
}

$response = rest_ensure_response( $data_objects );
}

return $response;
}

/**
* Featured product
*
* @since 2.9.1
*
* @return array
*/
public function get_featured_product( $request ) {
$result = dokan_get_featured_products( $request['number'], $request['seller_id'] );
$data = array();
$response = array();

if ( $result->posts ) {
$objects = array_map( array( $this, 'get_object' ), $result->posts );

foreach ( $objects as $object ) {
$data = $this->prepare_data_for_response( $object, $request );
$data_objects[] = $this->prepare_response_for_collection( $data );
}

$response = rest_ensure_response( $data_objects );
}

return $response;
}
/**
* Latest product
*
* @since 2.9.1
*
* @return array
*/
public function get_latest_product( $request ) {
$result = dokan_get_latest_products( $request['number'], $request['seller_id'] );
$data = array();
$response = array();

if ( $result->posts ) {
$objects = array_map( array( $this, 'get_object' ), $result->posts );

foreach ( $objects as $object ) {
$data = $this->prepare_data_for_response( $object, $request );
$data_objects[] = $this->prepare_response_for_collection( $data );
}

$response = rest_ensure_response( $data_objects );
}

return $response;
}

protected function prepare_objects_query( $request ) {
$args = parent::prepare_objects_query( $request );

Expand Down Expand Up @@ -1417,6 +1630,7 @@ protected function save_default_attributes( $product, $request ) {
*
* @return array
*/

public function get_item_schema() {
$weight_unit = get_option( 'woocommerce_weight_unit' );
$dimension_unit = get_option( 'woocommerce_dimension_unit' );
Expand Down
4 changes: 4 additions & 0 deletions includes/api/class-store-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ public function get_stores( $request ) {
$args['order'] = $params['order'];
}

if ( ! empty( $params['featured'] ) ) {
$args['featured'] = $params['featured'];
}

$stores = dokan()->vendor->get_vendors( $args );
$data_objects = array();

Expand Down
8 changes: 6 additions & 2 deletions includes/wc-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ function dokan_seller_displayname ( $display_name ) {
* @param int $per_page
* @return \WP_Query
*/
function dokan_get_featured_products( $per_page = 9 ) {
function dokan_get_featured_products( $per_page = 9, $seller_id = '' ) {

$args = array(
'posts_per_page' => $per_page,
Expand All @@ -496,6 +496,10 @@ function dokan_get_featured_products( $per_page = 9 ) {
)
);

if ( !empty( $seller_id ) ) {
$args['author'] = (int) $seller_id;
}

return dokan()->product->featured( apply_filters( 'dokan_get_featured_products', $args ) );
}

Expand All @@ -507,7 +511,7 @@ function dokan_get_featured_products( $per_page = 9 ) {
* @param int $per_page
* @return \WP_Query
*/
function dokan_get_latest_products( $per_page = 9 , $seller_id = '' ) {
function dokan_get_latest_products( $per_page = 9, $seller_id = '' ) {

$args = array(
'posts_per_page' => $per_page,
Expand Down

0 comments on commit 2a9e0d0

Please sign in to comment.