phpwoocommercehook-woocommerceorders

Change button position for invoices On WooCommerce My Account Orders


In WooCommerce, I have a plugin for invoices, and it has a function to show a button inside WooCommerce My Account Orders to download it / view it.

public function show_public_invoice( $order_id ) {
    $admin_settings = new Smartbill_Woocommerce_Admin_Settings_Fields();

    if ( $admin_settings->get_public_invoice() ) {
        $wtclass = get_post_meta( $order_id, 'smartbill_invoice_log' );

        if ( $wtclass && isset( $wtclass[0] ) && isset( $wtclass[0]['smartbill_view_document_url'] ) ) {
            echo '<section class="woocommerce-order-details"> <h2 class="woocommerce-column__title">';
            echo esc_attr( __( 'DOWNLOAD INVOICE', 'smartbill_woocommerce' ) ) . '</h2> <a target="_blank" href="' . esc_url( $wtclass[0]['smartbill_view_document_url'] ) . '" class="woocommerce-button button view">';
            echo esc_attr( $admin_settings->get_view_invoice_text() ) . '</a></section>';
        }
    }
}

But I can't change the position of the button, to put it on the My Account Orders page

I try to show it here, but nothing works:

orders- myaccount

I tried to change the but nothing happens, it does not change the position

<section class="woocommerce-order-details">

Solution

  • I've refined the solution to add an invoice button to the WooCommerce My Account Orders page. Below is the improved code to include in your theme's functions.php file, along with troubleshooting steps to ensure it works correctly.

    <?php
    
    class WooCommerce_Invoice_Button_Manager {
        
        public function __construct() {
            // Hook into WooCommerce actions
            add_filter('woocommerce_my_account_my_orders_actions', array($this, 'add_invoice_button_to_orders_actions'), 10, 2);
            
            // Alternative hook if you want to add it in a different position
            // add_action('woocommerce_my_account_my_orders_column_order-actions', array($this, 'add_custom_invoice_button'), 10, 1);
        }
        
        /**
         * Method 1: Add invoice button to the actions column (recommended)
         * This adds the button alongside existing action buttons like "View", "Cancel", etc.
         */
        public function add_invoice_button_to_orders_actions($actions, $order) {
            $admin_settings = new Smartbill_Woocommerce_Admin_Settings_Fields();
            
            if ($admin_settings->get_public_invoice()) {
                $wtclass = get_post_meta($order->get_id(), 'smartbill_invoice_log');
                
                if ($wtclass && isset($wtclass[0]) && isset($wtclass[0]['smartbill_view_document_url'])) {
                    
                    // Method 1A: Simple addition to the end
                    $actions['download_invoice'] = array(
                        'url'  => $wtclass[0]['smartbill_view_document_url'],
                        'name' => __('INVOICE', 'smartbill_woocommerce'),
                        'class' => 'button invoice-btn'
                    );
                    
                    /* 
                    // Method 1B: Insert at specific position
                    $new_actions = array();
                    $inserted = false;
                    
                    foreach($actions as $key => $action) {
                        // Insert before 'view' button
                        if($key === 'view' && !$inserted) {
                            $new_actions['download_invoice'] = array(
                                'url'  => $wtclass[0]['smartbill_view_document_url'],
                                'name' => __('INVOICE', 'smartbill_woocommerce'),
                                'class' => 'button invoice-btn'
                            );
                            $inserted = true;
                        }
                        $new_actions[$key] = $action;
                    }
                    
                    // If 'view' button doesn't exist, add at the end
                    if (!$inserted) {
                        $new_actions['download_invoice'] = array(
                            'url'  => $wtclass[0]['smartbill_view_document_url'],
                            'name' => __('INVOICE', 'smartbill_woocommerce'),
                            'class' => 'button invoice-btn'
                        );
                    }
                    
                    return $new_actions;
                    */
                }
            }
            
            return $actions;
        }
        
        /**
         * Method 2: Alternative
         * Use this if Method 1 doesn't work or you want more control
         */
        public function add_custom_invoice_button($order) {
            $admin_settings = new Smartbill_Woocommerce_Admin_Settings_Fields();
            
            if ($admin_settings->get_public_invoice()) {
                $wtclass = get_post_meta($order->get_id(), 'smartbill_invoice_log');
                
                if ($wtclass && isset($wtclass[0]) && isset($wtclass[0]['smartbill_view_document_url'])) {
                    echo '<a href="' . esc_url($wtclass[0]['smartbill_view_document_url']) . '" 
                             class="woocommerce-button button invoice-btn" 
                             target="_blank">' . 
                             esc_html__('INVOICE', 'smartbill_woocommerce') . 
                          '</a>';
                }
            }
        }
    }
    
    // Initialize the class
    new WooCommerce_Invoice_Button_Manager();
    
    /**
     * Method 3: Using different hooks for different positions
     * Uncomment the hook you want to use:
     */
    
    // Add invoice button after order actions
    // add_action('woocommerce_my_account_my_orders_column_order-actions', 'add_invoice_after_actions', 20);
    
    // Add invoice button in order number column
    // add_action('woocommerce_my_account_my_orders_column_order-number', 'add_invoice_in_order_number', 20);
    
    // Add invoice button in order status column  
    // add_action('woocommerce_my_account_my_orders_column_order-status', 'add_invoice_in_status', 20);
    
    function add_invoice_after_actions($order) {
        $admin_settings = new Smartbill_Woocommerce_Admin_Settings_Fields();
        
        if ($admin_settings->get_public_invoice()) {
            $wtclass = get_post_meta($order->get_id(), 'smartbill_invoice_log');
            
            if ($wtclass && isset($wtclass[0]) && isset($wtclass[0]['smartbill_view_document_url'])) {
                echo '<br><a href="' . esc_url($wtclass[0]['smartbill_view_document_url']) . '" 
                         class="woocommerce-button button invoice-btn" 
                         target="_blank">' . 
                         esc_html__('DOWNLOAD INVOICE', 'smartbill_woocommerce') . 
                      '</a>';
            }
        }
    }
    
    /**
     * CSS to style the invoice button (add to your theme's CSS)
     */
    /*
    .woocommerce-MyAccount-orders .invoice-btn {
        background-color: #0073aa;
        color: white !important;
        margin-left: 5px;
        text-decoration: none;
        padding: 8px 12px;
        border-radius: 3px;
        display: inline-block;
        font-size: 12px;
    }
    
    .woocommerce-MyAccount-orders .invoice-btn:hover {
        background-color: #005177;
    }
    */
    
    /**
     * DEBUGGING FUNCTION
     * Add this temporarily to see what hooks are available
     */
    /*
    function debug_woocommerce_my_account_hooks() {
        if (is_account_page()) {
            global $wp_filter;
            
            echo '<pre>';
            echo "Available WooCommerce My Account hooks:\n";
            
            foreach ($wp_filter as $hook_name => $hook_obj) {
                if (strpos($hook_name, 'woocommerce_my_account') !== false) {
                    echo $hook_name . "\n";
                }
            }
            echo '</pre>';
        }
    }
    add_action('wp_footer', 'debug_woocommerce_my_account_hooks');
    */
    ?>