Add variable attributes to search description

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Add variable attributes to search description

This topic contains 3 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 4 years, 11 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #22062
    weelhaus
    weelhaus
    Participant

    Is there a way I can add variable attributes to the content description on the search results so it would look like this:

    And if a product doesn’t have any variable attributes, then it would be empty as below:

    Product 1
    Color: Green, Size: XL

    Product 2

    #22069
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    Yes, it is possible. Usually these attributes are either taxonomy terms or custom fields. Using the Advanced title and content fields you can print taxonomies and custom field values to the results.

    In your case, to find the taxonomy names of the variable attributes, you can use the taxonomy list under the General Options -> Sources 2 panel, here: https://i.imgur.com/bjHRMCd.png
    In my case the taxonomy name is pa_color (as product attribute color). Now with the advanced fields, you can use that to print out the colors, like so:

    [Color: {_taxonomy_pa_color}]

    If you check the documentation above first, it will make more sense. Let me know if you have any questions.

    Best,
    Ernest Marcinko

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


    #22111
    weelhaus
    weelhaus
    Participant

    Hi,
    Well, I don’t have any attributes in the system.

    All the attributes are based on the variations. So what I would prefer is to pull up all the variation attribute data.

    Not all products have a color for example. So if I hardcode color into search results, several products will have no color listed.

    I have hundreds of products with different variable attributes, so I just want in the results to show any attributes for the variations depending on the product.

    Does that make sense?

    #22120
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    Yes, sure. Well, I had to google around, and maybe there is a way to list those attributes for variations via a custom code. Try adding this custom code to the functions.php in your theme/child theme directory (copy from line 3 only!). Before editing, please make sure to have a full site back-up just in case!

    add_filter('asp_results', 'asp_get_prod_atts_table', 10, 1);
    function asp_get_prod_atts_table($results) {
      foreach ($results as $k => &$r) {
    
        if ( !isset($r->post_type) || $r->post_type != 'product_variation' )
          continue;
    
        $product = wc_get_product($r->id);
        $r->content =  wc_get_formatted_variation( $product->get_variation_attributes(), true ) . '<br>' . $r->content;
      }
    
      return $results;
    }

    This might not be the best possible solution, but this is the only way to simply list the attributes according to the WooCommerce API.

    Best,
    Ernest Marcinko

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


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

You must be logged in to reply to this topic.