1

I am learning to code in PHP, and I need some guidance from you to help implement a feature for Woocommerce Product Category/Product Tag.

I have spent quite a lot of time on trial and error without much success, so I hope you can give me some guidance.

Below is what I am trying to achieve:

Natively, the Product Category/Product Tag Pages at the fronted display products that are explicitly assigned to each Product Category/Product Tag. Since we will be adding many products and many categories continuously, this would take too much time to manually assign products to each Product Category/Product Tag on a ongoing basis.

So the task is to create a solution, to allow each Product Category/Product Tag Page to display products from SearchWP plugin results based on a Specified Search Keyword, in place of the products assigned to the Product Category/Product Tag.

Meaning instead of displaying the products as usual in Product Category/Product Tag Pages, we would specify a keyword for each of the Product Categories/Product Tags in a term custom field named a1_product_search_keyword (https://screenpresso.com/=1MHY), and the products displayed at the frontend would be the "Search Results" of the Specified Keywords entered into that a1_product_search_keyword custom field.

For example, we have a Product Category called "February Birthstone - Amethyst". So instead of manually assigning products to that Product Category, we would just specify a keyword called "amethyst jewelry" at the Edit Screen of that Product Category, and when that Product Category Page is displayed at the frontend, the products on that page would be the "Search Result" of the keyword "amethyst jewelry".

SearchWP plugin will be used because it is more configurable than Native Wordpress search. I have discussed this topic with SearchWP and below is their recommendation:

To display search results to a category page you would need to customize your category template and use SWP_Query to retrieve the results. The documentation for SWP_Query can be found here:

https://searchwp.com/docs/swp_query/

The code example he provided is as below. I just need to know how could I implement these codes in order to replace the Native Output of Product Category/Product Tag pages with the Search Result from SWP_Query. I think the preferred method would be to inject these code to a Woocommerce Hook, if possible.

Thanks in advance for all your help :)

 <?php

/**
 * Use SearchWP's SWP_Query to perform a search using the default engine
 */

$swp_query = new SWP_Query(

    array(
        's' => 'coffee', // search query
    )
);

if ( ! empty( $swp_query->posts ) ) {

    foreach( $swp_query->posts as $post ) : setup_postdata( $post ); ?>

        <div class="search-result">

            <h3><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></h3>

            <?php the_excerpt(); ?>

        </div>

    <?php endforeach; wp_reset_postdata();

} else {

    ?><p>No results found.</p><?php

}

closed as off-topic by Michael, Krzysiek Dróżdż, Nathan Johnson, Jacob Peattie, Fayaz Apr 5 '18 at 14:39

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • Hi, can someone please chip in? – Zen3628 Apr 1 '18 at 12:02

Browse other questions tagged or ask your own question.