php 在woocommerce单页上添加到购物车按钮后添加内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28875514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:07:09  来源:igfitidea点击:

Adding content after add to cart button on woocommerce single page

phpwordpresswoocommercehook

提问by dingo_d

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.phpthere was apply_filters( 'woocommerce_short_description', $post->post_excerpt )

我看到short-description.php里面有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

同样,我想在添加到购物车按钮后添加一个内容,所以我找到了do_action( 'woocommerce_before_add_to_cart_button' ),现在我正在挂钩到woocommerce_before_add_to_cart_button. 我正在使用

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?

但什么也没有发生。我只能钩在里面的钩子上apply_filters吗?到目前为止,通过使用钩子,我了解到您只需要一个钩子名称即可挂钩,仅此而已。第一个是过滤器钩子,所以我使用了add_filter,第二个是动作钩子,所以我应该使用add_action,一切都应该工作。那为什么不呢?

回答by WisdmLabs

Here, you need to echo content as it is add_action hook.

在这里,您需要回显内容,因为它是 add_action 钩子。

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

}