• Hi, I have a client with several products (and variations) that may not contain a long or short description. I’m wondering if I can apply a filter to add something generic to the feed if a product doesn’t have a long description or short description.

    Here is what I have but I’m getting an error when generating my feed that reads: Call to a member function get_short_description() on bool

    add_filter( 'aepc_feed_item_description', function( $description, $item ) {
      $post = get_post( $item->get_id() );
      if ( ! empty( $post->post_parent ) ) {
        $post = get_post( $post->post_parent );
      }
    
      $product = wc_get_product($post);
      $description = $product->get_short_description();
    
      if ( empty($description ) ) {
        $description = 'Shop ' . $product->get_title();
      }
    
      return $description;
    }, 10, 2 );
Viewing 1 replies (of 1 total)
  • Plugin Author Antonino Scarfì

    (@antoscarface)

    Hi,

    sorry for the silly question, but it’s to be sure: are you using WooCommerce right?

    If so, you may change that snippet a little bit:

    
    add_filter( 'aepc_feed_item_description', function( $description, $item ) {
    	$id = $item->get_id();
    	if ( $item->get_group_id() ) {
    		$id = $item->get_group_id();
    	}
    
    	if ( $product = wc_get_product($id) ) {
    		$description = $product->get_short_description();
    
    		if ( empty($description ) ) {
    			$description = 'Shop ' . $product->get_title();
    		}
    	}
    
    	return $description;
    }, 10, 2 );
    

    Let me know if it solves for you.

Viewing 1 replies (of 1 total)
  • The topic ‘Filter to add something if description is empty’ is closed to new replies.