Adding content after add to cart button on woocommerce single page
Solution 1
Here, you need to echo content as it is add_action hook.
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart_button_func' );
/*
* Content below "Add to cart" Button.
*/
function add_content_after_addtocart_button_func() {
// Echo content.
echo '<div class="second_content">Other content here!</div>';
}
Solution 2
You need to do echo instead of return.
add_action( 'woocommerce_after_add_to_cart_button', 'ybc_after_add_to_cart_btn' );
function ybc_after_add_to_cart_btn(){
//add text OR HTML here
echo '<p>After custom text here</p>';
}
If you want the same thing on the shop archive page then you need to use the woocommerce_loop_add_to_cart_link
filter to modify the add to cart button.
dingo_d
Busines owner of D-Coded Consulting trade. Author at madebydenis.com, a site where I write whatever comes to mind. There are a lot of tutorials for WordPress there as well. Core contributor, plugin, and theme author on wordpress.org: Talks Add on for The Events Calendar plugin Simple Linked Variations for WooCommerce plugin Woo Solo Api plugin Expire theme Theme sniffer WPThemeReview coding standards I also did a simple plugin for Sublime Text - CSS Table of Contents Articles Headless WordPress: The Ups And Downs Of Creating A Decoupled WordPress Github: LINK Codepen profile: LINK My gist: LINK
Updated on July 05, 2022Comments
-
dingo_d almost 2 years
I have successfully added a content after short description on single product page with
if (!function_exists('my_content')) { function my_content( $content ) { $content .= '<div class="custom_content">Custom content!</div>'; return $content; } } add_filter('woocommerce_short_description', 'my_content', 10, 2);
I saw that in
short-description.php
there wasapply_filters( 'woocommerce_short_description', $post->post_excerpt )
so I hooked to that.
In the same way, I'd like to add a content after the add to cart button, so I found
do_action( 'woocommerce_before_add_to_cart_button' )
, and now I am hooking towoocommerce_before_add_to_cart_button
. I'm usingif (!function_exists('my_content_second')) { function my_content_second( $content ) { $content .= '<div class="second_content">Other content here!</div>'; return $content; } } add_action('woocommerce_after_add_to_cart_button', 'my_content_second');
But nothing happens. Can I only hook to hooks inside
apply_filters
? From what I've understood so far by working with hooks is that you only need a hook name to hook to and that's it. The first one was a filter hook, so I usedadd_filter
, and the second one is action hook so I should useadd_action
, and all should work. So why doesn't it?