Php 如何使用wc_paying_customer功能创建短代码并仅向付费客户显示内容
首先,我是一个初学者,不太懂php,所以请原谅我的无知。。! 我不知道如何正确地创建一个短代码,允许我只向已经购买了产品的客户显示内容,使用[CUSTOMER]内容[/CUSTOMER]为他们提供内部帖子,使用[USER]其他内容[/USER]为其他人(尚未购买产品的人)提供内部帖子 我想使用woocommerce中包含的wc_paying_customer功能,如下所示:Php 如何使用wc_paying_customer功能创建短代码并仅向付费客户显示内容,php,wordpress,function,woocommerce,shortcode,Php,Wordpress,Function,Woocommerce,Shortcode,首先,我是一个初学者,不太懂php,所以请原谅我的无知。。! 我不知道如何正确地创建一个短代码,允许我只向已经购买了产品的客户显示内容,使用[CUSTOMER]内容[/CUSTOMER]为他们提供内部帖子,使用[USER]其他内容[/USER]为其他人(尚未购买产品的人)提供内部帖子 我想使用woocommerce中包含的wc_paying_customer功能,如下所示: function wc_paying_customer( $order_id ) { $order = wc_get_o
function wc_paying_customer( $order_id ) {
$order = wc_get_order( $order_id );
if ( $order->user_id > 0 ) {
update_user_meta( $order->user_id, 'paying_customer', 1 );
$old_spent = absint( get_user_meta( $order->user_id, '_money_spent', true ) );
update_user_meta( $order->user_id, '_money_spent', $old_spent + $order->order_total );
$old_count = absint( get_user_meta( $order->user_id, '_order_count', true ) );
update_user_meta( $order->user_id, '_order_count', $old_count + 1 );
}
}
链接:及
因此,我在php函数中添加了以下内容:
add_shortcode('CUSTOMER','check_customer');
function check_customer($atts,$content=""){
if (wc_paying_customer()){
return do_shortcode($content);
}
}
add_shortcode('USER','check_user');
function check_user($atts,$content=""){
if (!wc_paying_customer()){
return do_shortcode($content);
}
}
但它似乎不起作用,我也不明白为什么(再说一遍,我对这个-第一个stackoverflow问题-完全是新手,所以我可能犯了一个错误或忘记了什么,但我需要一些帮助来解决它)
谢谢大家!
编辑:我使用了下面Diggy提供的答案,效果很好。非常感谢:)
如果其他人想使用此功能,以下是应添加到自定义函数php文件中的内容:
function so27296867_is_paying_customer()
{
$user_id = get_current_user_id();
if( '' != get_user_meta( $user_id, 'paying_customer', true ) )
return true;
return false;
}
add_shortcode('CUSTOMER','check_customer');
function check_customer($atts,$content=""){
if( so27296867_is_paying_customer() ){
return do_shortcode($content);
}
}
add_shortcode('USER','check_user');
function check_user($atts,$content=""){
if( !so27296867_is_paying_customer() ){
return do_shortcode($content);
}
}
然后在您的页面内容中,使用[CUSTOMER][/CUSTOMER]快捷码和[USER][/USER]显示付款客户的内容。我发现这样做的必要性,因为我允许人们在购买任何东西之前创建一个帐户,但需要他们在登录时已经购买了一个产品才能访问帐户中的其他部分
再次感谢diggy提供了这么好的修复 您可以使用以下功能检查当前用户的
付款客户
用户元字段:
function so27296867_is_paying_customer()
{
$user_id = get_current_user_id();
if( '' != get_user_meta( $user_id, 'paying_customer', true ) )
return true;
return false;
}
并在条件检查中使用该函数:
if( so27296867_is_paying_customer() ){
// current user is paying customer, do stuff
}
非常感谢。我只是在短代码之间用两行不同的内容进行了尝试,一个是登录但尚未购买产品的客户的帐户,另一个是购买商品的客户的帐户,正确的内容会显示给他们每个人。漂亮:)如果其他人想使用它,我会用你的回答和行更新问题,以添加短代码,因为我似乎无法在这个评论中做到这一点。