Change of user group when a certain amount of purchases is reached



  • Hello, everyone. Please help me with the task.

    I need to build the following logic: With a certain amount of purchase, the user group should change.

    I'm trying to do the next thing.

    $customer = new WC_Customer( $user_id ); // Получю йд пользоветяля
    $total_spent = $customer->get_total_spent(); // Получаю сумму покупок
    

    if ($total_spent > 1000) {
    //меняю группу...
    }

    The problem is that $total_spent There's always 0, although Adminca shows another amount of purchases. Explain how this logic can be implemented in other ways or improve mine.

    Thank you.

    How does it work? $customer->get_total_spent()

    /**

    • Get total spent by customer.
    • @param int $user_id User ID.
    • @return string
      */
      function wc_get_customer_total_spent( $user_id ) {
      $customer = new WC_Customer( $user_id );
      return $customer->get_total_spent();
      }


  • For buyers in WooCommerce, there is a function. wc_get_customer_total_spent( $user_id )♪ There are two excellent examples in the responses on different tracks, how to change the status of laser. https://stackoverflow.com/questions/44770912/woocommerce-change-user-role-on-purchase and https://wordpress.stackexchange.com/questions/120656/woocommerce-change-user-role-after-completing-order ♪ put a check on it ♪

    add_action( 'woocommerce_order_status_processing', 'change_role_on_purchase' );
    

    or

    add_action( 'woocommerce_order_status_completed','change_role_on_purchase' );
    

    It's just that we need to hang a check (wc_get_customer_total_spent) for the current user if it's authorised or from a verifiable warrant.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2