如何在Paid Membership Pro WordPress插件中获取指定订单详情
Hey there! Let's fix this up so you can retrieve the exact order details you need—invoice number, purchase date, payment method, and card info—from Paid Membership Pro. The code you tried only outputs the $current_user object, which doesn't include order-level specifics, so we'll need to tap into PMPro's order functions instead.
Why Your Original Code Didn't Work
The $current_user object holds user account and membership level data, but not the transaction/order details you're after. PMPro stores orders in its own database table (wp_pmpro_membership_orders), so we need to fetch those directly using PMPro's built-in helper functions.
Working Code Implementation
Here's a complete, commented solution that pulls the details you need, focusing on the user's most recent successful order:
<?php // Check if user is logged in and has an active membership if (is_user_logged_in() && function_exists('pmpro_hasMembershipLevel') && pmpro_hasMembershipLevel()) { global $current_user; // Fetch all successful orders for the current user // You can adjust the status filter if needed (e.g., 'pending', 'cancelled') $orders = pmpro_get_orders_for_user($current_user->ID, array('status' => 'success')); if (!empty($orders)) { // Grab the most recent order (orders are returned in descending order by default) $latest_order = reset($orders); // Extract each detail from the order object $invoice_number = $latest_order->code; // PMPro stores invoice numbers as the 'code' field $purchase_date = date_i18n(get_option('date_format'), strtotime($latest_order->timestamp)); // Format timestamp to match your site's date settings $payment_method = ucwords(str_replace('_', ' ', $latest_order->payment_method)); // Make payment method name more readable // Card details: PMPro only stores last 4 digits and card type (PCI compliance) $card_details = !empty($latest_order->cardtype) ? "{$latest_order->cardtype} ending in {$latest_order->lastfour}" : "No card details available"; // Display the details in a clean format echo "<div class='pmpro-order-details'>"; echo "<h3>Your Membership Order Details</h3>"; echo "<ul>"; echo "<li><strong>Invoice Number:</strong> {$invoice_number}</li>"; echo "<li><strong>Purchase Date:</strong> {$purchase_date}</li>"; echo "<li><strong>Payment Method:</strong> {$payment_method}</li>"; echo "<li><strong>Card Details:</strong> {$card_details}</li>"; echo "</ul>"; echo "</div>"; } else { echo "<p>No successful membership orders were found for your account.</p>"; } } else { echo "<p>Please log in with an active membership account to view your order details.</p>"; } ?>
Key Notes:
- Invoice Number: Stored in the
codeproperty of the order object. - Purchase Date: The
timestampfield is a MySQL datetime string, so we usedate_i18n()to format it to your site's configured date format. - Payment Method: The
payment_methodfield is usually a slug (e.g.,paypal,stripe), so we clean it up withucwords()and replace underscores with spaces. - Card Details: For PCI compliance, PMPro never stores full credit card numbers—only the last 4 digits and card type (e.g., Visa, Mastercard). If the order used a non-card payment method (like PayPal), this field will be empty.
Displaying All Orders (Not Just the Latest)
If you want to show all of the user's successful orders instead of just the most recent, replace the $latest_order = reset($orders); line with a loop:
foreach ($orders as $order) { // Extract details for each order here, then display them $invoice_number = $order->code; // ... rest of the detail extraction ... // Display each order's details echo "<div class='pmpro-single-order'>"; echo "<h4>Order #{$invoice_number}</h4>"; echo "<ul>"; echo "<li><strong>Purchase Date:</strong> " . date_i18n(get_option('date_format'), strtotime($order->timestamp)) . "</li>"; // ... other details ... echo "</ul>"; echo "</div>"; }
内容的提问来源于stack exchange,提问作者Samir Lakhani

