• Resolved agenziae20

    (@agenziae20)


    Hi,

    I would like to use your plugin with WCFM Multivendor Marketplace by WC Lovers, but cannot find a way to restrict the search to a single vendor’s products.
    I mean, if I place your shortcode in a Vendor Store Page (the very own single vendor shop page) I need to search ONLY throught this Vendor’s Products, while the default search is on the entire catalog which include Products by other Vendors.
    Is there any parameter to pass inside the shortcode in order to achive this function?

Viewing 11 replies - 1 through 11 (of 11 total)
  • Hi @agenziae20

    Unfortunately, this is currently not possible. We do not have integration with this plugin.

    Regards,
    Kris

    Thread Starter agenziae20

    (@agenziae20)

    Sorry to hear about that. Anyway it could be achieved passing an attribute with the author ID to the shortcode, something like [fibosearch author=”4″] to restrict results to only products added by author with userID “4”.

    @agenziae20

    Unfortunately, the shortcode won’t work. FiboSearch currently doesn’t have the functionality to search in the author’s archive.

    Regards,
    Kris

    Thread Starter agenziae20

    (@agenziae20)

    Ok, thanks. Is there a hook list or can you give me a hook name that I can use to insert a function and filter the results just before being printed?

    Hi @agenziae20

    I have prepared a code snippet for you that allows searching only the vendor’s product on the vendor archive page.

    // Check if we are in vendor store page and return author ID
    function fibosearch_get_product_author() {
    	global $wp_query;
    
    	if ( ! isset( $wp_query->query['store'] ) || empty ( $wp_query->query['store'] ) ) {
    		return false;
    	}
    
    	$product_id = isset( $wp_query->posts ) ? $wp_query->posts[0]->ID : false;
    	$author     = isset( $product_id ) ? get_post_field( 'post_author', $product_id ) : false;
    
    	return $author;
    }
    
    // Add author input to FiboSearch
    add_action( 'dgwt/wcas/form', function () {
    	$author = fibosearch_get_product_author();
    
    	if ( ! empty( $author ) ) { 
    		printf( '<input type="hidden" name="author" value="%d"/>', esc_attr( $author ) ); 
    	} 
    } );
    
    // Pass author ID to FiboSearch params
    add_filter( 'dgwt/wcas/scripts/custom_params', function ( $params ) {
    	$author = fibosearch_get_product_author();
    	$params['author'] = $author; 
    	return $params; 
    } ); 
    
    // Try to read the author ID and push to query args
    add_filter( 'dgwt/wcas/search_query/args', function( $args ) {
    	if ( isset( $_REQUEST['author'] ) && ! empty( $_REQUEST['author'] ) ) {
    		$args['author'] = absint( wp_unslash( $_REQUEST['author'] ) );
    	}
    
    	return $args;
    } );
    

    Before taking any actions, please make a full backup of your website (including the database).

    I recommend that you test this code on a test environment, e.g. staging.
    Check how to easily create a staging site for WordPress.

    You have two ways to add this code to your theme:
    Open the functions.php in your child theme and add the code at the end.
    or install the Code Snippets plugin and apply this code as a snippet.

    Regards,
    Kris

    Thread Starter agenziae20

    (@agenziae20)

    Hello @c0nst
    first of all, THANK YOU VERY MUCH for this snippet!
    I tested on the Vendor Store Page but it was not working, I don’t quite understand your code but fibosearch_get_product_author was searching in a wrong way (maybe searching in a product page?). Anyway, this is how I edited the function and now it gets the correct author_id

    // Check if we are in vendor store page and return author ID
    function fibosearch_get_product_author() {
    	global $post;
    	if(wcfm_is_store_page()){
    		$page_author = get_userdata( $post->post_author );
    		$author =  $page_author->ID ;
    	}
    	return $author;
    }

    Anyway I can see other products in the search autocomplete, it is due to the fact that all the catalogue Categories are displayed, we need to restrict this search as well.
    In fact, if I disable Show Categories (Fibosearch Settings> Autocomplete> Non-products in autocomplete) it works as expected.

    Thread Starter agenziae20

    (@agenziae20)

    The script worked on the staging site, doesn’t work on live site. Tested and found out it is blocked by Wordfence (that is inactive on staging site).

    Hi, @agenziae20 again!

    Please change the code to this to exclude all categories from the vendor page archive, and also please change dgwt/wcas/search_query/args to make sure, that $_REQUEST[‘author’] is false.

    // Try to read the author ID and push to query args
    add_filter( 'dgwt/wcas/search_query/args', function( $args ) {
    	if ( isset( $_REQUEST['author'] ) && 'false' != $_REQUEST['author'] ) {
    		$args['author'] = wp_unslash( $_REQUEST['author'] );
    	}
    	return $args;
    } );
    
    // Exclude all categories from display on vendor archive page
    add_filter( 'dgwt/wcas/search/product_cat/args', function ( $args ) {
    	$product_cats = get_categories( [ 'hide_empty' => 0, 'taxonomy' => 'product_cat', 'fields' => 'ids' ] );
    	if ( isset( $_REQUEST['author'] ) && 'false' != $_REQUEST['author'] && $product_cats ) {
    		$args['exclude'] = $product_cats;
    	}
    	
    	return $args;
    });

    Regards,
    Kris

    Thread Starter agenziae20

    (@agenziae20)

    Hello @c0nst
    thank you for this snippet, but unfortunately it did not work, it removed categories everywhere, not only on the vendor store page.

    I tried to modify:
    if ( isset( $_REQUEST['author'] ) && 'false' != $_REQUEST['author'] && $product_cats ) {
    with:
    if ( isset( $_REQUEST['author'] ) && ! empty( $_REQUEST['author']) && $product_cats ) {
    and it now works as expected.

    So, do you think it is better to also keep the following line as it was originally?
    if ( isset( $_REQUEST['author'] ) && ! empty( $_REQUEST['author'] ) ) {

    Regards.

    @agenziae20

    I modified the code. This way i thing would be the best:

    <?php
    
    // Try to read the author ID and push to query args
    add_filter( 'dgwt/wcas/search_query/args', function( $args ) {
    	if ( ! empty( $_REQUEST['author'] ) && is_numeric( $_REQUEST['author'] ) ) {
    		$args['author'] = absint( $_REQUEST['author'] );
    	}
    
    	return $args;
    } );
    
    // Exclude all categories from display on vendor archive page
    add_filter( 'dgwt/wcas/search/product_cat/args', function ( $args ) {
    	$product_cats = get_categories( [ 'hide_empty' => 0,'taxonomy' => 'product_cat', 'fields' => 'ids' ] );
    
    	if ( ! empty( $_REQUEST['author'] ) && is_numeric( $_REQUEST['author'] ) && $product_cats ) {
    		$args['exclude'] = $product_cats;
    	}
    	
    	return $args;
    });

    Regards,
    Kris

    Thread Starter agenziae20

    (@agenziae20)

    Thanks a lot @c0nst

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘wcfm store vendor’ is closed to new replies.