Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Added by WooPayments plugin before
- // If NOT Multisite
- function custom_add_transactions_menu() {
- // Add a top-level menu under "Payments" (if it exists)
- add_menu_page(
- __('Transactions', 'textdomain'), // Page title
- __('Transactions', 'textdomain'), // Menu title
- 'manage_woocommerce', // Capability
- 'custom-transactions', // Menu slug
- 'custom_transactions_page_callback', // Function to display content
- 'dashicons-money-alt', // Icon
- 56 // Position
- );
- }
- add_action('admin_menu', 'custom_add_transactions_menu');
- // Callback function for Transactions Page content
- (SEE THE CALLBACK FUNCTION BELOW from Multisite)
- ========
- // IF Multisite
- /* == ADDED Transactions Admin Menu == */
- function custom_add_transactions_menu() {
- $allowed_site_id = 1; // Change this to the site ID where you want the menu to appear
- if (get_current_blog_id() == $allowed_site_id) {
- add_menu_page(
- __('Transactions', 'textdomain'), // Page title
- __('Transactions', 'textdomain'), // Menu title
- 'manage_woocommerce', // Required capability
- 'custom-transactions', // Menu slug
- 'custom_transactions_page_callback', // Function to display content
- 'dashicons-money-alt', // Icon
- 56 // Position
- );
- }
- }
- add_action('admin_menu', 'custom_add_transactions_menu');
- // CALLBACK function to display Transactions page content
- function custom_transactions_page_callback() {
- // Set the number of orders to show per page
- $per_page = 20; // Display 20 orders per page
- // Get current page number using the query var for 'paged'
- $current_page = isset($_GET['paged']) ? absint($_GET['paged']) : 1;
- // Calculate the offset based on current page
- $offset = ($current_page - 1) * $per_page;
- // Arguments for the query
- $args = array(
- 'post_type' => 'shop_order',
- 'post_status' => array('wc-processing', 'wc-completed'), // Only paid transactions
- 'posts_per_page' => $per_page, // Display 20 transactions per page
- 'offset' => $offset, // Offset for pagination
- 'orderby' => 'date', // Order by date
- 'order' => 'DESC' // Descending order
- );
- // Fetch orders
- $orders = get_posts($args);
- ?>
- <div class="wrap">
- <h1><?php _e('Transactions', 'textdomain'); ?></h1>
- <table class="wp-list-table widefat fixed striped">
- <thead>
- <tr>
- <th><?php _e('Date', 'textdomain'); ?></th> <!-- Date as the first column -->
- <th><?php _e('Order ID', 'textdomain'); ?></th>
- <th><?php _e('Customer', 'textdomain'); ?></th>
- <th><?php _e('Amount', 'textdomain'); ?></th>
- <th><?php _e('Payment Method', 'textdomain'); ?></th>
- <th><?php _e('Status', 'textdomain'); ?></th>
- </tr>
- </thead>
- <tbody>
- <?php
- if ($orders) {
- foreach ($orders as $order_post) {
- $order = wc_get_order($order_post->ID);
- if (!$order) {
- continue;
- }
- // Custom date format (change to the desired format)
- $order_date = $order->get_date_created()->date('F j, Y / g:i a'); // Example: February 23, 2025, 4:30 pm
- echo '<tr>';
- // Date column as first
- echo '<td>' . esc_html($order_date) . '</td>';
- echo '<td><a href="' . esc_url(admin_url('post.php?post=' . $order->get_id() . '&action=edit')) . '">' . $order->get_id() . '</a></td>';
- echo '<td>' . esc_html($order->get_billing_first_name() . ' ' . $order->get_billing_last_name()) . '</td>';
- echo '<td>' . esc_html($order->get_total()) . '</td>'; // Raw total amount (no HTML)
- echo '<td>' . esc_html($order->get_payment_method_title()) . '</td>';
- echo '<td>' . esc_html(wc_get_order_status_name($order->get_status())) . '</td>';
- echo '</tr>';
- }
- // Pagination
- $total_orders = wp_count_posts('shop_order')->publish; // Count all orders
- $total_pages = ceil($total_orders / $per_page); // Calculate total pages
- if ($total_pages > 1) { // Show pagination if there are more than one page
- echo '<tr><td colspan="6">';
- // Manually create pagination links
- $base = add_query_arg('paged', '%#%'); // Ensure the paged query argument is in the URL
- // Generate pagination links
- echo paginate_links(array(
- 'base' => $base,
- 'format' => '',
- 'current' => $current_page,
- 'total' => $total_pages,
- 'prev_text' => __('« Previous', 'textdomain'),
- 'next_text' => __('Next »', 'textdomain'),
- ));
- echo '</td></tr>';
- }
- } else {
- echo '<tr><td colspan="6">' . __('No transactions found.', 'textdomain') . '</td></tr>';
- }
- ?>
- </tbody>
- </table>
- </div>
- <?php
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement