Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author joe_bopper

    (@joe_bopper)

    Hi Eric,

    This plugin uses WordPress’s default search form, therefore it doesn’t filter to just products unless adjustments are made. There are several ways to go about this.

    WooCommerce’s default search form can be found here. The important part of this is the last input:

    <input type="hidden" name="post_type" value="product" />

    You want this line inserted somewhere in the form tags this plugin provides.

    In each of the following solutions, you need to add the code to your theme’s functions.php (or another place of similar responsibility in the WordPress flow).

    First method:

    add_action( 'bop_nav_search_hidden_inputs', function( $item, $depth, $args ){
      echo '<input type="hidden" name="post_type" value="product" />';
    }, 10, 3 );

    This is probably the best solution. However, it requires v1.6.0 or higher of this plugin to work (n.b. I am just about to release v1.6.0). This uses a new action hook to output the hidden input field in the form. You can also use the arguments provided to the function to tailor your solution (say, e.g., if you wanted both a general search and a product search in different menus).

    Second Method:

    add_filter( 'bop_nav_search_show_submit_button', function( $show, $item, $depth, $args ){
      echo '<input type="hidden" name="post_type" value="product" />';
      return $show;
    }, 10, 4 );

    This is very similar to the first method, but works for v1.4.0 and above. The two main caveats are that it isn’t what is intended of the hook, and your function must return a boolean value (i.e. $show).

    Third Method:

    add_filter( 'get_nav_search_box_form', function( $item_output, $item, $depth, $args ){
      $item_output = $args->before . get_product_search_form( false ) . $args->after;
      return $item_output;
    }, 10, 4 );

    This is probably the worst of the methods, but does directly use the WooCommerce template. The benefit of using WC’s own stuff is drowned out by the inability to modify any part of the search box in the admin area (as WC’s function is not designed for that).

    N.B. This is outside the output buffer, so echos are inappropriate.

    Fourth Method:

    add_filter( 'get_nav_search_box_form', function( $item_output, $item, $depth, $args ){
      $item_output = "...YOUR OWN FORM HTML...";
      return $item_output;
    }, 10, 4 );

    This is the most open ended solution – you don’t even need to put a search form here, it could be any html. However, it takes a bit of a learning curve (probably from analysing this plugin’s code) in order to get it right and for the admin area fields to mean anything. This is for the most advanced users who are after a truly customised outcome.

    N.B. This is outside the output buffer, so echos are inappropriate.

    Also, note the following WC function defintion. If you look at its source code, you’ll notice:

    do_action( 'pre_get_product_search_form' );

    which you may wish to add to your code in case any other plugins are using it.

    In v1.6.0 or higher, you could do this to add it:

    add_action( 'bop_nav_search_pre_get_search_form', function( $item, $depth, $args ){
      do_action( 'pre_get_product_search_form' );
    }, 10, 3 );

    Hope this helps. Let me know if you need any further support.

    Cheers,
    Joe

    P.S. Thank you for the kind review too.

    Thread Starter oneandonlyeric

    (@oneandonlyeric)

    Works absolutely amazing! Thank you very very much!!! You are my man!

    Greetings,
    Eric

    Plugin Author joe_bopper

    (@joe_bopper)

    Glad I could help.

    Cheers,
    Joe

    Hi joe_bopper,

    I am very interested in installing your plugin, but I am using WooCommerce. I am still very new to WordPress and WooCommerce, and I am using Woo with a Theme called Thesis Classic Responsive.

    I don’t think that I am knowledgeable enough to make the script changes you’ve explained above, so I am wondering if you have any plans to add any kind of feature to the plugin to make it easier to set it up to search products?

    Thanks so much! … Connie

    My site is: https://www.AmericanCentralStore.com

    Plugin Author joe_bopper

    (@joe_bopper)

    Hi Connie,

    I’m afraid I can’t really add editorial options for WooCommerce (or any other integration or further customisation) at the moment as the WP core doesn’t allow for adding these kind of custom options to menu items as things stand. Also, it would take a large amount of coding and hacking around the WP flow in order to achieve it within this plugin itself. Moreover, that code would then likely become outdated with each WP update – this is an area of WP that is under active development.

    I would like to add these features, but only once WP allows for them, which probably won’t happen until Appearance > Menus has become deprecated (probably a year or so, in my opinion).

    I understand your apprehension with getting stuck into the coding, but at the moment, I can only recommend trying to use one of the methods above. I assume you have access to the files on your installation (through ftp or otherwise)? If so, you could add the first method (or one of the others, if you prefer) above as its own plugin. It sounds daunting, but it’s really not hard at all. If you add the following as a file into your plugins folder ( [wp-folder]/wp-content/plugins ) with the filename woo-search-as-bop-nav-search.php

    <?php
    /*
    Plugin Name: WooCommerce Product Search As Bop Nav Search Menu Item
    */
    
    defined( 'ABSPATH' ) || die(); //This stops direct access to this file; WP must load first.
    
    //First Method
    add_action( 'bop_nav_search_hidden_inputs', function( $item, $depth, $args ){
      echo '<input type="hidden" name="post_type" value="product" />';
    }, 10, 3 );

    That should do it. You just need to activate it in the Plugins section of the admin area.

    Hope this helps.

    Cheers,
    Joe

    P.S. The reason I’m advising to add this as a plugin is because it is easier to create a plugin than make a child theme of your current third party theme.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom Output -> WooCommerce’ is closed to new replies.