提问者:小点点

WooCommerce-扩展产品搜索表单


我需要扩展产品搜索表单的功能,以便能够按SKU搜索产品。此操作仅在标题和内容中搜索。我不知道。

<?php 
if ( isset( $_REQUEST['product_cat'] ) && ! empty( $_REQUEST['product_cat'] ) ) {
    $optsetlect = $_REQUEST['product_cat'];
} else {
    $optsetlect = 0;  
}

$args = array(
    'show_option_all' => __( 'All categories', 'domain' ),
    'hierarchical' => 1,
    'hide_empty' => 0,
    'class' => 'cat',
    'echo' => 0,
    'value_field' => 'slug',
    'selected' => $optsetlect
);

$args['taxonomy'] = 'product_cat';
$args['name'] = 'product_cat';              
$args['class'] = 'selectpicker dropdown-select';
?>

<form role="search" method="get" action="<?php echo esc_url( home_url() ); ?>">
    <input type="hidden" name="post_type" value="product">

    <div class="input-group">
        <input id="woocommerce-product-search-field-<?php echo isset( $index ) ? absint( $index ) : 0; ?>" name="s" placeholder="Search products" type="text" autocomplete="off">

        <div class="input-group-append">

            <?php echo wp_dropdown_categories( $args ); ?>

            <button class="btn btn-primary" type="submit">
                <span class="ec ec-search"></span>
            </button>
        </div>
    </div>
</form>

共1个答案

匿名用户

你应该试试这样的方法:

add_filter( 'get_product_search_form' , 'woo_custom_product_searchform' );
function woo_custom_product_searchform( $form ) {
    $form = '<form role="search" method="get" id="searchform" action="' . esc_url( home_url( '/'  ) ) . '">
    <div>
        <label class="screen-reader-text" for="s">' . __( 'Search for:', 'woocommerce' ) . '</label>
        <input type="text" value="' . get_search_query() . '" name="s" id="s" placeholder="' . __( 'My Search form', 'woocommerce' ) . '" />
        <input type="submit" id="searchsubmit" value="'. esc_attr__( 'Search', 'woocommerce' ) .'" />
        <input type="hidden" name="post_type" value="product" />
    </div>
    </form>';
return $form;
}