Skip to content

Commit

Permalink
Make method to get linked products.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aunshon committed Aug 13, 2024
1 parent 085ec21 commit 44e2f2b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
23 changes: 8 additions & 15 deletions includes/Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,22 +651,15 @@ public function json_search_product() {
wp_die();
}

$ids = dokan_search_seller_products( $term, $user_ids, '', true );

if ( ! empty( $_GET['exclude'] ) ) {
$ids = array_diff( $ids, (array) sanitize_text_field( wp_unslash( $_GET['exclude'] ) ) );
}

if ( ! empty( $_GET['include'] ) ) {
$ids = array_intersect( $ids, (array) sanitize_text_field( wp_unslash( $_GET['include'] ) ) );
}

if ( ! empty( $_GET['limit'] ) ) {
$ids = array_slice( $ids, 0, absint( $_GET['limit'] ) );
}
$product_objects = dokan()->product->get_linked_products(
$term,
$user_ids,
$_GET['exclude'] ?? '', // phpcs:ignore Squiz.WhiteSpace.SuperfluousWhitespace.EndLine, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$_GET['include'] ?? '', // phpcs:ignore Squiz.WhiteSpace.SuperfluousWhitespace.EndLine, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$_GET['limit'] ?? 0 // phpcs:ignore Squiz.WhiteSpace.SuperfluousWhitespace.EndLine, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
);

$product_objects = array_filter( array_map( 'wc_get_product', $ids ), 'dokan_products_array_filter_editable' );
$products = [];
$products = [];

foreach ( $product_objects as $product_object ) {
$products[ $product_object->get_id() ] = rawurldecode( $product_object->get_formatted_name() );
Expand Down
38 changes: 38 additions & 0 deletions includes/Product/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,4 +714,42 @@ public function top_rated( $args = [] ) {

return $products;
}

/**
* Returns linked products.
*
* @since DOKAN_SINCE
*
* @param string $term
* @param boolean|integer|array $user_ids
* @param integer $exclude
* @param integer $included_id
* @param integer $limit
*
* @return WC_Product[]
*/
public function get_linked_products( $term = '', $user_ids = false, $exclude = null, $included_id = null, $limit = 0 ) {
$term = ! empty( $term ) ? sanitize_text_field( wp_unslash( $term ) ) : '';
$user_ids = ! empty( $user_ids ) ? array_filter( array_map( 'absint', (array) wp_unslash( $user_ids ) ) ) : false;

if ( empty( $term ) ) {
wp_die();
}

$ids = dokan_search_seller_products( $term, $user_ids, '', true );

if ( ! empty( $exclude ) ) {
$ids = array_diff( $ids, (array) sanitize_text_field( wp_unslash( $exclude ) ) );
}

if ( ! empty( $included_id ) ) {
$ids = array_intersect( $ids, (array) sanitize_text_field( wp_unslash( $included_id ) ) );
}

if ( ! empty( $limit ) ) {
$ids = array_slice( $ids, 0, absint( $limit ) );
}

return array_filter( array_map( 'wc_get_product', $ids ), 'dokan_products_array_filter_editable' );
}
}

0 comments on commit 44e2f2b

Please sign in to comment.