• Resolved JJ

    (@jjtristargroup)


    Hi,

    First off, love the plug-in! Works fantastic and the customizability is great.

    I’d like to request a feature, is it possible to add a link to the PDF of the order in the WooCommerce Orders Rest API? (example.com/wp-json/wc/v2/orders)

    We export all our orders trough this API to our own ERP system but now we need to manually go to the order in WooCommerce to grab the PDF for further handling of the order. If the link to the PDF could be added to this output we can skip this step.

    Kind regards,
    Jan

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor alexmigf

    (@alexmigf)

    Hello @jjtristargroup

    Currently is not possible, but you can do this other way like saving the PDF url to the order meta, and export that meta when you export the orders.

    First you need to enable “Guest access” under WooCommerce > PDF Invoices > Status.

    The 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_link', esc_attr($pdf_url) );
    		}
    	}
    }, 10, 2 );

    If you never worked with filters/actions please read this documentation page: How to use filters

    Finally export with the orders the meta key: _wcpdf_document_link

    This will only work for future Invoices generated after you added the snippet.

    Hope that helps!

    Thread Starter JJ

    (@jjtristargroup)

    Hi @alexmigf ,

    Thanks for the quick reply!
    I looked into using custom filters/actions for this functionality but I thought I’d ask here first just be sure. And it looks like it paid off, this is more than I could ask for!

    Thank you very much for your help and taking the time to write that snippet. I really appreciate it. I’m going to try and implement it straight away.

    Kind regards,
    Jan

    Plugin Contributor alexmigf

    (@alexmigf)

    You’re welcome ??

    Have a nice day!

    Plugin Contributor alexmigf

    (@alexmigf)

    @jjtristargroup

    I believe you can access that meta from the REST API too.

    Thread Starter JJ

    (@jjtristargroup)

    Hi again @alexmigf ,

    Just wanted to update you and help others that may find this thread in the future, I’ve just implemented your snippet and added the meta field to the output successfully!

    For people who find this in the future, after adding alexmigf’s snippet above you can add this code as a plug-in (or snippet) to add it to the Orders Rest API:

    <?php
    /*
    Plugin Name: WooCommerce Invoices REST API
    Plugin URI: https://janjacobs.nl/
    Description: Adds invoices to the WooCommerce Orders REST API
    Version: 1.0.0
    Author: alexmigf + Jan Jacobs
    Author URI: https://janjacobs.nl/
    License: GPL2
    Text Domain: woocommerce-invoices-rest-api
    WC requires at least: 4.0.0
    WC tested up to: 4.6.1
    
    */
    
    //  Add PDFs to order output
    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_link', esc_attr($pdf_url) );
    		}
    	}
    }, 10, 2 );
    
    // Add PDFs URL to order output to meta_data
    function add_invoices_to_rest_api( $response, $object, $request ) {
    
        // Check if response has data
        if(empty($response->data)) {
            return $response;
        }
    
        // Get the Order ID
        $order_id = $response->data['id'];
    
        // Get _wcpdf_document_link value
        $product_meta__wcpdf_document_link = get_post_meta($order_id, '_wcpdf_document_link', true);
    
        // Catch empty fields
        if($product_meta__wcpdf_document_link == "" || $product_meta__wcpdf_document_link == false  || $product_meta__wcpdf_document_link == NULL) {
            $product_meta__wcpdf_document_link = "";
        }
    
        // Add _wcpdf_document_link value to response
        $response->data['_wcpdf_document_link'] = $product_meta__wcpdf_document_link;
     
        return $response;
    } 
    add_filter( 'woocommerce_rest_prepare_shop_order_object', 'add_invoices_to_rest_api', 10, 3 );
    
    

    *Edit: This code was originally intended for product meta, I’ll optimize it soon.
    *Edit 2: I wrapped alexmigf’s and my code in 1 big plug-in snippet as seen above.

    And again, thanks @alexmigf !

    Kind regards,
    Jan

    • This reply was modified 4 years, 4 months ago by JJ.
    • This reply was modified 4 years, 4 months ago by JJ.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘ORDERS REST API PDF Link’ is closed to new replies.