• Resolved Lemontec

    (@lemontec)


    Hello, I found this thread, where it is described to exclude product categories from beeing included in an invoice.

    https://www.ads-software.com/support/topic/exclude-specific-product-categories-from-invoices/

    Unfortunately this code does not work anymore? I created a category noinvoice, but when the order state is changed to processing / completed, an invoice number for this order is generated and the invoice is attached to the woocommerce order email, which should also be prevented

    
    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 == 'noinvoice' ) {
    //Set categories here (comma separated)
    $not_allowed_cats = array( 'noinvoice' );
    
    $order_cats = array();
    if ( $order = $document->order ) {
    //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;
    }
    • This topic was modified 12 months ago by Lemontec.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Contributor Yordan Soares

    (@yordansoares)

    Hi @lemontec,

    You have changed the $document->type check too, but you had to change only the $not_allowed_cats, like this:

    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' ) {
    
    		//Set categories here (comma separated)
    		$not_allowed_cats = array( 'noinvoice' );
    
    		$order_cats = array();
    		if ( $order = $document->order ) {
    			//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;
    }

    Try replacing your code with the above, and let me know if it worked! ??

    Thread Starter Lemontec

    (@lemontec)

    Sorry that was a mistake when i copied the code, in the source code the document type check is ‘invoice’

    Plugin Contributor Yordan Soares

    (@yordansoares)

    Thanks for the clarification, Lemontec.

    Did you try replacing your code with the one I sent, just to rule out there were a typo while editing the code snippet?

    Thread Starter Lemontec

    (@lemontec)

    Yes, I tried your code snippet.
    After placing the order its on hold (I use a cheque gateway for testing). Then order status is changed to processing and invoice number is generated and the invoice document sent along with the email.

    Only product in the cart was a product with noinvoice category

    Thread Starter Lemontec

    (@lemontec)

    Does the mentioned method also stop generating invoice numbers, when an order contains one ore more products with the noinvoice category or is it just the document generation which is blocked?

    Plugin Contributor Yordan Soares

    (@yordansoares)

    The code snippet above will completely disable the PDF invoice, that is, there will no created an invoice number either.

    Did you check that your category slug is indeed noinvoice?

    Thread Starter Lemontec

    (@lemontec)

    Plugin Contributor dwpriv

    (@dwpriv)

    I’ve added the ‘noinvoice’ category in my store along with the snippet, but it seems to be working on end. Does the category change when the order status is updated to processing/completed in your store? Are there any errors you can find as outlined here? You’re looking for errors prefixed with wpo_wcpdf or fatal_error

    Plugin Contributor dwpriv

    (@dwpriv)

    Since we haven’t heard from your in a while, I will mark this topic as resolved. Feel free to replay to it should you still need help.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Exclude product category from invoice’ is closed to new replies.