Viewing 15 replies - 1 through 15 (of 25 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    Hi Damian,
    You can disable invoice generation with the wpo_wcpdf_custom_attachment_condition filter.

    Here’s an example that collects all the product categories for all products in an order and checks if they match your selection and if so, cancels the attachment. Manual invoice creation will still be possible.

    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('Music','Clothing');
    
        $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 '<pre>';var_dump($term);echo '</pre>';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;
        }
    }

    Hope that helps!
    Ewout

    Hi,

    Having the same problem – need to exclude 2 categories from automatic invoice generating. Copied the code above to theme functions and replaced Clothing and Music with my categories. The categories I would like to disable automatic invoice generations are prepaid Credit categories and for these – the code for some reason does not work.

    Any ideas?

    BR,
    Kaspar

    • This reply was modified 8 years, 7 months ago by kasparjarve.

    Update – since the credit system works so that the customer can purchase credit (that is also a product) and afterwards can pay with that – the filter above does work when you use the credit for payment (it doesn’t matter what products you are buying), but it does not work when you use some other payment for any category. Also tried to disable the credit system – the filter still does not work.

    Hope you can help.

    BR,
    Kaspar

    • This reply was modified 8 years, 7 months ago by kasparjarve.
    Plugin Contributor Ewout

    (@pomegranate)

    Hello Kaspar,
    This filter checks for product categories, but you’re referring to payment method too. It’s not possible to know which ‘products’ were used to buy credit, since that’s a separate order?
    I’m not sure I fully understand your setup though… Again, the filter only checks the categories of the products in the current order.
    For payment methods you have to use another function.

    Hope that helps!
    Ewout

    Hi Ewout

    Doesn’t seem to be working for me. I’ve used the above code and replaced with my categories, but regardless of product purchased (with or without exluded cat) the pdf’s still showing next to orders in admin and appending invoice emails.

    Any reason this might be?

    Thanks in advance

    Plugin Contributor Ewout

    (@pomegranate)

    Hi Matt,
    The code above is only determining when the invoice should be sent as an email attachment. The packing slip is not sent by email (in the free version), so this code doesn’t influence that. The buttons are always shown but no indication of whether anything was generated (if there is no packing slip yet you should be able to click the button to create one). Note that a packing slip is never actually generated since it doesn’t have specific data like the invoice has (invoice number and date). It’s always generated on the fly.

    Ewout

    • This reply was modified 8 years, 2 months ago by Ewout.

    Hi Ewout,

    I’ve sorted (hidden) the packing-slip from view, but PDF invoices still attach to the order email regardless of whether i use the above code to cancel the creation of the invoice.

    Are you saying the code should only stop invoice pdfs from being attached to Admin order emails, or should they not be present anywhere (unless manually generated?). In my case the invoice pdf is created for all orders, even when trying to stop it from creating invoices for specific cateogries e.g. Music or Clothing.

    Does that make sense? I’m not sure if i’m being unclear, or i’m misunderstanding what the filter above is supposed to be stopping.

    Thanks again.

    • This reply was modified 8 years, 2 months ago by mattboden.
    Plugin Contributor Ewout

    (@pomegranate)

    Only attachment. It will still show the button in the admin (and My Account, depending on your settings).

    Ah, okay. Thanks for clarifying.

    So in ALL cases the PDF is actually generated. There is only the ability to stop it attaching to an Admin email (and/or just a customer order email?).

    I assume there is no means of stopping a PDF from generating at all – for certain categories or products?

    • This reply was modified 8 years, 2 months ago by mattboden.
    Plugin Contributor Ewout

    (@pomegranate)

    No a PDF is never generated automatically unless it’s attached: When is the PDF invoice number assigned?.

    Ahhh, okay now it makes sense.

    Okay, so what I actually want to do is stop the invoice from sending by any email (which I can do via the dashboard settings).

    BUT more importantly what I want to do is remove the BUTTON that generates the PDF from the Admin order list for certain products (can be by product, categories or some other means to filter out products). Is that possible somehow?

    Might need to rethink if not.

    Thanks for indulging me.

    Matt

    • This reply was modified 8 years, 2 months ago by mattboden.
    Plugin Contributor Ewout

    (@pomegranate)

    Yes that’s possible with the same filter you used to hide the packing slip buttons.
    Here’s an example that works with the order status but you can expand that to categories:
    https://wordpress.org/support/topic/invoice-is-generated-also-with-a-not-confirmend-order/#post-7545411

    Hi Ewout,

    Thanks for the new snippet. It’s giving me a white screen of death though.

    https://wordpress.org/support/topic/invoice-is-generated-also-with-a-not-confirmend-order/#post-7545411

    Also, i’d like to remove invoice button if an order is not part of a category, rather than the status of order.

    Is that possible?

    Thanks again for all your time on this.
    Matt

    Hi again,

    The “white screen of death” seems to be due to me using a similar code to hide the packing-slip button. When i use these together it seems to breaking something.

     */
    add_filter( 'wpo_wcpdf_listing_actions', 'wpo_wcpdf_restrict_invoice_button', 20, 2 );
    add_filter( 'wpo_wcpdf_meta_box_actions', 'wpo_wcpdf_restrict_invoice_button', 20, 1 );
    
    function wpo_wcpdf_restrict_invoice_button ($actions, $order = '' ) {
    	unset( $actions['packing-slip']);
    	return $actions;
    }
    

    Hmmm, conundrum…

    • This reply was modified 8 years, 2 months ago by mattboden.
    Plugin Contributor Ewout

    (@pomegranate)

    Hi Matt,
    You can’t have two functions with the same name. Either rename one (also in the add_filter calls), or unify them into one single function.

    It’s definitely possible to check the product categories instead of the order status. You could borrow some code from the filter originally posted in this thread to loop over the products and collect the categories. That requires a bit more PHP knowledge though.

    Good luck!
    Ewout

Viewing 15 replies - 1 through 15 (of 25 total)
  • The topic ‘Disable invoice for certain categories’ is closed to new replies.