WordPress WooCommerce - Print Invoice for Customers - Access denied

6.7k views Asked by At

I'm completely new to WordPress and I am trying to set up a simple store. It works fine, but I want my customers to be able to view the invoice right away from their orders. I downloaded the WooCommerce Print Invoices & Delivery Notes plugin, but it only allows me to print the invoice, not the customer. Therefore, I modified the WooCommerce /templates/myaccount/my-orders.php file to include a button that generates the invoice:

/* USERCONTENT */
if ( $order->status == 'completed' ) {
    $actions['invoice'] = array(
        'url' => wp_nonce_url( '/wp-admin/admin-ajax.php?action=generate_print_content&template_type=invoice&order_id=' . $order->id , 'generate_print_content' ),
        'name' => __( 'Invoice', 'woocommerce' )
    );
}
/* ENDUSERCONTENT */

This works when I view my orders. I assigned a test order to myself:

enter image description here

Clicking the Invoice button opens the invoice generated by the WooCommerce Print Invoices & Delivery Notes Plugin, which is exactly what I want. But it does not work for my customers. They see a webpage with the message "You do not have sufficient permissions to access this page." instead.

My assumption is that it has to do with the fact that it is accessing /wp-admin/admin-ajax.php. Is there another ~-ajax.php that I can invoke the GET on and users will have access to?

1

There are 1 answers

0
sPaul On

This call is tied to "wp_ajax_" type of action which, in general, allows all logged in users to access it, so that is fine. You can read more about Ajax in WordPress plugins here.

You may wish to open /classes/class-wcdn-print.php and comment out these two blocks:

            if( !is_admin() ) {
                wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
            }

            if( !current_user_can( 'manage_woocommerce_orders' ) && !current_user_can( 'edit_shop_orders' ) ) {
                wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
            }

Or you can give WooCommerce PDF Invoice plugin a try - invoices will be available for both administrators and customers plus it attaches invoices to emails (disclosure: I'm the author).