Support » Plugin: WooCommerce PDF Invoices & Packing Slips » Add Billing Company to filename

  • Resolved mosabua

    (@mosabua)


    Hello

    I used the code from the documentation to change the filename of the invoice pdf and I wanted to add the billing_company. So I tried this code but it didn’t work. Any help would be great

    // Change filenames of PDFInvoice Plugin
    add_filter( 'wpo_wcpdf_filename', 'wpo_wcpdf_custom_filename', 10, 4 );
    function wpo_wcpdf_custom_filename( $filename, $template_type, $order_ids, $context ) {
        $count = count($order_ids);
     
        switch ($template_type) {
            case 'invoice':
                $name = 'angebot';
    			;
                break;
            case 'packing-slip':
                $name = _n( 'packing-slip', 'packing-slips', $count, 'woocommerce-pdf-invoices-packing-slips' );
                break;
            case 'proforma':
                $name = _n( 'proforma-invoice', 'proforma-invoices', $count, 'wpo_wcpdf_pro' );
                break;    
            case 'credit-note':
                $name = _n( 'credit-note', 'credit-notes', $count, 'wpo_wcpdf_pro' );
                break;
            default:
                $name = $template_type;
                break;
        }
     
        if ( $count == 1 ) {
            $document = wcpdf_get_document( $template_type, $order_ids );
            if ( $number = $document->get_number() ) {
                $suffix = $number;
            } elseif (isset($document->order) && method_exists($document->order, 'get_order_number')) {
                $number = $document->order->get_order_number();
            } else {
                $number = $order_ids[0];
            }
            $suffix = $number;
        } else {
            $suffix = date('Y-m-d'); // 2020-11-11
        }
    	
    	$order = wc_get_order($order_ids);
        $filename = $name . '-' . $order->get_billing_company() . '-' . $suffix . '.pdf';
     
        return $filename;
    }
    

    thanks werner

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support kluver

    (@kluver)

    Hi @mosabua,

    They problem is that $order_ids is an array which will result in the wc_get_order() function to return false. The easiest solution here is to purchase our Professional extension. This will let you add the billing company to your filename right in the plugin settings (WooCommerce > PDF Invoices > Documents ).

    Also with regards to your other ticket this would be the best solution. As you need the Professional extension to add the packing slip to one of the WooCommerce emails.

    You can however also achieve adding the billing company to your filename by editing your code snippet a bit:

    add_filter( 'wpo_wcpdf_filename', 'wpo_wcpdf_custom_filename', 10, 4 );
    function wpo_wcpdf_custom_filename( $filename, $template_type, $order_ids, $context ) {
    	$count = count($order_ids);
     
    	switch ($template_type) {
    		case 'invoice':
    			$name = _n( 'invoice', 'invoices', $count, 'woocommerce-pdf-invoices-packing-slips' );
    			break;
    		case 'packing-slip':
    			$name = _n( 'packing-slip', 'packing-slips', $count, 'woocommerce-pdf-invoices-packing-slips' );
    			break;
    		case 'proforma':
    			$name = _n( 'proforma-invoice', 'proforma-invoices', $count, 'wpo_wcpdf_pro' );
    			break;    
    		case 'credit-note':
    			$name = _n( 'credit-note', 'credit-notes', $count, 'wpo_wcpdf_pro' );
    			break;
    		default:
    			$name = $template_type;
    			break;
    	}
     
    	if ( $count == 1 ) {
    		$document = wcpdf_get_document( $template_type, $order_ids );
    		if ( $number = $document->get_number() ) {
    			$suffix = $number;
    		} elseif (isset($document->order) && method_exists($document->order, 'get_order_number')) {
    			$number = $document->order->get_order_number();
    		} else {
    			$number = $order_ids[0];
    		}
    		$suffix = $number;
    		$order = wc_get_order($order_ids[0]);
    		$billing_company = $order->get_billing_company();
    	} else {
    		$suffix = date('Y-m-d'); // 2020-11-11
    	}
      
    	$filename = !empty($billing_company) ? $name . '-' . $billing_company . '-' . $suffix . '.pdf' : $name . '-' . $suffix . '.pdf';
     
    	return $filename;
    }
    Thread Starter mosabua

    (@mosabua)

    Hi @kluver

    thanks for your help with the code and I will buy the premium version of the ticket.

    Plugin Support kluver

    (@kluver)

    No problem. When you want to use the billing company in the file name via the plugin settings you can use the {{billing_company}} placeholder.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add Billing Company to filename’ is closed to new replies.