Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Question regarding custom fields for WooCommerce
This topic contains 7 replies, has 2 voices, and was last updated by Ernest Marcinko 3 years, 1 month ago.
- AuthorPosts
- October 14, 2020 at 10:29 pm #29799
Hey there! I have a question regarding the custom fields you can use in the advanced titel and description field (https://documentation.ajaxsearchpro.com/advanced-options/advanced-title-and-description-fields)
Is it possible to show the % discount that people are saving on the product?
is it possible to show nested product categories (woocommerce product categories) that the product is attached to?I undestand that these might need custom development, but i just wanted to make sure that these weren’t standard out of the box options, since i can’t read much more about it on the FAQ or the linked page (https://documentation.ajaxsearchpro.com/advanced-options/advanced-title-and-description-fields).
October 15, 2020 at 1:32 pm #29818Hi!
1. % discount
I’m afraid this is not possible without custom coding.2. Well, you can list them via using the
Best,{__tax_product_cat}
pseudo variable, but it is not going to be a formatted/nested list, only a list of the categories.
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
October 15, 2020 at 1:34 pm #29822Hey there Ernest, i’ve tried the {__tax_product_cat} but it seems to not be picking up on the woocommerce product categories (is it only inteded for blog categories?
If not, could you please provide me with a example i can insert? Using the example from the FAQ page just result in empty results after ”categories:”October 15, 2020 at 1:41 pm #29824No, it should work with any taxonomy. I just tested with WooCommerce product categories, and it looks all right: https://i.imgur.com/EF3d0V1.png
Best,
Are you sure that the “product_cat” is the right taxonomy? Some themes may add different taxonomy names with the same label.
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
October 15, 2020 at 1:44 pm #29825Hey again! So sorry, it worked! My question is then, there’s no option to have the product categories clickable out of the box, right? (right now they’re only informative) see attached image
Attachments:
You must be logged in to view attached files.October 15, 2020 at 1:56 pm #29827Nope, that has to be done programatically. I found something similar I suggested to someone else:
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_results', 'asp_add_pc_titles', 10, 1 ); function asp_add_pc_titles( $results ) { $taxonomy = "product_cat"; foreach ($results as $k=>&$r) { if ( isset($r->post_type) ) { // Get the post categories $post_terms = wp_get_object_terms( $r->id, $taxonomy ); $cats = array(); if ( !empty($post_terms) && !is_wp_error($post_terms) ) { // Concatenate category names to the $cats variable foreach($post_terms as $c){ $cats[] = '<a href="'.get_term_link($c->term_id, $taxonomy).'">' . $c->name . '</a>'; } } // Modify the post title if ( !empty($cats) ) $r->content .= "<br>Categories: ".implode(', ', $cats); } } return $pageposts; }
This may not work anymore though.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
October 15, 2020 at 2:00 pm #29828I see, thanks so much for clarifying. We’ll try modify the code provided and try to get it to work, the result didn’t work tho, just attaching the image for you to see.
Attachments:
You must be logged in to view attached files.October 15, 2020 at 3:08 pm #29832Maybe this variation:
Best,add_filter( 'asp_results', 'asp_add_pc_titles', 10, 1 ); function asp_add_pc_titles( $results ) { $taxonomy = "product_cat"; foreach ($results as $k=>&$r) { if ( isset($r->post_type) ) { // Get the post categories $post_terms = wp_get_object_terms( $r->id, $taxonomy ); $cats = array(); if ( !empty($post_terms) && !is_wp_error($post_terms) ) { // Concatenate category names to the $cats variable foreach($post_terms as $c){ $cats[] = '<a href="'.get_term_link($c->term_id, $taxonomy).'">' . $c->name . '</a>'; } } // Modify the post title if ( !empty($cats) ) $r->content .= "<br>Categories: ".implode(', ', $cats); } } return $results; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
- AuthorPosts
You must be logged in to reply to this topic.