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 was apply_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 to woocommerce_before_add_to_cart_button
. I'm using
if (!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 used add_filter
, and the second one is action hook so I should use add_action
, and all should work. So why doesn't it?
Go to Appearance -> Customize, then go to WooCommerce -> Add to Cart Buttons to choose your settings. Change the Add To Cart button text and/or select the other options on this screen.
You can find this option by going into WooCommerce > Settings > Advanced > Automatic Adding To Cart. From here – select what products get added and how many. There are many plugins with different features & prices available.
Upload the Button Customizer for WooCommerce plugin to your /wp-content/plugin/ directory or through the Plugin admin section under “add new”. Activate the plugin through the 'Plugins' menu in WordPress. Configure your settings by going to WooCommerce settings, then the Button Customizer menu. That's it!
Firstly, check that the product you are trying to add to the cart is set to 'Visible' in the WooCommerce product settings. If it is not, the Add to Cart button will not show. Secondly, check that your WooCommerce theme is compatible with the version of WooCommerce you are using.
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>';
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With