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.