简体   繁体   中英

Adding content after add to cart button on woocommerce single page

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?

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>';

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM