我正在创建高级woocommerce搜索,我想在搜索查询中添加sku以及product_tag和product_category。下面,我在WooCommerce产品搜索答案代码中使启用自定义分类法,从而启用了对多个分类法的搜索:
add_filter( 'posts_search', 'woocommerce_search_product_tag_extended', 999, 2 ); function woocommerce_search_product_tag_extended( $search, $query ) { global $wpdb, $wp; $qvars = $wp->query_vars; if ( is_admin() || empty($search) || ! ( isset($qvars['s']) && isset($qvars['post_type']) && ! empty($qvars['s']) && $qvars['post_type'] === 'product' ) ) { return $search; } // Here set your custom taxonomies in the array $taxonomies = array('product_tag', 'product_cat'); $tax_query = array('relation' => 'OR'); // Initializing tax query // Loop through taxonomies to set the tax query foreach( $taxonomies as $taxonomy ) { $tax_query[] = array( 'taxonomy' => $taxonomy, 'field' => 'name', 'terms' => esc_attr($qvars['s']), ); } // Get the product Ids $ids = get_posts( array( 'posts_per_page' => -1, 'post_type' => 'product', 'post_status' => 'publish', 'fields' => 'ids', 'tax_query' => $tax_query, ) ); if ( sizeof( $ids ) > 0 ) { $search = str_replace( 'AND (((', "AND ((({$wpdb->posts}.ID IN (" . implode( ',', $ids ) . ")) OR (", $search); } return $search; }
我也想在搜索查询中添加产品sku,如何添加它?
以下内容将产品搜索扩展到多个分类法(产品类别和产品标签)和多个自定义字段(如此处的SKU):
add_filter( 'posts_search', 'woocommerce_search_product_mega_extended', 999, 2 ); function woocommerce_search_product_mega_extended( $search, $query ) { global $wpdb, $wp; $qvars = $wp->query_vars; if ( is_admin() || empty($search) || ! ( isset($qvars['s']) && isset($qvars['post_type']) && ! empty($qvars['s']) && $qvars['post_type'] === 'product' ) ) { return $search; } // SETTINGS: $taxonomies = array('product_tag', 'product_cat'); // Here set your custom taxonomies in the array $meta_keys = array('_sku'); // Here set your product meta key(s) in the array // Initializing tax query $tax_query = count($taxonomies) > 1 ? array('relation' => 'OR') : array(); // Loop through taxonomies to set the tax query foreach( $taxonomies as $taxonomy ) { $tax_query[] = array( 'taxonomy' => $taxonomy, 'field' => 'name', 'terms' => esc_attr($qvars['s']), ); } // Get the product Ids from taxonomy(ies) $tax_query_ids = (array) get_posts( array( 'posts_per_page' => -1, 'post_type' => 'product', 'post_status' => 'publish', 'fields' => 'ids', 'tax_query' => $tax_query, ) ); // Initializing meta query $meta_query = count($meta_keys) > 1 ? array('relation' => 'OR') : array(); // Loop through taxonomies to set the tax query foreach( $taxonomies as $taxonomy ) { $meta_query[] = array( 'key' => '_sku', 'value' => esc_attr($qvars['s']), ); } // Get the product Ids from custom field(s) $meta_query_ids = (array) get_posts( array( 'posts_per_page' => -1, 'post_type' => 'product', 'post_status' => 'publish', 'fields' => 'ids', 'meta_query' => $meta_query, ) ); $product_ids = array_unique( array_merge( $tax_query_ids, $meta_query_ids ) ); // Merge Ids in one array with unique Ids if ( sizeof( $product_ids ) > 0 ) { $search = str_replace( 'AND (((', "AND ((({$wpdb->posts}.ID IN (" . implode( ',', $product_ids ) . ")) OR (", $search); } return $search; }
代码进入您的活动子主题(或活动主题)的functions.php文件中。经过测试和工作。