1
votes

I am trying to have it so for specific product attribute that contain "agave" text is displayed in the short description.

I have tried a few snippets of code but none seem to work. I have no problem getting them to work with Categories but I just want it for certain attributes of the products - Agave

function filter_woocommerce_short_description( $post_excerpt ) {
    global $post;

    if ( has_term( "agave", "categories", $post->ID ) ) {
        $post_excerpt .= "<br/>" . "Text Here";
    }
    return $post_excerpt; 
};

add_filter('woocommerce_short_description', 'filter_woocommerce_short_description',10, 1  );

I expect the text to show up under the certain attributes (Agave) but they do not

I have tried to use this

    add_filter('woocommerce_short_description', 
    'filter_woocommerce_short_description',10, 1  );
    function filter_woocommerce_short_description( $short_description ) {
     global $product;

   $string_values = $product->get_attribute('agave');

   if ( strpos($string_values, 'agave') !== false ) {
  $short_description .= '<br>' . __("Testing This Out - AGAVE");
    }
   return $short_description;
   }
2
Try changing 'categories' to 'product_cat'kisabelle

2 Answers

1
votes

For a specific product attribute "agave" you will use something a bit different:

add_filter('woocommerce_short_description', 'filter_woocommerce_short_description',10, 1  );
function filter_woocommerce_short_description( $short_description ) {
    global $product;

    $string_values = $product->get_attribute('agave');

    if ( ! empty($string_values) ) {
        $short_description .= '<br>' . __("Text Here");
    }
    return $short_description;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Now if "agave" is a term of a product attribute, you should need to set the product attribute name in $product->get_attribute('attribute-name'); and replace the condition by something like:

if ( strpos($string_values, 'agave') !== false ) {

Note: The taxonomy for product category is product_cat, but not categories

0
votes
 add_filter('woocommerce_short_description', 'filter_woocommerce_short_description',10, 1  );
 function filter_woocommerce_short_description( $short_description ) {
     global $product;

   $string_values = $product->get_attribute('agave');
   $agave = $attributes["agave"];
   if ( $agave ) {
        $short_description .= '<br>' . __("Testing This Out - AGAVE");
    }
   return $short_description;
   }