Hello @ckriegel,
Please add this code snippet to your site to achieve it:
/**
* Disable invoice generation if the billing country is Portugal
*/
add_filter( 'wpo_wcpdf_document_is_allowed', 'wpo_wcpdf_disable_invoice_based_on_billing_country', 10, 2 );
function wpo_wcpdf_disable_invoice_based_on_billing_country( $allowed, $document ) {
if ( $order = $document->order ) {
if ($document->type == 'invoice') {
$allowed = ( $order->get_billing_country() == 'PT' ) ? false : true;
}
}
return $allowed;
}
If you haven’t worked with code snippets (actions/filters) or functions.php before, read this guide: How to use filters
Let me know if it works! π
Hi!
Thank you so much for this code, it works on my side π However i’m not able to combine it with this other snippet : https://ww.wp.xz.cn/support/topic/exclude-specific-product-categories-from-invoices/#post-14444864 How can i have both working at the same time ? For now only one can work at a time if i add both in functions.php, i guess because it calls the same function.
Looking forward to your feedback,
kind regards,
Cyrille
Hi!
I’m just checking in to see if you would have any advice regarding the last question?
Thank you very much,
Cyrille
Hello @ckriegel,
Try with this code:
add_filter( 'wpo_wcpdf_document_is_allowed', 'wpo_wcpdf_not_allow_invoice_for_certain_categories', 10, 2 );
function wpo_wcpdf_not_allow_invoice_for_certain_categories ( $condition, $document ) {
if ( $document->type == 'invoice' ) {
if ( $order = $document->order ) {
// Disable invoice generation if the billing country is Portugal
if ( $order->get_billing_country() == 'PT' ) {
return false;
}
//Set categories here (comma separated)
$not_allowed_cats = array( 'donations' );
$order_cats = array();
//Get order categories
foreach ( $order->get_items() as $item_id => $item ) {
// get categories for item, requires product
if ( $product = $item->get_product() ) {
$id = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id();
$terms = get_the_terms( $id, 'product_cat' );
if ( empty( $terms ) ) {
continue;
} else {
foreach ( $terms as $key => $term ) {
$order_cats[$term->term_id] = $term->slug;
}
}
}
}
// get array of category matches
$cat_matches = array_intersect( $not_allowed_cats, $order_cats );
if ( count( $cat_matches ) > 0 ) {
return false; // 1 or more matches: do not allow invoice
}
}
}
return $condition;
}
Let me know if it worked π
Hi Yordan,
Thank you very much for your reply. I am able to have the invoices for portugal excluded with your last code, but for categories i can only have one category excluded at a time, if i put several categories that need to be excluded it doesn’t work. I noticed that the same happens with the initial code provided to exclude categories : https://ww.wp.xz.cn/support/topic/exclude-specific-product-categories-from-invoices/
Maybe there is an issue in the code regarding that? I add the category slugs separated by a comma to the variable not_allowed_cats, as specified.
Thanks for your help on that,
with kind regards,
Cyrille
Hello @ckriegel,
The code it’s working for me, even if I add two or more categories.
Make sure that you’re adding your categories this way (line 12):
//Set categories here (comma separated)
$not_allowed_cats = array( 'accessories', 'music', 'downloads' );
Hi Yordan, thank you for your reply, it works now π i didn’t write it the correct way, as i had put all categories seperated by a comma but within the same brackets
Thanks for your help,
Cyrille
I’m glad to know that it’s working now!
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!