Advertisement
sierre

Adding Transactions admin menu - WooCommerce

Feb 22nd, 2025
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.74 KB | None | 0 0
  1. <?php
  2. // Added by WooPayments plugin before
  3. // If NOT Multisite
  4. function custom_add_transactions_menu() {
  5.     // Add a top-level menu under "Payments" (if it exists)
  6.     add_menu_page(
  7.         __('Transactions', 'textdomain'), // Page title
  8.         __('Transactions', 'textdomain'), // Menu title
  9.         'manage_woocommerce', // Capability
  10.         'custom-transactions', // Menu slug
  11.         'custom_transactions_page_callback', // Function to display content
  12.         'dashicons-money-alt', // Icon
  13.         56 // Position
  14.     );
  15. }
  16.  
  17. add_action('admin_menu', 'custom_add_transactions_menu');
  18.  
  19. // Callback function for Transactions Page content
  20. (SEE THE CALLBACK FUNCTION BELOW from Multisite)
  21.  
  22. ========
  23. // IF Multisite
  24. /* == ADDED Transactions Admin Menu == */
  25. function custom_add_transactions_menu() {
  26.     $allowed_site_id = 1; // Change this to the site ID where you want the menu to appear
  27.  
  28.     if (get_current_blog_id() == $allowed_site_id) {
  29.         add_menu_page(
  30.             __('Transactions', 'textdomain'), // Page title
  31.             __('Transactions', 'textdomain'), // Menu title
  32.             'manage_woocommerce', // Required capability
  33.             'custom-transactions', // Menu slug
  34.             'custom_transactions_page_callback', // Function to display content
  35.             'dashicons-money-alt', // Icon
  36.             56 // Position
  37.         );
  38.     }
  39. }
  40.  
  41. add_action('admin_menu', 'custom_add_transactions_menu');
  42.  
  43. // CALLBACK function to display Transactions page content
  44. function custom_transactions_page_callback() {
  45.     // Set the number of orders to show per page
  46.     $per_page = 20;  // Display 20 orders per page
  47.  
  48.     // Get current page number using the query var for 'paged'
  49.     $current_page = isset($_GET['paged']) ? absint($_GET['paged']) : 1;
  50.  
  51.     // Calculate the offset based on current page
  52.     $offset = ($current_page - 1) * $per_page;
  53.  
  54.     // Arguments for the query
  55.     $args = array(
  56.         'post_type'      => 'shop_order',
  57.         'post_status'    => array('wc-processing', 'wc-completed'), // Only paid transactions
  58.         'posts_per_page' => $per_page, // Display 20 transactions per page
  59.         'offset'         => $offset,   // Offset for pagination
  60.         'orderby'        => 'date',    // Order by date
  61.         'order'          => 'DESC'     // Descending order
  62.     );
  63.  
  64.     // Fetch orders
  65.     $orders = get_posts($args);
  66.  
  67.     ?>
  68.     <div class="wrap">
  69.         <h1><?php _e('Transactions', 'textdomain'); ?></h1>
  70.         <table class="wp-list-table widefat fixed striped">
  71.             <thead>
  72.                 <tr>
  73.                     <th><?php _e('Date', 'textdomain'); ?></th> <!-- Date as the first column -->
  74.                     <th><?php _e('Order ID', 'textdomain'); ?></th>
  75.                     <th><?php _e('Customer', 'textdomain'); ?></th>
  76.                     <th><?php _e('Amount', 'textdomain'); ?></th>
  77.                     <th><?php _e('Payment Method', 'textdomain'); ?></th>
  78.                     <th><?php _e('Status', 'textdomain'); ?></th>
  79.                 </tr>
  80.             </thead>
  81.             <tbody>
  82.                 <?php
  83.                 if ($orders) {
  84.                     foreach ($orders as $order_post) {
  85.                         $order = wc_get_order($order_post->ID);
  86.  
  87.                         if (!$order) {
  88.                             continue;
  89.                         }
  90.  
  91.                         // Custom date format (change to the desired format)
  92.                         $order_date = $order->get_date_created()->date('F j, Y / g:i a'); // Example: February 23, 2025, 4:30 pm
  93.  
  94.                         echo '<tr>';
  95.                         // Date column as first
  96.                         echo '<td>' . esc_html($order_date) . '</td>';
  97.                         echo '<td><a href="' . esc_url(admin_url('post.php?post=' . $order->get_id() . '&action=edit')) . '">' . $order->get_id() . '</a></td>';
  98.                         echo '<td>' . esc_html($order->get_billing_first_name() . ' ' . $order->get_billing_last_name()) . '</td>';
  99.                         echo '<td>' . esc_html($order->get_total()) . '</td>'; // Raw total amount (no HTML)
  100.                         echo '<td>' . esc_html($order->get_payment_method_title()) . '</td>';
  101.                         echo '<td>' . esc_html(wc_get_order_status_name($order->get_status())) . '</td>';
  102.                         echo '</tr>';
  103.                     }
  104.  
  105.                     // Pagination
  106.                     $total_orders = wp_count_posts('shop_order')->publish;  // Count all orders
  107.                     $total_pages = ceil($total_orders / $per_page);         // Calculate total pages
  108.  
  109.                     if ($total_pages > 1) { // Show pagination if there are more than one page
  110.                         echo '<tr><td colspan="6">';
  111.                        
  112.                         // Manually create pagination links
  113.                         $base = add_query_arg('paged', '%#%');  // Ensure the paged query argument is in the URL
  114.                        
  115.                         // Generate pagination links
  116.                         echo paginate_links(array(
  117.                             'base' => $base,
  118.                             'format' => '',
  119.                             'current' => $current_page,
  120.                             'total' => $total_pages,
  121.                             'prev_text' => __('« Previous', 'textdomain'),
  122.                             'next_text' => __('Next »', 'textdomain'),
  123.                         ));
  124.  
  125.                         echo '</td></tr>';
  126.                     }
  127.                 } else {
  128.                     echo '<tr><td colspan="6">' . __('No transactions found.', 'textdomain') . '</td></tr>';
  129.                 }
  130.                 ?>
  131.             </tbody>
  132.         </table>
  133.     </div>
  134.     <?php
  135. }
  136.  
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement