Sending invoice only for some customers
-
Hello guys.
Nice plugin, congratulations.
I have a question. In the checkout form I added a new field called VAT number, my question is: ?Is possible to send the invoice only for customers that fill the VAT number field? For the rest of customers that do not have a VAT number is not necessary an invoice.
Thank you for your attention and time.
Regards.
-
Hello @edosantillana,
Add this code snippet to your site to achieve it:
/** * Enable invoice only if the customer fill the VAT number */ add_filter( 'wpo_wcpdf_document_is_allowed', 'wpo_wcpdf_allow_invoice_if_customer_has_vat_number', 10, 2 ); function wpo_wcpdf_allow_invoice_if_customer_has_vat_number( $allowed, $document ) { if ( $document->type == 'invoice' ) { if ( $order = $document->order ) { $allowed = ( ! empty( $order->get_meta( 'vat_number' ) ) ) ? true : false; } } return $allowed; }
If you haven’t worked with code snippets (actions/filters) or functions.php before, read this guide: How to use filters
Please note that I used
vat_number
as an example of meta key and, although in fact this is the most common meta key the VAT plugin uses, you need to figure out the actual meta key for your meta key in order this code snippet work (see Finding WooCommerce custom fields).Let me know if it does ??
Hello Yordan.
Very sorry for answering so late.
I’m not sure if it is working, this is my code:/**
* Enable invoice only if the customer fill the NIF/CIF field
*/
add_filter( ‘wpo_wcpdf_document_is_allowed’, ‘wpo_wcpdf_allow_invoice_if_customer_has_vat_number’, 10, 2 );
function wpo_wcpdf_allow_invoice_if_customer_has_vat_number( $allowed, $document ) {
if ( $document->type == ‘invoice’ ) {
if ( $order = $document->order ) {
$allowed = ( ! empty( $order->get_meta( ‘billing_nif’ ) ) ) ? true : false;
}
}
return $allowed;
}The meta key is billing_nif. Where can I found the invoices already sent, in case it is working? I look for them but can not find them. In case it is not working and need some changes, how can I test it without sending real invoices? Thanks in advanced.
Best regards.
Where can I found the invoices already sent, in case it is working?
You can use a mail log plugin like WP Mail Logging to check if the attachments are included in the orders without the VAT number (NIF). If not, the code is working.
In case it is not working and need some changes, how can I test it without sending real invoices?
If you activate the Actions column, from the Screen Option panel in the orders list, you can check if the code is working when the orders without a VAT number has disabled the invoice button (the one with the dollar sign):
Hello Yordan.
Thank you for your quick answer.
I think it’s not working because the Actions column shows nothing, and inside the plugin settings the first invoice number stills showing the number 1, so I think that any invoice was created despite some customers filled the VAT number option.
Any ideas? I copy/paste again the code in case you can see something strange./**
* Enable invoice only if the customer fill the NIF/CIF field
*/
add_filter( ‘wpo_wcpdf_document_is_allowed’, ‘wpo_wcpdf_allow_invoice_if_customer_has_vat_number’, 10, 2 );
function wpo_wcpdf_allow_invoice_if_customer_has_vat_number( $allowed, $document ) {
if ( $document->type == ‘invoice’ ) {
if ( $order = $document->order ) {
$allowed = ( ! empty( $order->get_meta( ‘billing_nif’ ) ) ) ? true : false;
}
}
return $allowed;
}Thank you Yordan.
Regards.Good morning Yordan.
?Any ideas? ?Am I doing something wrong?
Thank you.
Regards.Hi @edosantillana,
Although the code is working at my end, I just added a check to the code in order to display previous generated invoices, even if the customers didn’t include the VAT number:
/** * Enable invoice only if the customer fill the NIF/CIF field */ add_filter( 'wpo_wcpdf_document_is_allowed', 'wpo_wcpdf_allow_invoice_if_customer_has_vat_number', 10, 2 ); function wpo_wcpdf_allow_invoice_if_customer_has_vat_number( $allowed, $document ) { if ( $document->exists() ) { return $allowed; } if ( $document->type == 'invoice' ) { if ( $order = $document->order ) { $allowed = ( ! empty( $order->get_meta( 'billing_nif' ) ) ) ? true : false; } } return $allowed; }
If you still don’t see the invoice button, even if your customer included the VAT number, you’ll need to double-check that your VAT number fields is indeed named
billing_nif
(sometimes it has an underscore at behind:_billing_nif
). Also check if you’re disabling the invoice for certain order statuses, under WooCommerce > PDF Invoices > Documents > Invoice > Disable for setting.Hope it works ??
Hello Yordan.
Now it works! The underscore behind the meta key made the trick. Thank you very much.
I just have a couple of doubts:
You mention: I just added a check to the code in order to display previous generated invoices, even if the customers didn’t include the VAT number.I notice that the invoice button appears only in the customers that included the VAT number at it suppose to be, but the check to the code that you mention should show all the invoices, even if the customer didn’t include the VAT number. As I said, it works fine now, at it suppose to be, but your argument makes me think.
The invoice button appears with a red dot over it, it means that I have to click in the button in order to send the invoice to the customer? Because once click the button a green check appears, or the invoice is sent automatically once the order was completed? In the settings is selected that way, once Completed.
Thank you very much Yordan, great plugin and the best support.
Regards.Hi @edosantillana,
I notice that the invoice button appears only in the customers that included the VAT number at it suppose to be, but the check to the code that you mention should show all the invoices, even if the customer didn’t include the VAT number. As I said, it works fine now, at it suppose to be, but your argument makes me think.
The extra check that I added in my previous code is to allow our plugin to display the documents already created in the past, before you have added the VAT number field. Without that check, those documents will be hidden if the customer didn’t include the VAT number in the order. If you’re using the VAT number field from the beginning, this check it’s not necessary, although I recommend keeping it ??
The invoice button appears with a red dot over it, it means that I have to click in the button in order to send the invoice to the customer? Because once click the button a green check appears, or the invoice is sent automatically once the order was completed? In the settings is selected that way, once Completed.
We use the that red dot to semantically relate our icons to PDF documents, but it doesn’t change. However, there are two types of icons: without and with a green check mark. When you see the green check mark means that the document already exists for that particular order.
When you click on the icon, when it doesn’t yet have the mark, the document will be created, but this doesn’t mean that the document will be sent to the customer. To do so, you need to check the WooCommerce email notification in which you want to attach the invoices, under WooCommerce > PDF Invoices > Documents > Invoice > Attach to:
Please note that the email will create the invoice in order to be able to attachment it to the message. Therefore, it’s recommendable to select only the status email notifications that are sent when the customer paid the order, by default Processing and Completing.
Hello Yordan.
Works like a charm, great support.
Thank you very much.Best regards.
Hi @edosantillana,
I’m glad to know it works! ??
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!
Hello Yordan.
Review done! Thanks again.
Best regards.
- The topic ‘Sending invoice only for some customers’ is closed to new replies.