• Resolved ckriegel

    (@ckriegel)


    Hi,
    In addition to selling products, which require invoices, we receive donations through woocommerce. For these donations, we shouldn’t generate invoices in the same way as other purchases. Is it possible to exclude product categories (our “donations” categories), so that invoices are not generated at all for these donations ?
    Thank you very much,
    Cyrille

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor kluver

    (@kluver)

    Hi @ckriegel

    You can disable invoice creation for all orders that have products with the ‘donations’ category in them via the following code snippet:

    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( 'donations' );
    
    		$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 code snippet should be added to the functions.php of your child theme or via a plugin like Code Snippets. If you haven’t worked with code snippets or functions.php before please read the following guide: How to use filters

    • This reply was modified 3 years, 7 months ago by kluver.
    Thread Starter ckriegel

    (@ckriegel)

    Hi, thanks a lot for the code snippet ! I’m going to test it

    Thread Starter ckriegel

    (@ckriegel)

    Hi, just to confirm, i tested and it works perfectly, thank you !
    kind regards,
    Cyrille

    Plugin Contributor Yordan Soares

    (@yordansoares)

    Hello @ckriegel,

    I’m glad to know that 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!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Exclude specific product categories from invoices’ is closed to new replies.