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;
}
- The topic ‘Excluding certain categories’ is closed to new replies.