Can Search adhere to menu visibility rules or some other visibility logic?

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Can Search adhere to menu visibility rules or some other visibility logic?

This topic contains 9 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 9 months, 4 weeks ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #44958
    billium99
    billium99
    Participant

    Hi! We have visibility rules for showing some product category menu items ONLY to logged in users.

    This works great for preventing logged-out users from buying things when they browser our menu choices, but Ajax Search Pro is still showing these products. Is there a way to show some of the indexed products ONLY to logged in users? Just don’t even show the product in search results if the user is logged out?

    I don’t see how we might be able to do this.

    If you wish to see an example, you can search for ActivNutrient without Iron. You will find this product:

    https://www.spectrumsupplements.com/supplement/activnutrients-without-iron-120-capsules/

    But it will 404 if you click the search result, since logged out users should not have access to this product.

    Thanks

    Bill Henderson

    #44966
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi Bill,

    I assume you are using a plugin/custom code of some sort to make this restriction. It is very likely possible to remove these items from the search results list via a custom code snippet.

    For that however I have to know how these items being excluded, and if the specific solution has an API function (or any other way) which let us check if the product is viewable for the current user (or hidden for logged out user). Let me know!

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #44989
    billium99
    billium99
    Participant

    Thanks – I’m using WooCommerce own Catalog Visibility Options plugin to prevent them from being visible. Can we tie into that somehow?

    #45017
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Thanks! That helps a lot. Based on their API I have made a custom code, which may do the trick. Unfortunately I have not been able to test it yet.

    Try adding this code via the Code Snippets plugin or to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.

    add_filter('asp_results', 'asp_results_remove_restricted_woo', 10, 1);
    function asp_results_remove_restricted_woo($results) {
    	foreach ( $results as $k => &$r ) {
    		if ( isset($r->post_type) && $r->post_type == 'product' ) {
    			if ( !WC_Catalog_Restrictions_Filters::instance()->user_can_view_price( wc_get_product($r->id) ) ) {
    				unset($results[$k]);
    			}
    		}
    	}
    	
    	return $results;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #45103
    billium99
    billium99
    Participant

    Hi Ernest,

    Thanks so much for this – I will deploy this shortly and let you know if we have any snags. However, I have an unrelated (I think) question.

    This client with the 4 sites clones new products after creating in the US site. We built a “cloner” tool where they can pick and choose which other “child” sites to push the new product data and images out to.

    Product name search in your Ajax plugin seems to always work great here, despite us not having a traditional Save Product action on the Add/Edit products page in WooCommerce. However, we recently added SKU (with your help I believe) as a searchable element, and after cloning, the SKUs don’t ever seem to come available in search until we manually rebuild the index.

    Is there a way to include SKU in whatever process is allowing our cloner to create products without saving, that nonetheless can be found by the index? Any idea why it’s different for SKU vs Product Name?

    Thanks again!

    Bill

    #45112
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi Bill,

    Great!

    The reason is, that the index does not update if the post is not saved, so the SKU is not being added to the database. I suggest triggering at least the save_post hook after each item is pushed, so that WordPress can run all the triggers related to that item. It is a best practice, because WordPress and plugins can have some hooks applied to this for maintenance (like Ajax Search Pro), so this way the index is kept up to date.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #45118
    billium99
    billium99
    Participant

    Interesting – but how do we find the product via product name if we haven’t triggered save_post hook?

    #45122
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    The SKU was added later on to the existing products right? So the titles were already indexed (if I understand correctly). If you were to change a product title, clone it to a site then it shouldn’t change in the index either if the save_post (or wp_insert_post) action hook is not triggered.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #45123
    billium99
    billium99
    Participant

    No – I believe the product was added fresh, via Cloner, was not changed via save_post or wp_insert_post, and can be searched by name but not SKU. We’re still investigating on this end, but perhaps we added SKU after the hook, in our script.

    But if my answer doesn’t make any sense, don’t worry about it for now. I have my developer looking at the code literally as I type this…

    Thanks

    Bill

    #45133
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Okay – let me know if you find anything!

    If the dev could somehow add the save_post trigger, that could very likely resolve everything. It is future proof, as if WooCommerce or any other plugin has any other maintenance hooks for that trigger, that will surely execute it.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.