din*_*o_d 4 php wordpress hook woocommerce
我已经在单个产品页面上的简短描述后成功添加了一个内容
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);
Run Code Online (Sandbox Code Playgroud)
我看到short-description.php那里有apply_filters( 'woocommerce_short_description', $post->post_excerpt )
所以我迷上了.
以同样的方式,我想在添加到购物车按钮后添加内容,所以我找到了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');
Run Code Online (Sandbox Code Playgroud)
但没有任何反应.我可以只挂钩里面的钩子apply_filters吗?从我到目前为止通过使用钩子所理解的是,你只需要一个挂钩名称来挂钩,就是这样.第一个是过滤器钩子,所以我使用了add_filter,第二个是动作钩子所以我应该使用add_action,所有都应该工作.那为什么不呢?
Wis*_*abs 13
在这里,您需要回显内容,因为它是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>';
}
Run Code Online (Sandbox Code Playgroud)