当用户购买特定产品时,我如何限制部分帖子/页面内容?
例如:
some content
...
[wcm_restrict product_id="5"]
this content only show for users that bought product 5
[/wcm_restrict]
...
some other content
我尝试了许多插件,但不幸的是它们没有这个功能。有什么帮助吗?当用户购买特定产品时,我如何限制部分帖子/页面内容?
例如:
some content
...
[wcm_restrict product_id="5"]
this content only show for users that bought product 5
[/wcm_restrict]
...
some other content
我尝试了许多插件,但不幸的是它们没有这个功能。有什么帮助吗?嗯,我在回答我的问题。
正如@MilanPetrovic在评论中提到的,这很容易做到。只需创建短代码并检查用户是否购买了特定的产品。
代码如下:
/**
* [wcr_shortcode description]
* @param array pid product id from short code
* @return content shortcode content if user bought product
*/
function wcr_shortcode($atts = [], $content = null, $tag = \'\')
{
// normalize attribute keys, lowercase
$atts = array_change_key_case((array) $atts, CASE_LOWER);
// start output
$o = \'\';
// start box
$o .= \'<div class="wcr-box">\';
$current_user = wp_get_current_user();
if ( current_user_can(\'administrator\') || wc_customer_bought_product($current_user->email, $current_user->ID, $atts[\'pid\'])) {
// enclosing tags
if (!is_null($content)) {
// secure output by executing the_content filter hook on $content
$o .= apply_filters(\'the_content\', $content);
}
} else {
// User doesn\'t bought this product and not an administator
}
// end box
$o .= \'</div>\';
// return output
return $o;
}
add_shortcode(\'wcr\', \'wcr_shortcode\');
短代码用法示例:[wcr pid="72"]
This content only show for users that bought product with the id #72
[/wcr]
参考文献Shortcodes with ParametersWooCommerce提供function 这样做。
$current_user = wp_get_current_user();
if ( ! wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->id ) ) {
_e( \'This content is only visible for users who bought this product.\' );
}
我很难让插件正常工作Virtual Pages (WordPress插件可简化虚拟页面的创建)我确实进行了编辑,根据查询创建了一个循环。add_action( \'gm_virtual_pages\', function( $controller ) { /* Creating virtuals pages for companies */ $args = array( \'post_type\' => array(\'companies\',), \'post_status\'