Home Forums WoodMart support forum Search and ajax search by custom meta field

Search and ajax search by custom meta field

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #182081

    FreeRiderNSK
    Participant

    Hello!
    Help me please 🙂
    I have been using your amazing work for my projects for a long time! Thanks!
    I have an extra meta field for products “_sku_original”.
    How can I change this code to add my custom field to the search?

    if ( ! function_exists( 'woodmart_sku_search_query_new' ) ) {
    	function woodmart_sku_search_query_new( $where, $s ) {
    		global $wpdb;
    
    		$search_ids = array();
    		$terms = explode( ',', $s );
    
    		foreach ( $terms as $term ) {
    			//Include the search by id if admin area.
    			if ( is_admin() && is_numeric( $term ) ) {
    				$search_ids[] = $term;
    			}
    			// search for variations with a matching sku and return the parent.
    
    			$sku_to_parent_id = $wpdb->get_col( $wpdb->prepare( "SELECT p.post_parent as post_id FROM {$wpdb->posts} as p join {$wpdb->wc_product_meta_lookup} ml on p.ID = ml.product_id and ml.sku LIKE '%%%s%%' where p.post_parent <> 0 group by p.post_parent", wc_clean( $term ) ) );
    
    			//Search for a regular product that matches the sku.
    			$sku_to_id = $wpdb->get_col( $wpdb->prepare( "SELECT product_id FROM {$wpdb->wc_product_meta_lookup} WHERE sku LIKE '%%%s%%';", wc_clean( $term ) ) );
    
    			$search_ids = array_merge( $search_ids, $sku_to_id, $sku_to_parent_id );
    		}
    
    		$search_ids = array_filter( array_map( 'absint', $search_ids ) );
    
    		if ( sizeof( $search_ids ) > 0 ) {
    			$where = str_replace( ')))', ") OR ({$wpdb->posts}.ID IN (" . implode( ',', $search_ids ) . "))))", $where );
    		}
    		
    		#remove_filters_for_anonymous_class('posts_search', 'WC_Admin_Post_Types', 'product_search', 10);
    		return $where;
    	}
    }

    Thanks!

    Attachments:
    You must be logged in to view attached files.
    #182135

    Artem Temos
    Keymaster

    Hello,

    Sorry, but this code is developed for WooCommerce SKU and we don’t have instruction about how to transform it to your custom meta value.

    Kind Regards

Viewing 2 posts - 1 through 2 (of 2 total)