Question regarding custom fields for WooCommerce

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 Ernest Marcinko 3 years, 1 month ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #29799
    Winsa96
    Winsa96
    Participant

    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).

    #29818
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi!

    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.

    Best,
    Ernest Marcinko

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


    #29822
    Winsa96
    Winsa96
    Participant

    Hey 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:”

    #29824
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    No, 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.

    Best,
    Ernest Marcinko

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


    #29825
    Winsa96
    Winsa96
    Participant

    Hey 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.
    #29827
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Nope, 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 :)


    #29828
    Winsa96
    Winsa96
    Participant

    I 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.
    #29832
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Maybe 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;
    }
    Best,
    Ernest Marcinko

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


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

You must be logged in to reply to this topic.