Hook up the payment token API to the my account > payment methods tab

This commit is contained in:
Justin Shreve 2016-02-16 11:48:04 -08:00
parent 2e73486004
commit 9db1f65196
4 changed files with 57 additions and 13 deletions

View File

@ -351,25 +351,29 @@ class WC_Shortcode_My_Account {
* @since 2.6
* @param int $id Payment Token ID
*/
private static function delete_payment_method( $id ) {
public static function delete_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'], 'delete-payment-method-' . $id ) ) {
wc_add_notice( __( 'Invalid payment method', 'woocommerce' ), 'error' );
woocommerce_account_payment_methods();
return false;
}
WC_Payment_Tokens::delete( $id );
woocommerce_account_payment_methods();
}
/**
@ -378,25 +382,29 @@ class WC_Shortcode_My_Account {
* @since 2.6
* @param int $id Payment Token ID
*/
private static function set_default_payment_method( $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(), $id );
WC_Payment_Tokens::set_users_default( $token->get_user_id(), intval( $id ) );
woocommerce_account_payment_methods();
}
}

View File

@ -209,6 +209,19 @@ function wc_get_account_payment_methods_columns() {
) );
}
/**
* Get My Account > Payment methods types
*
* @since 2.6.0
* @return array
*/
function wc_get_account_payment_methods_types() {
return apply_filters( 'woocommerce_payment_methods_types', array(
'cc' => __( 'Credit Card', 'woocommerce' ),
'echeck' => __( 'eCheck', 'woocommerce' ),
) );
}
/**
* Returns an array of a user's saved payments list for output on the account tab.
*

View File

@ -250,3 +250,5 @@ add_action( 'woocommerce_account_edit-address_endpoint', 'woocommerce_account_ed
add_action( 'woocommerce_account_payment-methods_endpoint', 'woocommerce_account_payment_methods' );
add_action( 'woocommerce_account_add-payment-method_endpoint', 'woocommerce_account_add_payment_method' );
add_action( 'woocommerce_account_edit-account_endpoint', 'woocommerce_account_edit_account' );
add_action( 'woocommerce_account_set-default-payment-method_endpoint', array( 'WC_Shortcode_My_Account', 'set_default_payment_method' ) );
add_action( 'woocommerce_account_delete-payment-method_endpoint', array( 'WC_Shortcode_My_Account', 'delete_payment_method' ) );

View File

@ -24,7 +24,7 @@ if ( ! defined( 'ABSPATH' ) ) {
$saved_methods = wc_get_customer_saved_methods_list( get_current_user_id() );
$has_methods = (bool) $saved_methods;
$types = wc_get_account_payment_methods_types();
wc_print_notices(); ?>
<?php wc_get_template( 'myaccount/navigation.php' ); ?>
@ -43,16 +43,37 @@ wc_print_notices(); ?>
<?php endforeach; ?>
</tr>
</thead>
<?php foreach ( $saved_methods as $method ) : ?>
<tr class="method">
<?php foreach ( wc_get_account_payment_methods_columns() as $column_id => $column_name ) : ?>
<td class="payment-method-<?php echo esc_attr( $column_id ); ?>" data-title="<?php echo esc_attr( $column_name ); ?>">
<?php
// @TODO
?>
</td>
<?php endforeach; ?>
<?php foreach ( $saved_methods as $type => $methods ) : ?>
<tr>
<th colspan='3'>
<?php echo esc_html( $types[ $type ] );?>
</th>
</tr>
<?php foreach ( $methods as $method ) : ?>
<tr class="method">
<?php foreach ( wc_get_account_payment_methods_columns() as $column_id => $column_name ) : ?>
<td class="payment-method-<?php echo esc_attr( $column_id ); ?>" data-title="<?php echo esc_attr( $column_name ); ?>">
<?php
if ( has_action( 'woocommerce_account_payment_methods_column_' . $column_id ) ) {
do_action( 'woocommerce_account_payment_methods_column_' . $column_id, $method );
} else if ( 'method' === $column_id ) {
if ( ! empty ( $method['method']['last4'] ) ) {
echo sprintf( __( '%s ending in %s', 'woocommerce' ), esc_html( $method['method']['brand'] ), esc_html( $method['method']['last4'] ) );
} else {
echo esc_html( $method['method']['brand'] );
}
} else if ( 'expires' === $column_id ) {
echo esc_html( $method['expires'] );
} else if ( 'actions' === $column_id ) {
foreach ( $method['actions'] as $key => $action ) {
echo '<a href="' . esc_url( $action['url'] ) . '" class="button ' . sanitize_html_class( $key ) . '">' . esc_html( $action['name'] ) . '</a>&nbsp;';
}
}
?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
</table>