WC_Payment_Tokens::get_customer_default_token PHP Method

get_customer_default_token() public static method

Returns a customers default token or NULL if there is no default token.
Since: 2.6.0
public static get_customer_default_token ( integer $customer_id ) : WC_Payment_Token | null
$customer_id integer
return WC_Payment_Token | null
    public static function get_customer_default_token($customer_id)
    {
        if ($customer_id < 1) {
            return null;
        }
        $data_store = WC_Data_Store::load('payment-token');
        $token = $data_store->get_users_default_token($customer_id);
        if ($token) {
            return self::get($token->token_id, $token);
        } else {
            return null;
        }
    }

Usage Example

 /**
  * Create a new payment token in the database.
  *
  * @since 2.7.0
  * @param WC_Payment_Token $token
  */
 public function create(&$token)
 {
     if (false === $token->validate()) {
         throw new Exception(__('Invalid or missing payment token fields.', 'woocommerce'));
     }
     global $wpdb;
     if (!$token->is_default() && $token->get_user_id() > 0) {
         $default_token = WC_Payment_Tokens::get_customer_default_token($token->get_user_id());
         if (is_null($default_token)) {
             $token->set_default(true);
         }
     }
     $payment_token_data = array('gateway_id' => $token->get_gateway_id('edit'), 'token' => $token->get_token('edit'), 'user_id' => $token->get_user_id('edit'), 'type' => $token->get_type('edit'));
     $wpdb->insert($wpdb->prefix . 'woocommerce_payment_tokens', $payment_token_data);
     $token_id = $wpdb->insert_id;
     $token->set_id($token_id);
     $token->save_meta_data();
     $token->apply_changes();
     // Make sure all other tokens are not set to default
     if ($token->is_default() && $token->get_user_id() > 0) {
         WC_Payment_Tokens::set_users_default($token->get_user_id(), $token_id);
     }
     do_action('woocommerce_payment_token_created', $token_id);
 }
All Usage Examples Of WC_Payment_Tokens::get_customer_default_token