WC_AJAX::json_search_products
Search for products and echo json.
Method of the class: WC_AJAX{}
Hooks from the method
Returns
null. Nothing (null).
Usage
$result = WC_AJAX::json_search_products( $term, $include_variations );
- $term(string)
- Term to search for.
Default:'' - $include_variations(true|false)
- in search or not.
Default:false
WC_AJAX::json_search_products() WC AJAX::json search products code WC 10.9.4
public static function json_search_products( $term = '', $include_variations = false ) {
check_ajax_referer( 'search-products', 'security' );
if ( empty( $term ) && isset( $_GET['term'] ) ) {
$term = (string) wc_clean( wp_unslash( $_GET['term'] ) );
}
if ( empty( $term ) ) {
wp_die();
}
if ( ! empty( $_GET['limit'] ) ) {
$limit = absint( $_GET['limit'] );
} else {
$limit = absint( apply_filters( 'woocommerce_json_search_limit', 30 ) );
}
$include_ids = ! empty( $_GET['include'] ) ? array_map( 'absint', (array) wp_unslash( $_GET['include'] ) ) : array();
$exclude_ids = ! empty( $_GET['exclude'] ) ? array_map( 'absint', (array) wp_unslash( $_GET['exclude'] ) ) : array();
$exclude_types = array();
if ( ! empty( $_GET['exclude_type'] ) ) {
// Support both comma-delimited and array format inputs.
$exclude_types = wp_unslash( $_GET['exclude_type'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( ! is_array( $exclude_types ) ) {
$exclude_types = explode( ',', $exclude_types );
}
// Sanitize the excluded types against valid product types.
foreach ( $exclude_types as &$exclude_type ) {
$exclude_type = strtolower( trim( $exclude_type ) );
}
$exclude_types = array_intersect(
array_merge( array( ProductType::VARIATION ), array_keys( wc_get_product_types() ) ),
$exclude_types
);
}
$data_store = WC_Data_Store::load( 'product' );
$ids = $data_store->search_products( $term, '', (bool) $include_variations, false, $limit, $include_ids, $exclude_ids );
$products = array();
foreach ( $ids as $id ) {
$product_object = wc_get_product( $id );
if ( ! $product_object || ! wc_products_array_filter_readable( $product_object ) ) {
continue;
}
$formatted_name = $product_object->get_formatted_name();
$managing_stock = $product_object->managing_stock();
if ( in_array( $product_object->get_type(), $exclude_types, true ) ) {
continue;
}
if ( ! empty( $_GET['display_stock'] ) ) {
$stock_parts = array();
if ( $managing_stock ) {
$stock_amount = $product_object->get_stock_quantity();
/* Translators: %s stock amount */
$stock_parts[] = sprintf( __( 'Stock: %s', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_amount, $product_object ) );
}
$stock_status = $product_object->get_stock_status();
if ( ProductStockStatus::OUT_OF_STOCK === $stock_status ) {
$stock_parts[] = __( 'Out of stock', 'woocommerce' );
} elseif ( ProductStockStatus::ON_BACKORDER === $stock_status ) {
$stock_parts[] = __( 'On backorder', 'woocommerce' );
}
if ( ! empty( $stock_parts ) ) {
$formatted_name .= ' (' . implode( ' – ', $stock_parts ) . ')';
}
$product_status = $product_object->get_status();
if ( ProductStatus::PRIVATE === $product_status ) {
$formatted_name .= ' (' . __( 'Disabled', 'woocommerce' ) . ')';
} elseif ( ProductStatus::DRAFT === $product_status ) {
$formatted_name .= ' (' . __( 'Draft', 'woocommerce' ) . ')';
}
}//end if
$products[ $product_object->get_id() ] = rawurldecode( wp_strip_all_tags( $formatted_name ) );
}
wp_send_json( apply_filters( 'woocommerce_json_search_found_products', $products ) );
}