• Resolved claudioliverano

    (@claudioliverano)


    Hi all,

    I was following the steps shown in this topic, I want to exclude several products from invoice generation.

    I used the following code (in code snippet plugin) to do that, but this code worked one time (idk why, I used it, then I disabled the plugin for 5 mins, then I activated it again).

    add_filter( 'wpo_wcpdf_custom_attachment_condition', 'wpo_wcpdf_exclude_products', 100, 4 );
    function wpo_wcpdf_exclude_products( $condition, $order, $status, $template_type ) {
        // only apply check on invoices
        if ($template_type != 'invoice') {
            return $condition;
        }
    
        // define product IDs which shouldn't get an invoice here
        $no_invoice_products = array( 101, 102 );
    
        // loop through order items and cancel attachment if one of the products present
        $items = $order->get_items();
        foreach ($items as $item_id => $item) {
        	if (in_array($item->get_product_id(), $no_invoice_products)) {
    	        // matching product, don't attach invoice
        		return false;
        	}
        }
    
        // if we got here, there were no matching products
        return $condition;
    }

    What should I do? I need a code that disables several product IDs from generating invoices

    Thanks for the support!

    EDIT: I was wondering if…
    Perhaps it is better to act in the reverse way in my case: enable the generation of invoices only for specific products (because there are so many products to be excluded)

    EDIT2: The products are subcriptions that renew every month/year
    EDIT3: I recreated the code snipper and now… he worked! … I keep this topic open asking what I wrote in “EDIT:”, if possible…

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor alexmigf

    (@alexmigf)

    Hi @claudioliverano

    How many products do you have that could generate Invoices?

    Thread Starter claudioliverano

    (@claudioliverano)

    Hi @alexmigf

    They are 18 products (3 subscription with several variation that are considered other products)

    Plugin Contributor Ewout

    (@pomegranate)

    @claudioliverano do note that your code does not disable the invoice, it disables the automatic generation of the invoice. Even with that code it can still be generated manually from the backend. If you need to disabled it everywhere (including the bulk actions) you will want to use wpo_wcpdf_document_is_allowed instead. Here’s an example: Only Send Invoice for Only One Product

    Perhaps it is better to act in the reverse way in my case: enable the generation of invoices only for specific products (because there are so many products to be excluded)

    In terms of functionality it won’t matter much, but if that makes your life as a coder easier, then I would certainly take that approach.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Deactivate invoice for several products’ is closed to new replies.