Hi Ewout, yes you are right. I have actually figured out what to do and this snippet worked in my case.
// Display the custom field inside the PDF Invoice and Packing slips
add_filter( 'wpo_wcpdf_after_order_data', 'rdm_po_number', 10, 2 );
function rdm_po_number( $template_type, $order ) {
if ( $template_type == 'invoice' || $template_type == 'packing-slip' ) {
$rdm_po_number= $order->get_meta('Purchase Order Number');
echo "<tr style='padding:0.5rem; background: #ededed;'><td>Your PO no.:</td><td>{$rdm_po_number}</td></tr>";
}
}
That was the final part of the snippet, the full one is as follow and works fine for me:
/ 1 Let’s add a new field to checkout, after the order notes, by hooking into the following:
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'rdm_custom_checkout_field' );
function rdm_custom_checkout_field( $checkout ) {
echo '<div id="rdm_custom_checkout_field"><h2>' . __('Your Purchase Order no.') . '</h2>';
woocommerce_form_field( 'rdm_po_number_field', array(
'type' => 'text',
'class' => array('rdm-field-class form-row-wide'),
'label' => __('Add your internal purchase order number'),
'placeholder' => __('XX-123354'),
), $checkout->get_value( 'rdm_po_number_field' ));
echo '</div>';
}
// 2 Next we need to validate the field when the checkout form is posted. For this example the field is required and not optional:
/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'rdm_custom_checkout_field_process');
function rdm_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['rdm_po_number_field'] )
wc_add_notice( __( 'Please add your internal purchase order number.' ), 'error' );
}
//3 Finally, let’s save the new field to order custom fields using the following code:
/**
* Update the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta', 'rdm_custom_checkout_field_update_order_meta' );
function rdm_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['rdm_po_number_field'] ) ) {
update_post_meta( $order_id, 'Purchase Order Number', sanitize_text_field( $_POST['rdm_po_number_field'] ) );
}
}
// 4 If you wish to display the custom field value on the admin order edition page, you can add this code:
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'rdm_custom_checkout_field_display_admin_order_meta', 10, 1 );
function rdm_custom_checkout_field_display_admin_order_meta($order){
echo '<div style="padding:0.5rem; background: #D46A6A; border: 1px solid #670d0d " role="alert"><strong>'.__('Purchase Order Number').':</strong> ' . get_post_meta( $order->get_id(), 'Purchase Order Number', true ) . '</div>';
}
// Addd custom fields to the email
/* To use:
1. Add this snippet to your theme's functions.php file
2. Change the meta key names in the snippet
3. Create a custom field in the order post - e.g. key = "Tracking Code" value = abcdefg
4. When next updating the status, or during any other event which emails the user, they will see this field in their email
*/
add_filter('woocommerce_email_order_meta_keys', 'rdm_custom_order_meta_keys');
function rdm_custom_order_meta_keys( $keys ) {
$keys[] = 'Purchase Order Number'; // This will look for a custom field called 'Tracking Code' and add it to emails
return $keys;
}
// Display the custom field inside the PDF Invoice and Packing slips
add_filter( 'wpo_wcpdf_after_order_data', 'rdm_po_number', 10, 2 );
function rdm_po_number( $template_type, $order ) {
if ( $template_type == 'invoice' || $template_type == 'packing-slip' ) {
$rdm_po_number= $order->get_meta('Purchase Order Number');
echo "<tr style='padding:0.5rem; background: #ededed;'><td>Your PO no.:</td><td>{$rdm_po_number}</td></tr>";
}
}