0

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

  • have you print $wp_query and $where argument what query it will returning @Lolak. please share me that one – Gopala krishnan Dec 6 '19 at 10:16
  • @Gopalakrishnan18 this basically a modification for a plugin that search in products titles and if it find it attach to it custom field – Lolak Dec 6 '19 at 10:30
  • 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 ); – Lolak Dec 6 '19 at 10:30
  • added the whole code i have – Lolak Dec 6 '19 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.