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 has 9 replies, 2 voices, and was last updated 2 years, 9 months ago by
Ernest Marcinko.
-
AuthorPosts
-
August 7, 2023 at 8:29 pm #44958
billium99
ParticipantHi! 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
August 8, 2023 at 10:01 am #44966Ernest Marcinko
KeymasterHi 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!
August 8, 2023 at 9:07 pm #44989billium99
ParticipantThanks – I’m using WooCommerce own Catalog Visibility Options plugin to prevent them from being visible. Can we tie into that somehow?
August 9, 2023 at 9:55 pm #45017Ernest Marcinko
KeymasterThanks! 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; }August 17, 2023 at 10:20 pm #45103billium99
ParticipantHi 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
August 18, 2023 at 1:28 pm #45112Ernest Marcinko
KeymasterHi 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.
August 18, 2023 at 4:04 pm #45118billium99
ParticipantInteresting – but how do we find the product via product name if we haven’t triggered save_post hook?
August 19, 2023 at 11:43 am #45122Ernest Marcinko
KeymasterThe 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(orwp_insert_post) action hook is not triggered.August 19, 2023 at 2:27 pm #45123billium99
ParticipantNo – 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
August 21, 2023 at 11:56 am #45133Ernest Marcinko
KeymasterOkay – 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.
-
AuthorPosts
- You must be logged in to reply to this topic.