Home / eCommerce / WC Vendors – Automate PDF Invoice for vendor orders
Duplicate Snippet

Embed Snippet on Your Site

WC Vendors – Automate PDF Invoice for vendor orders

This snippet automates sending invoices automatically without anyone needing to click "Send Invoice" manually.

It runs automatically whenever an order moves to "Processing", it looks up every vendor on that order and sends each of them their invoice using the exact same process the manual "Send Invoice" button uses, no admin or vendor action required. It won't resend to a vendor who's already had an invoice sent for that order, and it respects your existing PDF Invoices setting, if that's turned off, nothing happens.

To apply it, add this to your child theme's functions.php, or via the WPCode Lite plugin if you have it installed. As always, please test it on a staging site first and take a full backup before applying it to your live site.

Code Preview
php
<?php
add_action( 'woocommerce_order_status_processing', function( $order_id ) {
    if ( 'yes' !== get_option( 'wcvendors_vendor_invoice_enabled', 'no' ) ) {
        return;
    }
    $order = wc_get_order( $order_id );
    if ( ! $order ) {
        return;
    }
    // Skip per-vendor child orders — only process the main customer-facing order.
    if ( $order->get_meta( 'wcv_vendor_id' ) ) {
        return;
    }
    $vendor_ids = array_filter( array_map( 'absint', (array) WCV_Vendors::get_vendor_ids_from_order( $order ) ) );
    if ( empty( $vendor_ids ) ) {
        return;
    }
    $emails = WC()->mailer()->get_emails();
    if ( empty( $emails['WCV_Pro_Email_Vendor_Invoice'] ) ) {
        return;
    }
    foreach ( $vendor_ids as $vendor_id ) {
        if ( $order->get_meta( '_wcv_invoice_sent_' . $vendor_id ) ) {
            continue;
        }
        $vendor_items = WCVendors_Pro_Invoice_PDF::get_vendor_items( $order, $vendor_id );
        if ( empty( $vendor_items ) ) {
            continue;
        }
        $emails['WCV_Pro_Email_Vendor_Invoice']->trigger( $order_id, $vendor_id );
        $order->update_meta_data( '_wcv_invoice_sent_' . $vendor_id, current_time( 'mysql' ) );
    }
    $order->save();
}, 20 );

Comments

Add a Comment