Hello @zewasdigital
I believe you need to save the PDF url to the order meta to be able to do that.
Please add the following code snippet to your theme functions.php file:
add_action( 'wpo_wcpdf_before_document', function( $document_type, $order ) {
if( !empty($order) && $document_type == 'invoice' ) {
$debug_settings = get_option('wpo_wcpdf_settings_debug', array());
if( isset($debug_settings['guest_access']) ) {
$pdf_url = admin_url( 'admin-ajax.php?action=generate_wpo_wcpdf&template_type='.$document_type.'&order_ids=' . $order->get_id() . '&order_key=' . $order->get_order_key() );
update_post_meta( $order->get_id(), '_wcpdf_'.$document_type.'_link', esc_attr($pdf_url) );
}
}
}, 10, 2 );
You will need to enable “Guest Access” under WooCommerce > PDF Invoices > Status to be able to access the link without login.
If you never worked with actions/filters please read this documentation page: How to use filters
Thanks for your quick response @alexmigf and a big thank you!
I tried a different approach I, registered a new type invoiceURL” at the order.
But my mistakes were, at first i did not added the orderKey for security and secondly i didn’t enable the “Guest Access”, but with your help it worked out.
add_action('graphql_register_types', function () {
register_graphql_field('Order', 'invoiceUrl', [
'type' => 'String',
'description' => __('Invoice URL', 'wp-graphql'),
'resolve' => function ($order) {
$newOrder = new WC_Order($order);
if (!(get_current_user_id() == $newOrder->user_id)) {
throw new UserError(sprintf(__('Access restricted! invoice URL '. get_current_user_id() .' 1' .$newOrder->user_id.' 1', 'your-plugin')));
}
$pdf_url = wp_nonce_url(admin_url('admin-ajax.php?action=generate_wpo_wcpdf&document_type=invoice&order_ids='.$order -> get_id().'&order_key='.$order->get_order_key()), 'generate_wpo_wcpdf');
$newURL = str_replace("amp;", "", $pdf_url);
return $newURL;
}
]);
});