admin管理员组文章数量:1122832
According this answer i want to count orders of each user, and if count > 3 for example, change role to another.
After searching i found this hook
I have modified function
function wpa_120656_convert_paying_customer( $order_id ) {
$order = new WC_Order( $order_id );
$int = wc_get_customer_order_count( $user_id );
if ( $int->user_id > 2 ) {
update_user_meta( $order->user_id, 'paying_customer', 1 );
$user = new WP_User( $order->user_id );
// Remove role
$user->remove_role( 'customer' );
// Add role
$user->add_role( 'dovclient' );
}
}
add_action( 'woocommerce_order_status_completed', 'wpa_120656_convert_paying_customer' );
But it doesn't work... Help me to find a mistake, please!
According this answer i want to count orders of each user, and if count > 3 for example, change role to another.
After searching i found this hook
I have modified function
function wpa_120656_convert_paying_customer( $order_id ) {
$order = new WC_Order( $order_id );
$int = wc_get_customer_order_count( $user_id );
if ( $int->user_id > 2 ) {
update_user_meta( $order->user_id, 'paying_customer', 1 );
$user = new WP_User( $order->user_id );
// Remove role
$user->remove_role( 'customer' );
// Add role
$user->add_role( 'dovclient' );
}
}
add_action( 'woocommerce_order_status_completed', 'wpa_120656_convert_paying_customer' );
But it doesn't work... Help me to find a mistake, please!
Share Improve this question edited Aug 19, 2024 at 6:54 Cow 1111 silver badge5 bronze badges asked Feb 15, 2017 at 10:52 Alex PainAlex Pain 34 bronze badges 6- You want to do it for all your current customers that had already orders > 3 or from now, on every time a customer pass the threshold of 3 orders change the role? – Laxmana Commented Feb 15, 2017 at 11:05
- @Laxmana Now my site is testing, and i think the 2 variant pass for me. This function catch new order, catch user id, if user id has 3 or more orders, change status. – Alex Pain Commented Feb 15, 2017 at 11:09
- Ok and why isn't working? What is the problem? Have you test if the action is being fired? – Laxmana Commented Feb 15, 2017 at 11:12
- @Laxmana This function doesn't work, i don't know why...may be $int = wc_get_customer_order_count( $user_id ); call not right.... – Alex Pain Commented Feb 15, 2017 at 11:16
- First of all you have to see if the function is being executed, meaning that wordpress is calling the function. You can do this by echo/print_r/error_log inside the function. Secondly check the content of the $int variable by var_dump or print_r – Laxmana Commented Feb 15, 2017 at 11:21
1 Answer
Reset to default 0Thank you for all people that help me to find an answer (0 helpers)! I found a solution on my own.
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',
) );
if ( $customer_orders > 1 ) {
update_user_meta( $order->user_id, 'paying_customer', 1 );
$user = new WP_User( $order->user_id );
// Remove role
$user->remove_role( 'customer' );
// Add role
$user->add_role( 'dovclient' );
}
}
add_action( 'woocommerce_order_status_completed', 'wpa_120656_convert_paying_customer' );
本文标签: hooksChange user role if it39s orders count more than
版权声明:本文标题:hooks - Change user role if it's orders count more than 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736297646a1930056.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论