WC_Payment_Token_Data_Store::create()publicWC 3.0.0

Create a new payment token in the database.

Метод класса: WC_Payment_Token_Data_Store{}

Хуки из метода

Возвращает

null. Ничего (null).

Использование

$WC_Payment_Token_Data_Store = new WC_Payment_Token_Data_Store();
$WC_Payment_Token_Data_Store->create( $token );
$token(WC_Payment_Token) (обязательный) (передается по ссылке — &)
Payment token object.

Список изменений

С версии 3.0.0 Введена.

Код WC_Payment_Token_Data_Store::create() WC 8.7.0

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 );
	$this->save_extra_data( $token, true );
	$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_new_payment_token', $token_id, $token );
}