Support » Plugin: PDF Invoices & Packing Slips for WooCommerce » Excluding certain categories
Excluding certain categories
-
I’ve tried using the code below to exclude the deposit and event categories, however invoice are still being attached to orders for products in these categories. Should the code below work? Any help much appreciated..
add_filter( ‘wpo_wcpdf_custom_attachment_condition’, ‘wpo_wcpdf_exclude_product_categories’, 100, 4 );
function wpo_wcpdf_exclude_product_categories( $condition, $order, $status, $template_type ) {
// only apply check on invoices
if ($template_type != ‘invoice’) {
return $condition;
}// define categories which shouldn’t get an invoice here
$no_invoice_cats = array(‘deposit’,’event’);$items = $order->get_items();
$order_cats = array();
foreach ($items as $item_id => $item) {
// get categories for item, requires product
$product = $order->get_product_from_item( $item );
if ($product) {
if (empty($terms)) {
continue;
}
$terms = get_the_terms( $product->id, ‘product_cat’ );
foreach ($terms as $key => $term) {
// echo ‘';var_dump($term);echo '
‘;die();
$order_cats[$term->term_id] = $term->name;
}
}
}// get array of category matches
$cat_matches = array_intersect($no_invoice_cats, $order_cats);
if ( count($cat_matches) > 0 ) {
// 1 or more matches, don’t attach invoice
return false;
} else {
return $condition;
}
-
Hi! There’s a small error in that code that would skip checking the first product. Also note that the category names are case sensitive, so double check that your ‘deposit’ and ‘event’ categories are all lowercase like you wrote in the code:
add_filter( 'wpo_wcpdf_custom_attachment_condition', 'wpo_wcpdf_exclude_product_categories', 100, 4 ); function wpo_wcpdf_exclude_product_categories( $condition, $order, $status, $template_type ) { // only apply check on invoices if ($template_type != 'invoice') { return $condition; } // define categories which shouldn't get an invoice here $no_invoice_cats = array('deposit','event'); $items = $order->get_items(); $order_cats = array(); foreach ($items as $item_id => $item) { // get categories for item, requires product $product = $order->get_product_from_item( $item ); if ($product) { $terms = get_the_terms( $product->id, 'product_cat' ); if (empty($terms)) { continue; } foreach ($terms as $key => $term) { $order_cats[$term->term_id] = $term->name; } } } // get array of category matches $cat_matches = array_intersect($no_invoice_cats, $order_cats); if ( count($cat_matches) > 0 ) { // 1 or more matches, don't attach invoice return false; } else { return $condition; } }
Let me know if that works for you!
Ewoutp.s. there’s a ‘code’ button in the forum post editor, if you wrap your code in those backticks (
`), that will make the code more readable.
Thanks very much for the quick reply. That works now – thank you! And for the tip about adding code to posts 🙂
Fantastic, very glad to hear that!
Have a great week!
EwoutHello! First off, thank you for a wonderful plugin.
Now, If I understand correctly, the code above is used to exclude certain categories, so that PDF invoices are not attached in emails from orders that contain products of said categories… right?
If that’s the case, I can’t make it work. I have already categorized all my virtual products under the same category, and tried the code above.
Just in case you can lend a hand (and there’s an easier approach), what I need is to exclude invoice attachment for orders that contain virtual products. I only have physical and virtual products on my shop. That said, it should behave like this:
If order contains a…
Physical product => Attach invoice on email.
Physical product + virtual product => Do NOT attach invoice on email.
Physical product => Do NOT attach invoice on email.I thought the code you fixed for @guymondo would solve this, but apparently I’m getting something wrong.
Thank you in advance!!
Hello Francisco,
That’s indeed how this should work, although there may be just one step missing in your flow: This filter does not enable the attachment, it only overrides the attachment setting you have already put into place in the plugin settings.That said, if this is only to check for virtual products, then doing this with the category filter is unnecessarily complex. Can you try this?
add_filter( 'wpo_wcpdf_custom_attachment_condition', 'wpo_wcpdf_exclude_virtual_products', 100, 4 ); function wpo_wcpdf_exclude_virtual_products( $condition, $order, $status, $template_type ) { // only apply check on invoices if ($template_type != 'invoice') { return $condition; } $items = $order->get_items(); foreach ( $items as $item_id => $item ) { // check if product is virtual, requires product object if ( $product = $order->get_product_from_item( $item ) ) { if ( $product->is_virtual() ) { // virtual product, do not attach invoice return false; } } } // no virtual products found, don't override setting return $condition; }
Let me know if that does the trick for you!
EwoutWorks like a CHARM!! You sir, are wizard of the code.
Thank you very much for your (swift) help,
Frankvery glad to hear that Francisco, and thanks for the compliment 🙂
If you can spare a minute, I’d appreciate a plugin review: https://wordpress.org/support/plugin/woocommerce-pdf-invoices-packing-slips/reviews/have a great day!
Ewout
- The topic ‘Excluding certain categories’ is closed to new replies.