Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Question regarding custom fields for WooCommerce
- This topic has 7 replies, 2 voices, and was last updated 5 years, 7 months ago by
Ernest Marcinko.
-
AuthorPosts
-
October 14, 2020 at 10:29 pm #29799
Winsa96
ParticipantHey 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 #29818Ernest Marcinko
KeymasterHi!
1. % discount
I’m afraid this is not possible without custom coding.2. Well, you can list them via using the
{__tax_product_cat}pseudo variable, but it is not going to be a formatted/nested list, only a list of the categories.October 15, 2020 at 1:34 pm #29822Winsa96
ParticipantHey 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 #29824Ernest Marcinko
KeymasterNo, it should work with any taxonomy. I just tested with WooCommerce product categories, and it looks all right: https://i.imgur.com/EF3d0V1.png
Are you sure that the “product_cat” is the right taxonomy? Some themes may add different taxonomy names with the same label.October 15, 2020 at 1:44 pm #29825Winsa96
ParticipantHey 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
October 15, 2020 at 1:56 pm #29827Ernest Marcinko
KeymasterNope, 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.
October 15, 2020 at 2:00 pm #29828Winsa96
ParticipantI 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.
October 15, 2020 at 3:08 pm #29832Ernest Marcinko
KeymasterMaybe this variation:
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; } -
AuthorPosts
- You must be logged in to reply to this topic.