• Resolved R.B.

    (@rb-1)


    I add this plugin

    //Languages
    	add_action('plugins_loaded', 'woocommerce_nif_init');
    	function woocommerce_nif_init() {
    		load_plugin_textdomain('woocommerce_nif', false, dirname(plugin_basename(__FILE__)) . '/lang/');
    	}
    
    	//Add field to checkout
    	add_filter('woocommerce_checkout_fields' , 'woocommerce_nif_checkout');
    	function woocommerce_nif_checkout( $fields ) {
    		global $woocommerce;
    		if(trim($woocommerce->customer->get_country())=='PT') {
    			$current_user=wp_get_current_user();
    			$fields['billing']['billing_nif'] = array(
    				'type'			=>	'text',
    				'label'			=> __('NIF / NIPC', 'woocommerce_nif'),
    				'placeholder'	=> _x('Portuguese VAT identification number', 'placeholder', 'woocommerce_nif'),
    				'class'			=> array('form-row-first'),
    				'required'		=> false,
    				'clear'			=> true,
    				'default'		=> ($current_user->billing_nif ? trim($current_user->billing_nif) : ''),
    			);
    		}
    		return $fields;
    	}
    
    	//Add NIF to My Account / Billing Address form
    	add_filter('woocommerce_address_to_edit', 'woocommerce_nif_my_account');
    	function woocommerce_nif_my_account($fields) {
    		global $wp_query;
    		if (isset($wp_query->query_vars['edit-address']) && $wp_query->query_vars['edit-address']!='billing') {
    			return $fields;
    		} else {
    			$current_user=wp_get_current_user();
    			if ($current_user->billing_country=='PT') {
    				$fields['billing_nif']=array(
    					'type'			=>	'text',
    					'label'			=> __('NIF / NIPC', 'woocommerce_nif'),
    					'placeholder'	=> _x('Portuguese VAT identification number', 'placeholder', 'woocommerce_nif'),
    					'class'			=> array('form-row-first'),
    					'required'		=> false,
    					'clear'			=> true,
    					'default'		=> ($current_user->billing_nif ? trim($current_user->billing_nif) : ''),
    				);
    			}
    			return $fields;
    		}
    	}
    	//Save NIF to customer Billing Address
    	add_action('woocommerce_customer_save_address', 'woocommerce_nif_my_account_save', 10, 2);
    	function woocommerce_nif_my_account_save($user_id, $load_address) {
    		if ($load_address=='billing') {
    			if (isset($_POST['billing_nif'])) {
    				update_user_meta( $user_id, 'billing_nif', trim($_POST['billing_nif']) );
    			}
    		}
    	}
    
    	//Add field to order admin panel
    	add_action( 'woocommerce_admin_order_data_after_billing_address', 'woocommerce_nif_admin', 10, 1);
    	function woocommerce_nif_admin($order){
    		if (@is_array($order->order_custom_fields['_billing_country'])) {
    			//Old WooCommerce versions
    			if(@in_array('PT', $order->order_custom_fields['_billing_country']) ) {
    				echo "<p><strong>".__('NIF / NIPC', 'woocommerce_nif').":</strong> " . $order->order_custom_fields['_billing_nif'][0] . "</p>";
    	  		}
    		} else {
    			//New WooCommerce versions
    			if ($order->billing_country=='PT') {
    				$order_custom_fields=get_post_custom($order->ID);
    				echo "<p><strong>".__('NIF / NIPC', 'woocommerce_nif').":</strong> " . $order_custom_fields['_billing_nif'][0] . "</p>";
    			}
    		}
    	}
    
    	/* If you're reading this you must know what you're doing ;-) Greetings from sunny Portugal! */
    
    }

    So I add this code:

    <?php
    $woocommerce_nif = get_post_meta($wpo_wcpdf->export->order->id,'NIF',true);
    if (isset($woocommerce_nif)) {
    	echo $woocommerce_nif;
    }
    ?>

    Its not working.

    https://www.ads-software.com/plugins/woocommerce-pdf-invoices-packing-slips/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    Hi! From your first code, you’re saving the nif in ‘billing_nif’ rather than ‘NIF’.
    This should work:

    <?php $wpo_wcpdf->custom_field('billing_nif'); ?>

    Or even better, without touching the template (makes it easier to stay up to date), add this to your theme functions:

    add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_billing_nif', 10, 2 );
    function wpo_wcpdf_billing_nif ($template_type, $order) {
        global $wpo_wcpdf;
        ?>
        <tr class="billing-nif">
            <th>NIF:</th>
            <td><?php $wpo_wcpdf->custom_field('billing_nif'); ?></td>
        </tr>
        <?php
    }

    Check out the docs for more information:
    Displaying a custom field

    Let me know if you have any other questions!
    Ewout

    Thread Starter R.B.

    (@rb-1)

    Parse error: syntax error, unexpected ‘}’ in /…. /wp-content/themes/mts_ecommerce/functions.php on line 5485

    Plugin Contributor Ewout

    (@pomegranate)

    Hi! It’s probably a copy-paste error, I tested the code on my own site and it works for me. For reference, check our guide on how to use filters.

    Let me know if you have any other questions!

    Ewout

    Hello!

    I have the same problem!
    I tried the code! Not getting any errors – deleted the last } from R.B. code – but I not getting any output.
    In a view-order page I can get the data just calling $order->billing_nif;. I can see the data in the admin area…

    but if I use $wpo_wcpdf->custom_field(‘billing_nif’); or the action (functions.php) I just get a empty field in the invoice – all the rest is fine!

    Thanks!

    Update!

    Solved it!

    <?php
    echo get_user_meta(get_post_meta( $wpo_wcpdf->export->order->id,'_customer_user',true) , 'billing_nif', true );
    ?>
    Plugin Contributor Ewout

    (@pomegranate)

    Hi! That’s another method, you’re then getting the data from the user, not the order though. The error in your initial code was the missing leading underscore. $order->billing_nif calls the magic __get() method, which automatically appends the leading underscore.

    Two other options:

    • The order is accessible via $this->order in the invoice. So you can do $this->order->billing_nif and accomplishing the same as $order->billing_nif elsewhere.
    • If you want to use the custom field function, you only need to add the underscore: $wpo_wcpdf->custom_field('_billing_nif');

    Have a fantastic weekend!
    Ewout

    Thanks!

    I’ll try and give you a feedback!

    Hope you have a nice weekend too!

    Hello!
    All the options

    echo $this->order->billing_nif;
    $wpo_wcpdf->custom_field('_billing_nif');

    are working fine!

    I think you an close this support ticket!

    Thanks again!

    Plugin Contributor Ewout

    (@pomegranate)

    Great, thanks for the feedback!

    If you can spare a minute, I’d really appreciate a plugin review here on www.ads-software.com.

    Thanks in advance!

    Ewout

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Do not show field in Invoice’ is closed to new replies.