Trying to append content to Short Description in WooCommerce

Alicia St Rose
Alicia St Rose used Ask the Experts™
on
Hello!

I'm trying to add some content to the short description in the single product view. I've created a div that contains Post Objects from an Advanced Custom Fields Post Object field.

I've got the code below, but instead of appending, it's actually replacing. I can see why, but I can't figure out how to bring back the summary in the code.

<?php add_filter('woocommerce_short_description','ts_add_text_short_descr');
function ts_add_text_short_descr($description){
	$post_objects = get_field( 'juices_included_in_the_cleanse' );
			if ( $post_objects ): ?>
				<div class="cleanse-juices">
					<ul>
			
			 
				<?php foreach ( $post_objects as $post ):  ?>
					<?php setup_postdata( $post ); ?>
					<li>
						<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
					</li>
				<?php endforeach; ?>
				<?php wp_reset_postdata(); ?>
			 </ul>
			<?php endif; ?>
		</div>
	</div>
  
<?php }
?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Hey Alicia,

That hook is ran when WordPress needs to output the short description. Your filter is taking in the existing description (the $description variables that's passed into the function), but then it's only ever outputting your own code. If you want to append your HTML to the existing description, then you need to make sure you include that in the output as well.

Also, when using filters like this, you should be returning the content, not just echoing it out:

<?php
add_filter('woocommerce_short_description','ts_add_text_short_descr');
function ts_add_text_short_descr($description){
    $post_objects = get_field( 'juices_included_in_the_cleanse' );
    
    ob_start();

    if ( $post_objects ): ?>
        <div class="cleanse-juices">
            <ul>
                <?php foreach ( $post_objects as $post ):  ?>
                <?php setup_postdata( $post ); ?>
                <li>
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </li>
                <?php endforeach; ?>
                <?php wp_reset_postdata(); ?>
            </ul>
        </div>
    <?php endif; ?>

    $newCode = ob_get_clean();
    $newDescription = $description . $newCode;

    return $newDescription;  
<?php }
?>

Open in new window

What we've done here is enabled the output buffer. This means that the code you've created won't be echoed out. Instead we capture it into a variable called $newCode. We then string the existing $description and your new code in a variable called $newDescription and we return that. If you want your new code to come before the existing description, just change the order of the string concat:

$newDescription = $newCode . $description;
Alicia St RoseOwner & Principle Developer/Designer

Author

Commented:
Hi This is working, however, I'm only seeing the name of the actual post listed three times no matter how many or what juices I add in the Post Object custom field. Not sure what going wrong..

The-Rainbow---Juice-Ranch-2.jpg
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Hey Alicia,

Without knowing what you've set up in the ACF Field, I can't be sure what's going on here. Try it without initialising the postdata:

<ul>
    <?php foreach ( $post_objects as $post_object ):  ?>
    <li>
        <a href="<?php echo get_permalink($post_object->ID) ?>"><?php echo get_the_title($post_object->ID) ?></a>
    </li>
    <?php endforeach; ?>
</ul>

Open in new window

C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Alicia St RoseOwner & Principle Developer/Designer

Author

Commented:
Here's a snapshot of my ACF settings

Edit-Field-Group---Juice-Ranch---Wor.jpg
Alicia St RoseOwner & Principle Developer/Designer

Author

Commented:
I just added your new code, correctly and it worked.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
OK. That all looks OK. Now on a particular product page, make sure you've selected some Posts that have the correct titles you want to display. Then test the new code I've posted. The code should work, but it looks like you've just selected additional products that have the same title, which is why you're seeing the same title several times.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Ah. Brilliant :)

Glad it's working
Alicia St RoseOwner & Principle Developer/Designer

Author

Commented:
Actually, they were all different, but were somehow pulling in the current product/post title instead of the post objects I'd selected.

Now, I've got to remove the list from the Variation Description. Looks like the description hook works on both...

The-Caveman---Juice-Ranch.jpg
Alicia St RoseOwner & Principle Developer/Designer

Author

Commented:
I may just do a display: none; on that one!
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Hey Alicia,

I think you have too. Just done a quick search and it looks like a bug in WooCommerce. The solutions provided seem to suggest hiding it with CSS. Not ideal, but it should work fine.
Alicia St RoseOwner & Principle Developer/Designer

Author

Commented:
Ah, good to know!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial