Skip to content

Commit

Permalink
Hook up the payment token API to the my account > payment methods tab
Browse files Browse the repository at this point in the history
  • Loading branch information
justinshreve committed Mar 1, 2016
1 parent 2e73486 commit 9db1f65
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
14 changes: 11 additions & 3 deletions includes/shortcodes/class-wc-shortcode-my-account.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,25 +351,29 @@ public static function add_payment_method() {
* @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();
}

/**
Expand All @@ -378,25 +382,29 @@ private static function delete_payment_method( $id ) {
* @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();
}

}
13 changes: 13 additions & 0 deletions includes/wc-account-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 2 additions & 0 deletions includes/wc-template-hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,5 @@
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' ) );
41 changes: 31 additions & 10 deletions templates/myaccount/payment-methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

$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' ); ?>
Expand All @@ -43,16 +43,37 @@
<?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>

Expand Down

0 comments on commit 9db1f65

Please sign in to comment.