[英]Woocommerce change role by total of user orders
I have a problem with php realization of automaticaly changing role of user. 我对自动更改用户角色的php实现有疑问。 So i have custom role dovclient
. 所以我有自定义角色dovclient
。 First time user ordering get role Customer
. 首次用户订购获得角色Customer
。
I need to change user role if he has buy products total 500$. 如果他购买的产品总额为500 $,我需要更改用户角色。
So i have a function 所以我有一个功能
function wpa_120656_convert_paying_customer( $order_id ) {
$order = new WC_Order( $order_id );
$user_id = $order->user_id;
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => 'shop_order',
) );
$total = 0;
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order );
$total += $order->get_total();
}
return $total;
if ( $total > 500 ) {
update_user_meta( $order->user_id, 'paying_customer', 1 );
$user = new WP_User( $order->user_id );
$user->remove_role( 'customer' );
$user->add_role( 'dovclient10' );
}
}
add_action( 'woocommerce_order_status_completed', 'wpa_120656_convert_paying_customer' );
I know, that $customer_orders
are counting, and i make function working by count of orders. 我知道, $customer_orders
正在计数,并且我使功能按订单计数工作。 I mean if $customer_orders > 2
for ex., so change role. 我的意思是,例如,如果$customer_orders > 2
,则更改角色。
But with total of order prices i have a problem. 但是总的订单价格我有一个问题。 Role changes not. 角色不会改变。 I don't know why. 我不知道为什么 Maybe problem is in foreach
... 也许问题在foreach
...
Please help me! 请帮我!
Amazing help! 很棒的帮助! 0... ok. 0 ...好 I have done it. 我已经做了。
function wpa_120656_convert_paying_customer( $order_id ) {
$order = new WC_Order( $order_id );
$user_id = $order->user_id;
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => 'shop_order',
) );
$totall = 0;
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order );
$totall += $order->get_total();
}
if ( $totall > 500 ) {
update_user_meta( $order->user_id, 'paying_customer', 1 );
$user = new WP_User( $order->user_id );
if( get_role('customer') ){
$user->remove_role( 'customer' );
$user->add_role( 'dovclient' );
}
}
}
add_action( 'woocommerce_order_status_completed', 'wpa_120656_convert_paying_customer' );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.