i have this code
it work perfect if i search for 1 term but when i add 2 terms to search it fail
protected function process_item ( $data ) {
try {
$data = array_map ( 'trim', $data );
// Get product ID from SKUs if created during the importation
$product_ids = array ();
if ( ! empty ( $data['sku'] ) ) {
$skus = $data['sku'];
$skus = explode ( ',', $skus );
$skus = array_map ( 'trim', $skus );
foreach ( $skus as $sku ) {
$args = array (
'posts_per_page' => -1,
'suppress_filters' => false,
'post_type' => 'product',
'post_status' => 'publish',
'product_title_like' => $sku
);
add_filter ( 'posts_where', array ( $this, 'product_title_like_where' ), 10, 2 );
$products = get_posts( $args );
remove_filter ( 'posts_where', array ( $this, 'product_title_like_where' ), 10, 2 );
if ( ! empty ( $products ) ) {
foreach ( $products as $product ) {
$product_ids[] = $product->ID;
}
}
//$product_ids[] = wc_get_product_id_by_sku ( $sku );
}
}
unset ( $data['sku'] );
// Create terms
$term_ids = array ();
$data = array_values ( $data );
foreach ( $data as $term ) {
if ( $term == '' ) {
break;
}
// Term Parent ID
$parent_id = 0;
if ( ! empty ( $term_ids ) ) {
$parent_id = end ( $term_ids );
}
$term_id = wpce_create_term ( $this->finder_id, $term, $parent_id );
if ( ! $term_id ) {
break;
}
$term_ids[] = $term_id;
}
if ( ! empty ( $term_ids ) && ! empty ( $product_ids ) ) {
foreach ( $product_ids as $product_id ) {
if ( $product_id ) {
foreach ( $term_ids as $term_id ) {
$term_relation_id = wpce_create_term_relationship ( $term_id, $product_id );
}
}
}
}
return array (
'id' => $term_ids[0]
);
} catch ( Exception $e ) {
return new WP_Error( 'wpce_importer_error', $e->getMessage(), array( 'status' => $e->getCode() ) );
}
}
function product_title_like_where ( $where, &$wp_query ) {
global $wpdb;
if ( $search_term = $wp_query->get( 'product_title_like' ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $search_term ) ) . '%\'';
}
return $where;
}
so for example if i search for XX it match but if i search for XX X2 it fail to find
thank you
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744933615a4601884.html
评论列表(0条)