-1

I've been searching around but can't find the solution. I'm trying to show some user meta values for current logged in user in a shortcode.

The meta key is

_wc_braintree_credit_card_payment_tokens

The example values are

a:1:{s:7:"bhaddfh";a:7:{s:7:"default";b:0;s:4:"type";s:11:"credit_card";s:9:"last_four";s:4:"2122";s:9:"card_type";s:10:"mastercard";s:9:"exp_month";s:2:"10";s:8:"exp_year";s:4:"2022";s:18:"billing_address_id";s:2:"sz";}}

How do I display "last_four" value, and "card_type" value (which is mastercard in the example here) in a shortcode?

To display them on the frontend for the current user to see. Maximum one latest card saved in their payment method list (if there are more than one saved).

Thank you.

0

You didn't mention whether users could only reach this page when logged in, so this example includes a check to make sure the current visitor is logged in, otherwise it won't output anything.

<?php
// create the shortcode
add_shortcode('wpse_312268_show_cc_info', 'create_wpse_312268_shortcode');
function create_wpse_312268_shortcode() {
    // only do something if user is logged in
    if(is_user_logged_in()) {
        // get their id
        $user_id = get_current_user_id();
        // get the specific usermeta value as an array
        $cc_meta = get_user_meta($user_id, '_wc_braintree_credit_card_payment_tokens', false);
        // get specific values from the array
        $last_four = $cc_meta['last_four'];
        $card_type = $cc_meta['card_type'];
        // only show if the values aren't empty
        if(!empty($last_four) && !empty($card_type)) {
            // display with whatever markup you like
            echo "<div class="cc_info">$card_type ending in
                $last_four</div>";
        }
    }
}
?>

Depending on your needs you may also want to make the text translatable.

  • Thank you. I've just tried the code above, changed the echo to echo '<div class="cc_info">' . $card_type . 'ending in' . $last_four . '</div>'; to make it works. But the card type and last four are not showing up on the frontend, just return empty. Why? I use this plugin for the payment method: wordpress.org/plugins/… – Alief Aug 23 '18 at 14:10
  • I don't have the same plugins you do so I can't test on my end, but I would suggest simplifying the example code - start with just the if(is_user_logged_in()) and have it echo something simple. Then if that works, add in the get_user_meta and do a var_dump of $cc_meta, and so on and so forth. – WebElaine Aug 23 '18 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.