set_users_default() public static method

Loops through all of a users payment tokens and sets is_default to false for all but a specific token.
Since: 2.6.0
public static set_users_default ( integer $user_id, integer $token_id )
$user_id integer User to set a default for
$token_id integer The ID of the token that should be default
 /**
  * Update a payment token.
  *
  * @since 2.7.0
  * @param WC_Payment_Token $token
  */
 public function update(&$token)
 {
     if (false === $token->validate()) {
         throw new Exception(__('Invalid or missing payment token fields.', 'woocommerce'));
     }
     global $wpdb;
     $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->update($wpdb->prefix . 'woocommerce_payment_tokens', $payment_token_data, array('token_id' => $token->get_id('edit')));
     $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->get_id());
     }
     do_action('woocommerce_payment_token_updated', $token->get_id());
 }
 /**
  * Process the delete payment method form.
  */
 public static function set_default_payment_method_action()
 {
     global $wp;
     if (isset($wp->query_vars['set-default-payment-method'])) {
         $token_id = absint($wp->query_vars['set-default-payment-method']);
         $token = WC_Payment_Tokens::get($token_id);
         $delete = true;
         if (is_null($token)) {
             wc_add_notice(__('Invalid payment method.', 'woocommerce'), 'error');
             $delete = false;
         }
         if (get_current_user_id() !== $token->get_user_id()) {
             wc_add_notice(__('Invalid payment method.', 'woocommerce'), 'error');
             $delete = false;
         }
         if (false === wp_verify_nonce($_REQUEST['_wpnonce'], 'set-default-payment-method-' . $token_id)) {
             wc_add_notice(__('Invalid payment method.', 'woocommerce'), 'error');
             $delete = false;
         }
         if ($delete) {
             WC_Payment_Tokens::set_users_default($token->get_user_id(), intval($token_id));
             wc_add_notice(__('This payment method was successfully set as your default.', 'woocommerce'));
         }
         wp_redirect(wc_get_account_endpoint_url('payment-methods'));
         exit;
     }
 }
 /**
  * Create a new payment token in the database.
  * @since 2.6.0
  * @return boolean on success, false if validation failed and a payment token could not be created
  */
 public function create()
 {
     if (false === $this->validate()) {
         return false;
     }
     global $wpdb;
     // Are there any other tokens? If not, set this token as default
     if (!$this->is_default() && $this->get_user_id() > 0) {
         $default_token = WC_Payment_Tokens::get_customer_default_token($this->get_user_id());
         if (is_null($default_token)) {
             $this->set_default(true);
         }
     }
     $payment_token_data = array('gateway_id' => $this->get_gateway_id(), 'token' => $this->get_token(), 'user_id' => $this->get_user_id(), 'type' => $this->get_type());
     $wpdb->insert($wpdb->prefix . 'woocommerce_payment_tokens', $payment_token_data);
     $this->_data['id'] = $token_id = $wpdb->insert_id;
     $this->save_meta_data();
     // Make sure all other tokens are not set to default
     if ($this->is_default() && $this->get_user_id() > 0) {
         WC_Payment_Tokens::set_users_default($this->get_user_id(), $token_id);
     }
     do_action('woocommerce_payment_token_created', $token_id);
     return true;
 }
 /**
  * Test setting a users default token.
  * @since 2.6.0
  */
 function test_wc_payment_tokens_set_users_default()
 {
     $token = WC_Helper_Payment_Token::create_cc_token($this->user_id);
     $token_id = $token->get_id();
     $token->save();
     $token2 = WC_Helper_Payment_Token::create_cc_token($this->user_id);
     $token_id_2 = $token2->get_id();
     $token2->save();
     $this->assertTrue($token->is_default());
     // first created is default
     $this->assertFalse($token2->is_default());
     WC_Payment_Tokens::set_users_default($this->user_id, $token_id_2);
     $token->read($token_id);
     $token2->read($token_id_2);
     $this->assertFalse($token->is_default());
     $this->assertTrue($token2->is_default());
     WC_Payment_Tokens::set_users_default($this->user_id, $token_id);
     $token->read($token_id);
     $token2->read($token_id_2);
     $this->assertTrue($token->is_default());
     $this->assertFalse($token2->is_default());
 }
 /**
  * Sets a payment method as default and displays a message to the user
  *
  * @since  2.6
  * @param  int $id  Payment Token ID
  */
 public static function set_default_payment_method($id)
 {
     $token = WC_Payment_Tokens::get($id);
     if (is_null($token)) {
         wc_add_notice(__('Invalid payment method', 'woocommerce'), 'error');
         woocommerce_account_payment_methods();
         return false;
     }
     if (get_current_user_id() !== $token->get_user_id()) {
         wc_add_notice(__('Invalid payment method', 'woocommerce'), 'error');
         woocommerce_account_payment_methods();
         return false;
     }
     if (false === wp_verify_nonce($_REQUEST['_wpnonce'], 'set-default-payment-method-' . $id)) {
         wc_add_notice(__('Invalid payment method', 'woocommerce'), 'error');
         woocommerce_account_payment_methods();
         return false;
     }
     WC_Payment_Tokens::set_users_default($token->get_user_id(), intval($id));
     wc_add_notice(__('This payment method was successfully set as your default.', 'woocommerce'));
     woocommerce_account_payment_methods();
 }
Example #6
0
 /**
  * Test setting a users default token.
  * @since 2.6.0
  */
 function test_wc_payment_tokens_set_users_default()
 {
     $token = \WC_Helper_Payment_Token::create_cc_token();
     $token_id = $token->get_id();
     $token->set_user_id(1);
     $token->save();
     $token2 = \WC_Helper_Payment_Token::create_cc_token();
     $token_id_2 = $token2->get_id();
     $token2->set_user_id(1);
     $token2->save();
     $this->assertFalse($token->is_default());
     $this->assertFalse($token2->is_default());
     \WC_Payment_Tokens::set_users_default(1, $token_id_2);
     $token->read($token_id);
     $token2->read($token_id_2);
     $this->assertFalse($token->is_default());
     $this->assertTrue($token2->is_default());
     \WC_Payment_Tokens::set_users_default(1, $token_id);
     $token->read($token_id);
     $token2->read($token_id_2);
     $this->assertTrue($token->is_default());
     $this->assertFalse($token2->is_default());
 }