亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 WooCommerce 產品搜索擴展到自定義分類法和自定義字段

將 WooCommerce 產品搜索擴展到自定義分類法和自定義字段

PHP
慕田峪4524236 2023-10-22 21:28:09
我正在創建高級 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,如何添加?
查看完整描述

1 回答

?
HUX布斯

TA貢獻1876條經驗 獲得超6個贊

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,如何添加?


查看完整回答
反對 回復 2023-10-22
  • 1 回答
  • 0 關注
  • 138 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號