WooCommerce 通过自定义产品数据字段扩展产品搜索

WooCommerce extend product search by custom product data field

我目前正在尝试扩展 WooCommerce 产品搜索,以便搜索使用我在一般产品数据部分中创建的自定义字段:

add_action( 'woocommerce_product_options_general_product_data', 'woocommerce_product_options_general_product_data_action', 9999, 0 );
function woocommerce_product_options_general_product_data_action(): void {
    global $post;

    echo '<div class="options_group">';

    woocommerce_wp_text_input( [
        'id'            => '_pdc_notch',
        'wrapper_class' => '',
        'label'         => 'Notch',
        'desc_tip'      => true,
        'type'          => 'text',
        'description'   => 'Enter notch PDC',
        'value'         => get_post_meta( $post->ID, '_pdc_notch', true )
    ] );

    echo '</div>';
}

add_action( 'woocommerce_process_product_meta', 'woocommerce_process_product_meta_action', 10, 1 );
function woocommerce_process_product_meta_action( int $product_id ): void {
    $pdc_notch = sanitize_text_field( wp_unslash( $_POST['_pdc_notch'] ?? null ) );

    update_post_meta( $product_id, '_pdc_notch', $pdc_notch );
}

使用上面的代码,我可以添加和保存我的自定义字段。现在我用下面的钩子扩展了搜索:

add_action( 'woocommerce_product_query', 'woocommerce_product_query_action', 10, 2 );
function woocommerce_product_query_action( WP_Query $q, object $instance ) {
    if ( ! is_admin() && $q->is_main_query() && $q->is_search() ) {
        $meta_query = $q->get( 'meta_query' );

        $meta_query[] = [
            'key'     => '_pdc_notch',
            'value'   => $q->query['s'],
            'compare' => 'LIKE'
        ];

        $q->set( 'meta_query', $meta_query );
    }
}

当我在我的字段中设置像“Iamabigtestword”这样的文本并将其放入搜索中时,我仍然一无所获。我错过了什么?我真的找不到这里的问题。通常,钩子应该可以工作(关于 Whosebug 的回答:

备注

您可以 copy/paste 直接在您子主题的 functions.php 文件中的代码来测试它,因为它没有依赖项并且应该将字段添加到 Products > Product > General 选项卡结束。

经过一些测试,我想我找到了一个方法。首先,我调试了应用操作的 WC 函数,但由于我没有取得任何进展,因此改变了我的方法。我现在通过给定的 WordPress 过滤器扩展了 post 搜索:

add_filter( 'posts_search', 'filter_posts_search', 10, 2 );
/**
 * Extend product search to use the new custom field within search
 *
 * @param string $search
 * @param WP_Query $query
 *
 * @return string
 */
function filter_posts_search( string $search, WP_Query $query ): string {
    global $wpdb;

    if ( empty( $search ) || ! ( isset( $query->query_vars['s'], $query->query_vars['post_type'] ) && ! empty( $query->query_vars['s'] ) && $query->query_vars['post_type'] === 'product' ) || is_admin() || ! is_search() || ! is_main_query() ) {
        return $search;
    }

    $product_ids = [];

    $products = wc_get_products( [
        'post_type'    => 'product',
        'limit'        => - 1,
        'meta_key'     => '_pdc_notch',
        'meta_value'   => esc_attr( $query->query_vars['s'] ),
        'meta_compare' => 'LIKE' // or '='
    ] );

    /**
     * @var WC_Product $product
     */
    foreach ( $products as $product ) {
        $product_ids[] = $product->get_id();
    }

    $product_ids = array_unique( $product_ids );

    if ( count( $product_ids ) > 0 ) {
        $search = str_replace( 'AND (((', "AND ((({$wpdb->posts}.ID IN (" . implode( ',', $product_ids ) . ")) OR (", $search );
    }

    return $search;
}

希望对大家有所帮助!如果有人找到更好的方法,我可以帮你测试。