1
votes

I am using WooCommerce Print Invoices & Packing Lists Plugin. I want to add some custom field values in the order invoice. But the fields are not loading properly.

add_action('wc_pip_after_body', 'dvsi_invoice_footer');
function dvsi_invoice_footer ($order_id) {

    $order = new WC_Order( $order_id );

    echo '<h4>'.__('Name: ').'</strong> ' . $order->billing_first_name . '</h4>';
    echo '<h4>'.__('Note: ').'</strong> ' . get_post_meta( $order->id, 'public_details_new', true ) . '</h4>';

    ?>
    <h4 style="margin-bottom:0px;">Important:</h4>
    <p>If you have purchased Domain, Hosting, SSL certificate or any other service that has an Expiry date then please contact us 10 days before prior to the Expiry date so we can renew or repurchase it on time.<p>                      
    <h4 style="margin-bottom:0px;">Contact Details</h4>
    <p><strong>Office: </strong>AL-Hafeez Heights Gulberg III, Lahore<p>
    <p><strong>Mobile: </strong>0323-4152099<p>
    <p><strong>Email: </strong>[email protected]<p>
    <?php   
}

The output didnot load my custom field values and display blank. It only load html values.

Here is the link of the available plugin hooks: https://docs.woocommerce.com/document/woocommerce-print-invoices-packing-lists-developer-reference/#section-1

Any help please?

2

2 Answers

1
votes

Not sure if this solves the issue, but need to use the WooCommerce CRUD getters.

$order = new WC_Order( $order_id );
echo '<h4>'.__('Name: ').'</strong> ' . $order->get_billing_first_name() . '</h4>';
echo '<h4>'.__('Note: ').'</strong> ' . $order->get_meta( 'public_details_new', true ) . '</h4>';

Direct access of object properties has been deprecated for a few years now and at least is throwing lots of warnings.

You may also need to verify that your custom meta is being saved correctly.

1
votes

There is no $order_id argument for wc_pip_after_body hook, but the following arguments are available: $type, $action, $document and $order.

So you can use it this way in your code (assuming that public_details_new custom field exits):

add_action('wc_pip_after_body', 'dvsi_invoice_footer', 90, 4 );
function dvsi_invoice_footer ( $type, $action, $document, $order ) {

    echo '<h4>'.__('Name: ').'</strong> ' . $order->get_billing_first_name() . '</h4>';
    echo '<h4>'.__('Note: ').'</strong> ' . $order->get_meta( 'public_details_new' ) . '</h4>';

    echo '<h4 style="margin-bottom:0px;">'.__('Important: ').'</h4>
    <p>'.__('If you have purchased Domain, Hosting, SSL certificate or any other service that has an Expiry date then please contact us 10 days before prior to the Expiry date so we can renew or repurchase it on time.').'<p>'; 

    echo '<h4 style="margin-bottom:0px;">'.__('Contact Details: ').'</h4>
    <p><strong>Office: </strong>'.__('AL-Hafeez Heights Gulberg III, Lahore').'<p>
    <p><strong>Mobile: </strong>'.__('0323-4152099').'<p>
    <p><strong>Email: </strong>'.__('[email protected]').'<p>'; 
}

It should better work.

Note: As @helgatheviking said WC_Order Object properties are not anymore accessible since Woocommerce 3, and you need to use the corresponding available methods instead.