Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Use of users custom fields in a product search
- This topic has 19 replies, 2 voices, and was last updated 6 years ago by
T0mX06.
-
AuthorPosts
-
May 4, 2020 at 5:13 pm #27089
T0mX06
ParticipantHi,
I’m trying to use ASP with WCFM MarketPlace. Everyhing went fine but there is one thing i could not acheive :
I want to use the custom fields create by WCFM MarketPlace in users’ profils (like store name, store policy …) in the products search list (for example, if I search for a product A, the name of the product is displayed in the search list, but also in its description the store name). I could not do that I think because these custom fields like store name are in the users’ customs fields and not in the product one. So my question is, may i ‘link’ or load all the users metadata in function.php to make them available in the product search ?May 5, 2020 at 8:36 am #27093Ernest Marcinko
KeymasterHi,
I’m afraid that is not possible. Using a custom code will not help, as there is no direct connection between user meta and posts. This would require direct modifications to the core search code to make it work – but even then it might not work correctly.
May 5, 2020 at 11:33 am #27099T0mX06
ParticipantHi,
Thanks for your answer. Do you think there is a way to sync metadata between users ones and products ones ?
May 5, 2020 at 1:06 pm #27101Ernest Marcinko
KeymasterHi,
Could be possible. Perhaps an import/export plugin might be able to do that. Although if you have a lot of products (thousands), I would highly discourage using custom fields – as it would be repetitive, taking up a lot from the post meta table. Using taxonomy terms (like categories) could be a better idea – as the same tags can be assigned to multiple posts.
Do you happen to know the names of these user meta fields? If these are all user meta, then using the index table engine there might be a way to use a custom code – to index these meta data along with product descriptions (sort of).
May 5, 2020 at 1:52 pm #27105T0mX06
ParticipantHi,
Yes they are all users meta. You will find below all the customs fields I would like to use in the product search list.
If you could acheive this via a custom code and the index table this will be amazing !Thanks for everything
_store_description
store_name
wcfm_vendor_store_hours‘store_email’
‘phone’
‘payment’
‘wcfm_store_hours’
‘wcfm_shipping_policy’
‘wcfm_vacation_mode’
‘store_location’
wcfmmp_store_nameMay 5, 2020 at 2:18 pm #27109Ernest Marcinko
KeymasterOkay, if these are indeed the author metadata, then this custom code may do the trick:
Try adding this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!
add_filter('asp_post_content_before_tokenize', 'vendor_asp_post_content_before_tokenize', 10, 2); function vendor_asp_post_content_before_tokenize($content, $the_post) { $meta_fields = array( '_store_description', 'store_name', 'wcfm_vendor_store_hours', 'store_email', 'phone', 'payment', 'wcfm_store_hours', 'wcfm_shipping_policy', 'wcfm_vacation_mode', 'store_location', 'wcfmmp_store_name' ); foreach ( $meta_fields as $field ) { $s = get_user_meta( $the_post->post_author, $field, true ); if ( !empty($s) ) { $content .= ' ' . $s; } } return $content; }After that, you will have to re-create the index table. You will find all the information in this documentation.
May 5, 2020 at 3:10 pm #27111T0mX06
ParticipantOk,
First of all thanks for everything. Really !
I’ve done the changes in function.php. I’ve switched the search engine from regular to index one. Added a {wcfmmp_store_name} and a {store_name} just in case but both are filled with stores names informations. The results don’t display the store name in the results.
Perheaps you’ll need my creditentials to log in my admin and see my function.php in my FTP ?May 5, 2020 at 3:25 pm #27112T0mX06
ParticipantYou cannot access this content.
May 6, 2020 at 8:02 am #27122Ernest Marcinko
KeymasterOh, I’m sorry, I missunderstood your query.
You wanted to display the store data in the results content or title fields, and not search by that data. Since you added back-end access, I made another quick code to make it work, and already included in the functions.php file.
Now, use the store user meta fields in the advanced title and content fields like this:
||store_name||
..the new code will recognize that: https://i.imgur.com/UnFp2Ix.pngFor future reference, I added this code there:
// Vendor user meta fields to advanced title and content add_filter('asp_results', 'vendor_asp_post_content_user_meta', 10, 1); function vendor_asp_post_content_user_meta($results) { $meta_fields = array( '_store_description', 'store_name', 'phone', 'store_email', 'wcfm_vendor_store_hours', '_wcfm_email_verified', '_wcfm_email_verified_for', 'wcemailverified', '_wcfm_billing_phone', 'payment', 'wcfm_store_hours', 'wcfm_shipping_policy', 'wcfm_vacation_mode', '_wcfm_store_location', 'wcfmmp_store_name' ); foreach ( $results as &$r ) { foreach ( $meta_fields as $field ) { if ( strpos($r->content, '||' . $field . '||') !== false ) { $s = get_user_meta( get_post_field( 'post_author', $r->id ), $field, true ); if ( !empty($s) ) { $r->content = str_replace('||' . $field . '||', $s, $r->content); } else { $r->content = str_replace('||' . $field . '||', '', $r->content); } } } } return $results; }May 6, 2020 at 11:23 am #27131T0mX06
ParticipantThank you so much ! That’s perfect. 5 stars customer support
Will the changes made will still work with the next updates of ASP ?
Last question, may I have to keep the index search engine or I could use the normal one ?Ok i’ve tested and it works with the classic search engine 😉-
This reply was modified 6 years ago by
T0mX06.
May 6, 2020 at 11:25 am #27132T0mX06
Participantoh, and do you think that I could use the store logos instead of the default avatar ?
I think its store-avatar that is used or gravatar that is listed in the user metadas-
This reply was modified 6 years ago by
T0mX06.
May 6, 2020 at 1:08 pm #27137Ernest Marcinko
KeymasterThank you very much 🙂
Yes, it could be doable, depends on how exactly is that avater stored. I tried to look for user meta with the avatars, but there are no matches for “avatar”, “logo”, “image” – so I don’t know where those could be stored. Let me know if you find out, and I will suggest another custom code to fix that.
May 6, 2020 at 1:37 pm #27141T0mX06
ParticipantThe only thing i’ve found for now is that. Like the image is associated with a gravatar number.
wcfmmp_profile_settings
array (
0 =>
array (
‘user_name’ => ‘Boutique Demo 3’,
‘user_email’ => ’[email protected]’,
‘first_name’ => ‘Boutique Démo 3 Prénom’,
‘last_name’ => ‘Boutique Démo 3 Nom’,
‘store_name’ => ‘Boutique Démo 3’,
‘store_slug’ => ’boutique-demo-3′,
‘store_email’ => ’[email protected]’,
‘phone’ => ‘0490000000’,
‘vendor_id’ => ‘99999’,
‘gravatar’ => ‘254’,
‘banner_type’ => ‘single_img’,
‘banner’ => ”,
‘banner_video’ => ”,
‘banner_slider’ =>
array (
0 =>
array (
‘image’ => ”,
‘link’ => ”,
),
),May 6, 2020 at 3:23 pm #27145Ernest Marcinko
KeymasterWell, try this custom code then:
add_filter( 'asp_results', 'asp_custom_images_gr', 10, 4 ); function asp_custom_images_gr( $results, $id, $is_ajax, $args ) { foreach ($results as $k=>&$r) { if ( $r->content_type == 'user' ) { $s = get_user_meta( $r->id, 'wcfmmp_profile_settings', true ); if ( isset($s['gravatar']) ) { $r->image = get_avatar_url($s['gravatar']); } } } return $results; }I suspect that this may not work though, usually gravatars are handled by WordPress internally, and the search should already return them when they are enabled – but it’s worth a try.
May 6, 2020 at 4:33 pm #27149T0mX06
ParticipantTried and not working :(. Like you say it worth the try.
I’ve found this perheaps it could help ?
https://wclovers.com/forums/topic/display-the-store-logo-and-the-avatar-of-the-vendor/ -
This reply was modified 6 years ago by
-
AuthorPosts
- The topic ‘Use of users custom fields in a product search’ is closed to new replies.