Support » Plugin: WooCommerce PDF Invoices & Packing Slips » Disable invoice for certain categories

Viewing 10 replies - 16 through 25 (of 25 total)
  • Agghhh! Face palm!

    Haha, of course; definitely over thinking this.

    Cool, thanks again.

    Matt

    Hi Ewout,

    Struggling with combining both of these functions (hiding invoice button for a particular category) without creating a heap of errors.

    Any help warmly appreciated.

    Cheers
    Matt

    Same problem, white screen after adding code to child theme. Not using the same function anywhere else. Used as provided, only changed the category names. Woocommerce 3.0.5, using Subscriptions. To clarify, I’m getting the white screen with the first code to exclude product categories, not the code to hide the button.

    • This reply was modified 4 years, 8 months ago by scarlettr8.
    • This reply was modified 4 years, 8 months ago by scarlettr8.
    Plugin Author Ewout

    (@pomegranate)

    I just tested the code you refer to (from this thread) on a site with WC3.0 and don’t get a white screen. I think you may have made a copy paste error or a typo in the code when changing the category names.
    I would recommend using Code Snippets, it’s a lot safer then editing functions.php. Also, if you enable WP_DEBUG you’ll see a PHP error instead of a blank screen, which may help in finding the error. Unfortunately this is beyond the scope of free support…

    Ewout

    Having no luck with this. Added Code Snippets. This is the function Code Snippets:

    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('Membership','Gala');
    
        $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;
        }
    }

    Does not eliminate PDF Invoice from being attached to the email. Thoughts?

    Ewout,

    How about to achieve the opposite?

    So, how to enable creation of invoice in certain situations?

    Thanks.

    • This reply was modified 4 years, 6 months ago by Robbie.
    Plugin Author Ewout

    (@pomegranate)

    Enabling is done by setting the attachments. If you don’t want to send attachments, you’ll have to trigger the invoice creation separately based on the order status. I’m giving a code example for the upcoming 2.0 release (download the release candidate here), because that version is has much better support for this.

    
    add_action('woocommerce_order_status_completed', 'wpo_wcpdf_create_invoice_number');
    function wpo_wcpdf_create_invoice_number ( $order_id ) {
    	$order = wc_get_order( $order_id );
    	$invoice = wcpdf_get_document( 'invoice', $order, true );
    }
    

    Hope that helps!

    Thanks!

    Will deprecated functions and hooks still work in 2.0 ?

    • This reply was modified 4 years, 6 months ago by Robbie.
    Plugin Author Ewout

    (@pomegranate)

    Mostly, yes. I have built a legacy class that backports most deprecated functions.

    I’ve tried this code many times and still get the white screen every time I try it in the functions.php file and it doesn’t work at all when using Code Snippets. Ideas? No other function with this name.

    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 ‘

    ';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;
    }
    }

    • This reply was modified 4 years, 1 month ago by scarlettr8.
Viewing 10 replies - 16 through 25 (of 25 total)
  • The topic ‘Disable invoice for certain categories’ is closed to new replies.