Woocommerce 根据客户总购买金额切换用户角色

问题描述 投票:0回答:1

我正在使用这里的代码。

达到购买金额后,用户的角色会发生变化。 我尝试扩展它并检查一些条件:

  1. 必须验证购买的期限为 2 年。如果2年内购买金额不等于5000,则必须重新计算状态,并根据其订单金额为其分配角色。 以下是我计划使用的金额:

$threshold_amount_to_silver = 4999.99;

$threshold_amount_to_gold = 9999.99;

$threshold_amount_to_platinum = 14999.99;

  1. 购买金额应仅包括具有
    completed
    状态的订单,如果订单被取消或状态与已完成状态有所不同,则应减少金额并重新计算订单状态。如果退货,则必须从购买金额中减去退款金额。
  2. 仅在角色之间进行重新计算:
    subscriber
    customer
    karta-silver
    karta-gold
    karta-platinum

我尝试使用银色条件作为示例将我自己的更改添加到代码中。 但它不起作用,并且角色不会重新计算。 请帮忙。

function bbloomer_maybe_trigger_switch_user_role( $order_id ) {
   $order = wc_get_order( $order_id );
   $user_id = $order->get_user_id();
   $order_status = $order->get_status();
   $switch_already_done = $order->get_meta( '_bb_role_switched' );

   $roles_to_check = array( 'subscriber', 'customer', 'karta-platinum', 'karta-gold', 'karta-silver' );
   if ( ! $switch_already_done && $order->has_status( wc_get_is_paid_statuses() ) ) {
      foreach ( $roles_to_check as $role ) {
         if ( wc_user_has_role( $user_id, $role ) ) {
            bbloomer_customer_maybe_upgrade_to_silver( $user_id );
            $order->update_meta_data( '_bb_role_switched', 'true' );
            $order->save();
            break; 
         }
      }
   }
}

function bbloomer_customer_maybe_upgrade_to_silver( $user_id ) {
   $threshold_amount_to_silver = 4999.99; 
   $threshold_date  = date('Y-m-d', strtotime('-2 years')); 
   $total_spent = wc_get_customer_total_spent( $user_id, $threshold_date ); 
   if ( $total_spent > $threshold_amount_to_silver ) {
      $user = new WP_User( $user_id );
      $user->add_role( 'karta-silver' );
      $user->remove_role( 'subscriber' );
      $user->remove_role( 'customer' );
      $user->remove_role( 'karta-gold' );
      $user->remove_role( 'karta-platinum' );
   }
}
php wordpress woocommerce orders user-roles
1个回答
0
投票

以下将根据客户总购买金额(过去 2 年)添加/删除自定义用户角色。

保持 WooCommerce/Subscriptions 分配的用户角色(“客户”或“订阅者”)并仅添加或删除您的自定义用户角色非常重要。

代码将:

  • 每日检查/调整客户拜访时的用户角色,
  • 客户订单完成后检查/调整用户角色。

尝试以下(已评论):

// Utility function: Get customer completed orders total amount on a period
function get_customer_orders_total( $user_id, $period = '2 years' ) {
    global $wpdb;
    return $wpdb->get_var( $wpdb->prepare( "
        SELECT SUM(total_amount)
        FROM {$wpdb->prefix}wc_orders
        WHERE status = 'wc-completed'
        AND customer_id = %d
        AND date_created_gmt >= '%s'
    ", $user_id, date('Y-m-d H:i:s', strtotime('-'.$period) ) ) );
}

// Utility function: Maybe adjust user roles based on a total amount
function maybe_adjust_user_role( $user_id ) {
    $user = get_userdata( $user_id ); // Get the WP_user object

    // Exit if admins or shop manager users
    if ( array_intersect(['administrator', 'shop_manager'], $user->roles) ) return;
    
    // Get customer completed orders total purchased amount on last 2 years
    $total_purchased_amount = get_customer_orders_total( $user_id );

    if ( $total_purchased_amount > 15000 ) {
        $targeted_role = 'karta-platinium';
    } elseif ( $total_purchased_amount > 10000 ) {
        $targeted_role = 'karta-gold';
    } elseif ( $total_purchased_amount > 5000 ) {
        $targeted_role = 'karta-silver';
    }

    if ( count($user->roles) > 1 && ! isset($targeted_role) ) {
        if ( in_array('customer', $user->roles) ) {
            $user->set_role('customer'); // We only keep "customer" user role
        } elseif ( in_array('subscriber', $user->roles) ) {
            $user->set_role('subscriber'); // We only keep "subscriber" user role
        }
    } 
    elseif ( isset($targeted_role) && ! in_array($targeted_role, $user->roles) ) {
        if ( in_array('customer', $user->roles) ) {
            $user->set_role('customer'); // We only keep "customer" user role
        } elseif ( in_array('subscriber', $user->roles) ) {
            $user->set_role('subscriber'); // We only keep "subscriber" user role
        }
        $user->add_role($targeted_role); // and we add the targeted user role
    }
}

// Maybe adjust users roles when an order get the completed status
add_action('woocommerce_order_status_completed', 'order_completed_maybe_update_user_role', 10, 2); 
function order_completed_maybe_update_user_role( $order_id, $order ) {
    $user_id = $order->get_user_id();
    
    if ( $user_id > 0 ) {
        maybe_adjust_user_role( $user_id );
    }
}

// Maybe adjust users roles daily (only) on customer visit
add_action('template_redirect', 'front_end_maybe_update_user_role', 10, 2);
function front_end_maybe_update_user_role() {
    if ( ! is_user_logged_in() ) return;

    $user = wp_get_current_user();

    if ( $user->daily_roles_check != date('Y-m-d') ) {
        maybe_adjust_user_role( $user->ID );
        update_user_meta($user->ID, 'check_roles_day', date('Y-m-d')); // Tag user as roles checked
    }
}

代码位于活动子主题(或活动主题)的functions.php 文件中。应该可以。

© www.soinside.com 2019 - 2024. All rights reserved.