skip to Main Content

I am looking to add custom meta after the order data in WooCommerce PDF Invoices & Packing Slips. If a Custom Field is present on a WooCommerce order,

In my code my custom field name is wholesale_order.

For this I make use of:

add_action( 'wpo_wcpdf_after_order_data', 'add_due_date', 10, 2 );
function add_due_date() {

 $order_data = get_post_meta( $post->ID, 'wholesale_order', true );
  if( $order_data ) {
      // Do stuff
     }
}

Unfortunately without the desired result, I think the $post->ID is incorrect and probably the get_post_meta.

What am I missing?

2

Answers


  1. Declare global $post before $order_data variable.

    Login or Signup to reply.
  2. First try:

    function action_wpo_wcpdf_after_order_data( $template_type, $order ) {
        // Get meta
        $wholesale_order = $order->get_meta( 'wholesale_order' );
    
        echo $wholesale_order;
    }
    add_action( 'wpo_wcpdf_after_order_data', 'action_wpo_wcpdf_after_order_data', 10, 2 );
    

    If that works you can expand your code to something like:

    function action_wpo_wcpdf_after_order_data( $template_type, $order ) {
        // Get meta
        $wholesale_order = $order->get_meta( 'wholesale_order' );
    
        // NOT empty
        if ( ! empty ( $wholesale_order ) ) {
            ?>
            <tr class="my-class>
                <th><?php __( 'My title', 'woocommerce' ); ?></th>
                <td>
                    <?php echo $wholesale_order; ?>
                </td>
            </tr>
            <?php
        }
    }
    add_action( 'wpo_wcpdf_after_order_data', 'action_wpo_wcpdf_after_order_data', 10, 2 );
    

    Action hook:

    • wpo_wcpdf_after_order_data has 2 arguments $template_type & $order.
    • After the order data – note that this is inside a table, and you should output the data as an html table row/cells.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search