• Resolved teleanu

    (@teleanu)


    Hi.

    I was woundering how I can add a custom field that i’v created for the checkout page on the invoice and maybe on the packing slip

    the custom field meta is: _billing_cnp
    That custom field is just plain text.

    My custom field is added on the checkout page exactly after the company field.
    So… would be nice If I can get something like.
    ________
    Name
    Company
    My Custom Field
    Billing Address
    ________
    I’v tried this, but doesn’t work:

    add_action( 'wpo_wcpdf_before_billing_address', 'wpo_wcpdf_billing_cnp', 10, 2 );
    function wpo_wcpdf_billing_cnp ($document_type, $order) {
        if ($document_type == 'invoice') {
            // get the delivery date from the order
            $billing_cnp = $order->get_meta('billing_cnp');
            ?>
            <tr class="billing-cnp">
                <th>CNP:</th>
                <td><?php echo $billing_cnp; ?></td>
            </tr>
            <?php
        }
    }

    I’v read from the documentation, but I can’t find something to help me achive what I want.

    Thank you!

    • This topic was modified 3 years, 1 month ago by teleanu.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter teleanu

    (@teleanu)

    I’v added it after the order data

    I tried adding it after billing address, but for some reason it doesn’t work.
    If you guys have a solution for that would be awesome… If not, I’m ok with it after the order date too.

    Code used:

    add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_billing_cnp', 10, 2 );
    function wpo_wcpdf_billing_cnp( $template_type, $order ) {
    	// if order is empty, bail
    	if ( empty($order) ) { return; }	
    
    	?>      
    	<tr class="billing-cnp">
    		<th>CNP: </th>
    		<td><?php echo $order->get_meta('_billing_cnp'); ?></td>
    	</tr>
    	<?php
    }
    Plugin Contributor Yordan Soares

    (@yordansoares)

    Hi @teleanu,

    I see two issues in your first code snippet: 1. You’re using an HTML table structure, but this is only needed in the order data section. 2. You forgot adding the underscore to your custom field meta key (_billing_cnp).

    Please try using this code snippet instead:

    /**
     * Show CNP number on the invoice
     */
    add_action( 'wpo_wcpdf_after_billing_address', function($template_type, $order){
    	if( $template_type == 'invoice') {
    		if ( $billing_cnp  = $order->get_meta( '_billing_cnp' ) ){
    			echo '<div>CNP: ' . $billing_cnp  . '</div>';
    		}
    	}
    }, 10, 2 );

    Let me know if worked!

    Thread Starter teleanu

    (@teleanu)

    Thank you… it worked.

    But I change it a little bit by removing

    if( $template_type == 'invoice')

    Now it looks like this because I wanted it on the slip too:

    /**
     * Show CNP number on the invoice
     */
    add_action( 'wpo_wcpdf_after_billing_address', function($template_type, $order){
    	{
    		if ( $billing_cnp  = $order->get_meta( '_billing_cnp' ) ){
    			echo '<div>CNP: ' . $billing_cnp  . '</div>';
    		}
    	}
    }, 10, 2 );

    I checked and haven’t saw any error after that modification. The CNP is added under the billing address as wanted, both on invoice & packing slip. Don’t know if that is the way to do it, but worked. If u have any idea of adding on both, please share. thank you for helping me with the initial code.

    Plugin Contributor Yordan Soares

    (@yordansoares)

    Oh, sorry, I limited this to invoices, but I just realized that you wanted it in packing slip too!

    The way you did it is right: Well done! I’m glad to know you managed to make it work as you wanted ??

    If you don’t mind and have the time, do you think you could leave us a review?

    Thanks in advance and all the best with your store!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘add custom field on invoice and maybe on packing slip’ is closed to new replies.