WooCommerce多站点全球搜索如何实现mod ajax功能?

时间:2020-05-08 作者:Vito Santimone

我创建了一个多站点电子商务装置,以区分男性和女性部门。然而,经常发生的情况是,在男性部门,他们做与女性文章相关的研究,反之亦然。因此,我的网站说它找不到文章,显然不是这样。如何实现全局搜索功能?我尝试在粘贴的这个功能上使用switch\\u to\\u blog,但遗憾的是我无法使用它。谁能帮帮我吗?这是我尝试的代码。。。但它是否停止并仅在站点1上工作。。。有什么想法吗?提前感谢

 switch_to_blog(1);
    $results = new WP_Query( apply_filters( \'basel_ajax_search_args\', $query_args ) );

    if ( basel_get_opt( \'relevanssi_search\' ) && function_exists( \'relevanssi_do_query\' ) ) {
        relevanssi_do_query( $results );
    }

    $suggestions = array();

    if ( $results->have_posts() ) {

        if ( $post_type == \'product\' && basel_woocommerce_installed() ) {
            $factory = new WC_Product_Factory();
        }

        while ( $results->have_posts() ) {
            $results->the_post();

            if ( $post_type == \'product\' && basel_woocommerce_installed() ) {
                $product = $factory->get_product( get_the_ID() );

                $suggestions[] = array(
                    \'value\' => get_the_title(),
                    \'permalink\' => get_the_permalink(),
                    \'price\' => $product->get_price_html(),
                    \'thumbnail\' => $product->get_image(),
                    \'sku\' => $product->get_sku() ? esc_html__( \'SKU:\', \'basel\' ) . \' \' . $product->get_sku() : \'\',
                );
            } else {
                $suggestions[] = array(
                    \'value\' => get_the_title(),
                    \'permalink\' => get_the_permalink(),
                    \'thumbnail\' => get_the_post_thumbnail( null, \'medium\', \'\' ),
                );
            }
        }

        wp_reset_postdata();
        restore_current_blog();
    } else {
        $suggestions[] = array(
            \'value\' => ( $post_type == \'product\' ) ? esc_html__( \'No products found\', \'basel\' ) : esc_html__( \'No posts found\', \'basel\' ),
            \'no_found\' => true,
            \'permalink\' => \'\'
        );
    }

    switch_to_blog(2);
    $results = new WP_Query( apply_filters( \'basel_ajax_search_args\', $query_args ) );

    if ( basel_get_opt( \'relevanssi_search\' ) && function_exists( \'relevanssi_do_query\' ) ) {
        relevanssi_do_query( $results );
    }

    //$suggestions = array();

    if ( $results->have_posts() ) {

        if ( $post_type == \'product\' && basel_woocommerce_installed() ) {
            $factory = new WC_Product_Factory();
        }

        while ( $results->have_posts() ) {
            $results->the_post();

            if ( $post_type == \'product\' && basel_woocommerce_installed() ) {
                $product = $factory->get_product( get_the_ID() );

                $suggestions[] = array(
                    \'value\' => get_the_title(),
                    \'permalink\' => get_the_permalink(),
                    \'price\' => $product->get_price_html(),
                    \'thumbnail\' => $product->get_image(),
                    \'sku\' => $product->get_sku() ? esc_html__( \'SKU:\', \'basel\' ) . \' \' . $product->get_sku() : \'\',
                );
            } else {
                $suggestions[] = array(
                    \'value\' => get_the_title(),
                    \'permalink\' => get_the_permalink(),
                    \'thumbnail\' => get_the_post_thumbnail( null, \'medium\', \'\' ),
                );
            }
        }

        wp_reset_postdata();
        restore_current_blog();
    } else {
        $suggestions[] = array(
            \'value\' => ( $post_type == \'product\' ) ? esc_html__( \'No products found\', \'basel\' ) : esc_html__( \'No posts found\', \'basel\' ),
            \'no_found\' => true,
            \'permalink\' => \'\'
        );
    }

     //$my_array2 = array(\'suggestions\' => $suggestions);
     //$my_array1 = array(\'suggestions\' => $suggestions1);
     //$res = array_merge($my_array2, $my_array1); 
    // echo json_encode($res);
    echo json_encode(array(\'suggestions\' => $suggestions));

die(); 
}

2 个回复
SO网友:Watch Teller

stackexchange是一家新公司,我只有拥有50个声誉才能发表评论。

我们是同一个主题开发人员。我找到了这个topic 可以帮助你。我正在测试它-WooCommerce多站点全球搜索。但这两个网站都是新安装的,具有相同的产品数据(名称、价格、相同的产品类别、相同的主题)。你可以试试看。

OPTION 1

在此行之前

$results = new WP_Query( apply_filters( \'basel_ajax_search_args\', $query_args ) );
添加

// in this variable you can pass all the blog IDs you would like to display posts from
$blog_ids = array( 1, 2, 3, 4, 5 );

foreach( $blog_ids as $id ) {

    switch_to_blog( $id );

And

在此行之后

wp_reset_postdata();
添加

        restore_current_blog();

And

在这2行之后

die();

}
添加

}

使用本地主机或登台进行尝试OR 在尝试之前备份您的网站。

我正在我的主题搜索中尝试它。php

/**
 * ------------------------------------------------------------------------------------------------
 * Ajax search
 * ------------------------------------------------------------------------------------------------
 */
if ( ! function_exists( \'woodmart_init_search_by_sku\' ) ) {
    function woodmart_init_search_by_sku() {
        if ( apply_filters( \'woodmart_search_by_sku\', woodmart_get_opt( \'search_by_sku\' ) ) && woodmart_woocommerce_installed() ) {
            add_filter( \'posts_search\', \'woodmart_product_search_sku\', 9 );
        }
    }

    add_action( \'init\', \'woodmart_init_search_by_sku\', 10 );
}

if ( ! function_exists( \'woodmart_ajax_suggestions\' ) ) {
    function woodmart_ajax_suggestions() {

        $allowed_types = array( \'post\', \'product\', \'portfolio\' );
        $post_type = \'product\';

        if ( apply_filters( \'woodmart_search_by_sku\', woodmart_get_opt( \'search_by_sku\' ) ) && woodmart_woocommerce_installed() ) {
            add_filter( \'posts_search\', \'woodmart_product_ajax_search_sku\', 10 );
        }

        $query_args = array(
            \'posts_per_page\' => 5,
            \'post_status\'    => \'publish\',
            \'post_type\'      => $post_type,
            \'no_found_rows\'  => 1,
        );

        if ( ! empty( $_REQUEST[\'post_type\'] ) && in_array( $_REQUEST[\'post_type\'], $allowed_types ) ) {
            $post_type = strip_tags( $_REQUEST[\'post_type\'] );
            $query_args[\'post_type\'] = $post_type;
        }

        if ( $post_type == \'product\' && woodmart_woocommerce_installed() ) {

            $product_visibility_term_ids = wc_get_product_visibility_term_ids();
            $query_args[\'tax_query\'][] = array(
                \'taxonomy\' => \'product_visibility\',
                \'field\'    => \'term_taxonomy_id\',
                \'terms\'    => $product_visibility_term_ids[\'exclude-from-search\'],
                \'operator\' => \'NOT IN\',
            );

            if ( ! empty( $_REQUEST[\'product_cat\'] ) ) {
                $query_args[\'product_cat\'] = strip_tags( $_REQUEST[\'product_cat\'] );
            }
        }

        if ( \'yes\' === get_option( \'woocommerce_hide_out_of_stock_items\' ) && $post_type == \'product\' ) {
            $query_args[\'meta_query\'][] = array( \'key\' => \'_stock_status\', \'value\' => \'outofstock\', \'compare\' => \'NOT IN\' );
        }

        if ( ! empty( $_REQUEST[\'query\'] ) ) {
            $query_args[\'s\'] = sanitize_text_field( $_REQUEST[\'query\'] );
        }

        if ( ! empty( $_REQUEST[\'number\'] ) ) {
            $query_args[\'posts_per_page\'] = (int) $_REQUEST[\'number\'];
        }

        $blog_ids = array( 1, 2, 3, 4, 5, 6, 7, 8, 9 );

        foreach( $blog_ids as $id ) {

            switch_to_blog( $id );


        $results = new WP_Query( apply_filters( \'woodmart_ajax_search_args\', $query_args ) );

        if ( woodmart_get_opt( \'relevanssi_search\' ) && function_exists( \'relevanssi_do_query\' ) ) {
            relevanssi_do_query( $results );
        }

        $suggestions = array();

        if ( $results->have_posts() ) {

            if ( $post_type == \'product\' && woodmart_woocommerce_installed() ) {
                $factory = new WC_Product_Factory();
            }

            while ( $results->have_posts() ) {
                $results->the_post();

                if ( $post_type == \'product\' && woodmart_woocommerce_installed() ) {
                    $product = $factory->get_product( get_the_ID() );

                    $suggestions[] = array(
                        \'value\' => html_entity_decode( get_the_title() ),
                        \'permalink\' => get_the_permalink(),
                        \'price\' => $product->get_price_html(),
                        \'thumbnail\' => $product->get_image(),
                        \'sku\' => $product->get_sku() ? esc_html__( \'SKU:\', \'woodmart\' ) . \' \' . $product->get_sku() : \'\',
                    );
                } else {
                    $suggestions[] = array(
                        \'value\' => html_entity_decode( get_the_title() ),
                        \'permalink\' => get_the_permalink(),
                        \'thumbnail\' => get_the_post_thumbnail( null, \'medium\', \'\' ),
                    );
                }
            }

            wp_reset_postdata();

                restore_current_blog();

        } else {
            $suggestions[] = array(
                \'value\' => ( $post_type == \'product\' ) ? esc_html__( \'No products found\', \'woodmart\' ) : esc_html__( \'No posts found\', \'woodmart\' ),
                \'no_found\' => true,
                \'permalink\' => \'\'
            );
        }

        if ( woodmart_get_opt( \'enqueue_posts_results\' ) && \'post\' !== $post_type ) {
            $post_suggestions = woodmart_get_post_suggestions();
            $suggestions = array_merge( $suggestions, $post_suggestions );
        }

        echo json_encode( array(
            \'suggestions\' => $suggestions,
        ) );

        die();
    }
    }

    add_action( \'wp_ajax_woodmart_ajax_search\', \'woodmart_ajax_suggestions\', 10 );
    add_action( \'wp_ajax_nopriv_woodmart_ajax_search\', \'woodmart_ajax_suggestions\', 10 );
}

if ( ! function_exists( \'woodmart_get_post_suggestions\' ) ) {
    function woodmart_get_post_suggestions() {
        $query_args = array(
            \'posts_per_page\' => 5,
            \'post_status\'    => \'publish\',
            \'post_type\'      => \'post\',
            \'no_found_rows\'  => 1,
        );

        if ( ! empty( $_REQUEST[\'query\'] ) ) {
            $query_args[\'s\'] = sanitize_text_field( $_REQUEST[\'query\'] );
        }

        if ( ! empty( $_REQUEST[\'number\'] ) ) {
            $query_args[\'posts_per_page\'] = (int) $_REQUEST[\'number\'];
        }

        $blog_ids = array( 1, 2, 3, 4, 5, 6, 7, 8, 9 );

        foreach( $blog_ids as $id ) {

            switch_to_blog( $id );


        $results = new WP_Query( $query_args );
        $suggestions = array();

        if ( $results->have_posts() ) {

            $suggestions[] = array(
                \'value\' => \'\',
                \'divider\' => esc_html__( \'Results from blog\', \'woodmart\' ),
            );

            while ( $results->have_posts() ) {
                $results->the_post();

                $suggestions[] = array(
                    \'value\' => html_entity_decode( get_the_title() ),
                    \'permalink\' => get_the_permalink(),
                    \'thumbnail\' => get_the_post_thumbnail( null, \'medium\', \'\' ),
                );
            }

            wp_reset_postdata();

                restore_current_blog();

                }

        }

        return $suggestions;
    }
}

if ( ! function_exists( \'woodmart_product_search_sku\' ) ) {
    function woodmart_product_search_sku( $where, $class = false ) {
        global $pagenow, $wpdb, $wp;

        $type = array(\'product\', \'jam\');

        if ( ( is_admin() ) //if ((is_admin() && \'edit.php\' != $pagenow) 
                || !is_search()  
                || !isset( $wp->query_vars[\'s\'] ) 
                //post_types can also be arrays..
                || (isset( $wp->query_vars[\'post_type\'] ) && \'product\' != $wp->query_vars[\'post_type\'] )
                || (isset( $wp->query_vars[\'post_type\'] ) && is_array( $wp->query_vars[\'post_type\'] ) && !in_array( \'product\', $wp->query_vars[\'post_type\'] ) ) 
                ) {
            return $where;
        }

        $s = $wp->query_vars[\'s\'];

        //WC 3.6.0
        if ( function_exists( \'WC\' ) && version_compare( WC()->version, \'3.6.0\', \'<\' ) ) {
            return woodmart_sku_search_query( $where, $s );
        } else {
            return woodmart_sku_search_query_new( $where, $s );
        }
    }
}

if ( ! function_exists( \'woodmart_product_ajax_search_sku\' ) ) {
    function woodmart_product_ajax_search_sku( $where ) {
        if ( ! empty( $_REQUEST[\'query\'] ) ) {
            $s = sanitize_text_field( $_REQUEST[\'query\'] );

            //WC 3.6.0
            if ( function_exists( \'WC\' ) && version_compare( WC()->version, \'3.6.0\', \'<\' ) ) {
                return woodmart_sku_search_query( $where, $s );
            } else {
                return woodmart_sku_search_query_new( $where, $s );
            }
        }

        return $where;
    }
}

if ( ! function_exists( \'woodmart_sku_search_query\' ) ) {
    function woodmart_sku_search_query( $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->postmeta} pm on p.ID = pm.post_id and pm.meta_key=\'_sku\' and pm.meta_value 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 post_id FROM {$wpdb->postmeta} WHERE meta_key=\'_sku\' AND meta_value 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;
    }
}

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;
    }
}

if ( ! function_exists( \'woodmart_rlv_index_variation_skus\' ) ) {
    function woodmart_rlv_index_variation_skus( $content, $post ) {
        if ( ! woodmart_get_opt( \'search_by_sku\' ) || ! woodmart_get_opt( \'relevanssi_search\' ) || ! function_exists( \'relevanssi_do_query\' ) ) {
            return $content;
        }

        if ( $post->post_type == \'product\' ) {

            $args = array( \'post_parent\' => $post->ID, \'post_type\' => \'product_variation\', \'posts_per_page\' => -1 );
            $variations = get_posts( $args );
            if ( !empty( $variations)) {
                foreach ( $variations as $variation ) {
                    $sku = get_post_meta( $variation->ID, \'_sku\', true );
                    $content .= " $sku";
                }
            }
        }

        return $content;
    }

    add_filter( \'relevanssi_content_to_index\', \'woodmart_rlv_index_variation_skus\', 10, 2 );
}
wp_reset_postdata();

如果你成功了,请通知我。

SO网友:Watch Teller

Answer: Worked ajax search suggestion

/**
 * ------------------------------------------------------------------------------------------------
 * Ajax search
 * ------------------------------------------------------------------------------------------------
 */
if ( ! function_exists( \'woodmart_init_search_by_sku\' ) ) {
    function woodmart_init_search_by_sku() {
        if ( apply_filters( \'woodmart_search_by_sku\', woodmart_get_opt( \'search_by_sku\' ) ) && woodmart_woocommerce_installed() ) {
            add_filter( \'posts_search\', \'woodmart_product_search_sku\', 9 );
        }
    }

    add_action( \'init\', \'woodmart_init_search_by_sku\', 10 );
}

if ( ! function_exists( \'woodmart_ajax_suggestions\' ) ) {
    function woodmart_ajax_suggestions() {

        $allowed_types = array( \'post\', \'product\', \'portfolio\' );
        $post_type = \'product\';

        if ( apply_filters( \'woodmart_search_by_sku\', woodmart_get_opt( \'search_by_sku\' ) ) && woodmart_woocommerce_installed() ) {
            add_filter( \'posts_search\', \'woodmart_product_ajax_search_sku\', 10 );
        }

        $query_args = array(
            \'posts_per_page\' => 5,
            \'post_status\'    => \'publish\',
            \'post_type\'      => $post_type,
            \'no_found_rows\'  => 1,
        );

        if ( ! empty( $_REQUEST[\'post_type\'] ) && in_array( $_REQUEST[\'post_type\'], $allowed_types ) ) {
            $post_type = strip_tags( $_REQUEST[\'post_type\'] );
            $query_args[\'post_type\'] = $post_type;
        }

        if ( $post_type == \'product\' && woodmart_woocommerce_installed() ) {

            $product_visibility_term_ids = wc_get_product_visibility_term_ids();
            $query_args[\'tax_query\'][] = array(
                \'taxonomy\' => \'product_visibility\',
                \'field\'    => \'term_taxonomy_id\',
                \'terms\'    => $product_visibility_term_ids[\'exclude-from-search\'],
                \'operator\' => \'NOT IN\',
            );

            if ( ! empty( $_REQUEST[\'product_cat\'] ) ) {
                $query_args[\'product_cat\'] = strip_tags( $_REQUEST[\'product_cat\'] );
            }
        }

        if ( \'yes\' === get_option( \'woocommerce_hide_out_of_stock_items\' ) && $post_type == \'product\' ) {
            $query_args[\'meta_query\'][] = array( \'key\' => \'_stock_status\', \'value\' => \'outofstock\', \'compare\' => \'NOT IN\' );
        }

        if ( ! empty( $_REQUEST[\'query\'] ) ) {
            $query_args[\'s\'] = sanitize_text_field( $_REQUEST[\'query\'] );
        }

        if ( ! empty( $_REQUEST[\'number\'] ) ) {
            $query_args[\'posts_per_page\'] = (int) $_REQUEST[\'number\'];
        }

// Blog 1
switch_to_blog(1);

        $results = new WP_Query( apply_filters( \'woodmart_ajax_search_args\', $query_args ) );

        if ( woodmart_get_opt( \'relevanssi_search\' ) && function_exists( \'relevanssi_do_query\' ) ) {
            relevanssi_do_query( $results );
        }

        $suggestions = array();

        if ( $results->have_posts() ) {

            if ( $post_type == \'product\' && woodmart_woocommerce_installed() ) {
                $factory = new WC_Product_Factory();
            }

            while ( $results->have_posts() ) {
                $results->the_post();

                if ( $post_type == \'product\' && woodmart_woocommerce_installed() ) {
                    $product = $factory->get_product( get_the_ID() );

                    $suggestions[] = array(
                        \'value\' => html_entity_decode( get_the_title() ),
                        \'permalink\' => get_the_permalink(),
                        \'price\' => $product->get_price_html(),
                        \'thumbnail\' => $product->get_image(),
                        \'sku\' => $product->get_sku() ? esc_html__( \'SKU:\', \'woodmart\' ) . \' \' . $product->get_sku() : \'\',
                    );
                } else {
                    $suggestions[] = array(
                        \'value\' => html_entity_decode( get_the_title() ),
                        \'permalink\' => get_the_permalink(),
                        \'thumbnail\' => get_the_post_thumbnail( null, \'medium\', \'\' ),
                    );
                }
            }

            wp_reset_postdata();
                restore_current_blog();
        } else {
            $suggestions[] = array(
                \'value\' => ( $post_type == \'product\' ) ? esc_html__( \'No products found\', \'woodmart\' ) : esc_html__( \'No posts found\', \'woodmart\' ),
                \'no_found\' => true,
                \'permalink\' => \'\'
            );
        }

// Blog 2
switch_to_blog(2);

        $results = new WP_Query( apply_filters( \'woodmart_ajax_search_args\', $query_args ) );

        if ( woodmart_get_opt( \'relevanssi_search\' ) && function_exists( \'relevanssi_do_query\' ) ) {
            relevanssi_do_query( $results );
        }

        $suggestions1 = array();

        if ( $results->have_posts() ) {

            if ( $post_type == \'product\' && woodmart_woocommerce_installed() ) {
                $factory = new WC_Product_Factory();
            }

            while ( $results->have_posts() ) {
                $results->the_post();

                if ( $post_type == \'product\' && woodmart_woocommerce_installed() ) {
                    $product = $factory->get_product( get_the_ID() );

                    $suggestions[] = array(
                        \'value\' => html_entity_decode( get_the_title() ),
                        \'permalink\' => get_the_permalink(),
                        \'price\' => $product->get_price_html(),
                        \'thumbnail\' => $product->get_image(),
                        \'sku\' => $product->get_sku() ? esc_html__( \'SKU:\', \'woodmart\' ) . \' \' . $product->get_sku() : \'\',
                    );
                } else {
                    $suggestions[] = array(
                        \'value\' => html_entity_decode( get_the_title() ),
                        \'permalink\' => get_the_permalink(),
                        \'thumbnail\' => get_the_post_thumbnail( null, \'medium\', \'\' ),
                    );
                }
            }

            wp_reset_postdata();
                restore_current_blog();
        } else {
            $suggestions[] = array(
                \'value\' => ( $post_type == \'product\' ) ? esc_html__( \'No products found\', \'woodmart\' ) : esc_html__( \'No posts found\', \'woodmart\' ),
                \'no_found\' => true,
                \'permalink\' => \'\'
            );
        }

        if ( woodmart_get_opt( \'enqueue_posts_results\' ) && \'post\' !== $post_type ) {
            $post_suggestions = woodmart_get_post_suggestions();
            $suggestions = array_merge( $suggestions, $suggestion1, $post_suggestions );
        }

            echo json_encode(array(\'suggestions\' => $suggestions
        ));

        die();
    }

    add_action( \'wp_ajax_woodmart_ajax_search\', \'woodmart_ajax_suggestions\', 10 );
    add_action( \'wp_ajax_nopriv_woodmart_ajax_search\', \'woodmart_ajax_suggestions\', 10 );
}

if ( ! function_exists( \'woodmart_get_post_suggestions\' ) ) {
    function woodmart_get_post_suggestions() {
        $query_args = array(
            \'posts_per_page\' => 5,
            \'post_status\'    => \'publish\',
            \'post_type\'      => \'post\',
            \'no_found_rows\'  => 1,
        );

        if ( ! empty( $_REQUEST[\'query\'] ) ) {
            $query_args[\'s\'] = sanitize_text_field( $_REQUEST[\'query\'] );
        }

        if ( ! empty( $_REQUEST[\'number\'] ) ) {
            $query_args[\'posts_per_page\'] = (int) $_REQUEST[\'number\'];
        }

        $results = new WP_Query( $query_args );
        $suggestions = array();

        if ( $results->have_posts() ) {

            $suggestions[] = array(
                \'value\' => \'\',
                \'divider\' => esc_html__( \'Results from blog\', \'woodmart\' ),
            );

            while ( $results->have_posts() ) {
                $results->the_post();

                $suggestions[] = array(
                    \'value\' => html_entity_decode( get_the_title() ),
                    \'permalink\' => get_the_permalink(),
                    \'thumbnail\' => get_the_post_thumbnail( null, \'medium\', \'\' ),
                );
            }

            wp_reset_postdata();
        }

        return $suggestions;
    }
}

if ( ! function_exists( \'woodmart_product_search_sku\' ) ) {
    function woodmart_product_search_sku( $where, $class = false ) {
        global $pagenow, $wpdb, $wp;

        $type = array(\'product\', \'jam\');

        if ( ( is_admin() ) //if ((is_admin() && \'edit.php\' != $pagenow) 
                || !is_search()  
                || !isset( $wp->query_vars[\'s\'] ) 
                //post_types can also be arrays..
                || (isset( $wp->query_vars[\'post_type\'] ) && \'product\' != $wp->query_vars[\'post_type\'] )
                || (isset( $wp->query_vars[\'post_type\'] ) && is_array( $wp->query_vars[\'post_type\'] ) && !in_array( \'product\', $wp->query_vars[\'post_type\'] ) ) 
                ) {
            return $where;
        }

        $s = $wp->query_vars[\'s\'];

        //WC 3.6.0
        if ( function_exists( \'WC\' ) && version_compare( WC()->version, \'3.6.0\', \'<\' ) ) {
            return woodmart_sku_search_query( $where, $s );
        } else {
            return woodmart_sku_search_query_new( $where, $s );
        }
    }
}

if ( ! function_exists( \'woodmart_product_ajax_search_sku\' ) ) {
    function woodmart_product_ajax_search_sku( $where ) {
        if ( ! empty( $_REQUEST[\'query\'] ) ) {
            $s = sanitize_text_field( $_REQUEST[\'query\'] );

            //WC 3.6.0
            if ( function_exists( \'WC\' ) && version_compare( WC()->version, \'3.6.0\', \'<\' ) ) {
                return woodmart_sku_search_query( $where, $s );
            } else {
                return woodmart_sku_search_query_new( $where, $s );
            }
        }

        return $where;
    }
}

if ( ! function_exists( \'woodmart_sku_search_query\' ) ) {
    function woodmart_sku_search_query( $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->postmeta} pm on p.ID = pm.post_id and pm.meta_key=\'_sku\' and pm.meta_value 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 post_id FROM {$wpdb->postmeta} WHERE meta_key=\'_sku\' AND meta_value 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;
    }
}

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;
    }
}

if ( ! function_exists( \'woodmart_rlv_index_variation_skus\' ) ) {
    function woodmart_rlv_index_variation_skus( $content, $post ) {
        if ( ! woodmart_get_opt( \'search_by_sku\' ) || ! woodmart_get_opt( \'relevanssi_search\' ) || ! function_exists( \'relevanssi_do_query\' ) ) {
            return $content;
        }

        if ( $post->post_type == \'product\' ) {

            $args = array( \'post_parent\' => $post->ID, \'post_type\' => \'product_variation\', \'posts_per_page\' => -1 );
            $variations = get_posts( $args );
            if ( !empty( $variations)) {
                foreach ( $variations as $variation ) {
                    $sku = get_post_meta( $variation->ID, \'_sku\', true );
                    $content .= " $sku";
                }
            }
        }

        return $content;
    }

    add_filter( \'relevanssi_content_to_index\', \'woodmart_rlv_index_variation_skus\', 10, 2 );
}

相关推荐

WordPress AJAX表单出现问题

我正在尝试使用ajax将表单中的数据保存到用户元,但没有成功。我对jQuery还很陌生,不确定我会错在哪里,因为似乎没有任何suntax错误。以下是我的插件文件中的代码:/** * Action hooks */ // Register and Enqueue plugin styles and scripts add_action( \'wp_enqueue_scripts\',\'load_css_and_js\');