• Resolved tezalsec

    (@tezalsec)


    Hi,

    thanks for a great plugin. I was wondering if you have any code snippet to customize the filename based on the amount spent? I would like to be able to include in the filename if the total price is zero (free) or not.

    I did read your page:
    https://docs.wpovernight.com/woocommerce-pdf-invoices-packing-slips/custom-pdf-filenames/

    Should I do something like this? What is the amount/price total variable?

    $document = wcpdf_get_document( $template_type, $order_ids );
    if ($document->order->get_order_price() == 0) etc..;

    Cheers!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Darren Peyou

    (@dpeyou)

    Hi @tezalsec,

    You’re looking for this:

    $order_total = $document->order->get_total();

    So my code for what you need looks like this, though you could insert “free” in different ways:

    
    add_filter( 'wpo_wcpdf_filename', 'wpo_wcpdf_custom_filename', 10, 4 );
    function wpo_wcpdf_custom_filename( $filename, $template_type, $order_ids, $context ) {
    	// get document object in order to get access to order object
    	$document = wcpdf_get_document( $template_type, $order_ids );
    	$order_total = $document->order->get_total();
    	
    	// check if the order total is zero
    	if ($order_total == 0 || $order_total == 0.00) {
    		$invoice_string = _n( 'invoice', 'invoices', count($order_ids), 'woocommerce-pdf-invoices-packing-slips' );
        	$new_prefix = _n( 'invoice-FREE', 'invoices-FREE', count($order_ids), 'woocommerce-pdf-invoices-packing-slips' );
        	$new_filename = str_replace($invoice_string, $new_prefix, $filename);
    
    		
    		return $new_filename;
    
    	} else {
    		// return without modifications
    		return $filename;
    	}
    }
    
    Thread Starter tezalsec

    (@tezalsec)

    Hi @dpeyou ,

    cool, it’s working, thanks a lot! ??

    Plugin Contributor Darren Peyou

    (@dpeyou)

    Hi @tezalsec,

    That’s awesome, I’m glad. ??
    I’d like to make an update to my previous code however. It doesn’t check for whether or not there is a single order existing in a bulk export. This code will fix that for you, thus being safer:

    add_filter( 'wpo_wcpdf_filename', 'wpo_wcpdf_custom_filename', 10, 4 );
    function wpo_wcpdf_custom_filename( $filename, $template_type, $order_ids, $context ) {
    ?
    	foreach( $order_ids as $key => $order_id ) {
    		$order = wc_get_order( $order_id );
    ?
    		//chech if bulk order has at least an order
    		if( empty($order) ) { return $filename; }
    ?
    		// grab the order total
    		$order_total = $order->get_total();
    	}
    	
    	// check if the order total is zero
    	if ($order_total == 0 || $order_total == 0.00) {
    		$invoice_string = _n( 'invoice', 'invoices', count($order_ids), 'woocommerce-pdf-invoices-packing-slips' );
        	$new_prefix = _n( 'invoice-FREE', 'invoices-FREE', count($order_ids), 'woocommerce-pdf-invoices-packing-slips' );
        	$new_filename = str_replace($invoice_string, $new_prefix, $filename);
    ?
    		// append '.free.pdf' 
    		return $new_filename;
    ?
    	} else {
    		// return without modifications
    		return $filename;
    	}
    }
    • This reply was modified 4 years ago by Darren Peyou. Reason: safer code
    Thread Starter tezalsec

    (@tezalsec)

    Hey @dpeyou , thanks for the update, but I think I got that covered by my existing check on count($order_ids) which I borrowed from your docs:

    
        if ($template_type == 'invoice' && count($order_ids) == 1 ) {    
    	
            // is order total zero/free?
            $document = wcpdf_get_document( $template_type, $order_ids );
    	$order_total = $document->order->get_total();
    	if ($order_total == 0 || $order_total == 0.00) {
    		$free = '-free';
    	} else {
    		$free = '';
    	}
        etc...
    
    Plugin Contributor Darren Peyou

    (@dpeyou)

    @tezalsec,

    You’re right, that is a better way to go about it. Just in case for others who might view this thread, my updated code snippet would be:

    
    add_filter( 'wpo_wcpdf_filename', 'wpo_wcpdf_custom_filename', 10, 4 );
    function wpo_wcpdf_custom_filename( $filename, $template_type, $order_ids, $context ) {
    	if ( $template_type != 'invoice' || count( $order_ids ) > 1 ) {
    		return $filename;
    	}
    	$order = wc_get_order( $order_ids[0] );
    	$order_total = $order->get_total();
    	// check if the order total is zero
    	if ( floatval( $order_total ) == 0.00 ) {
    		$invoice_string = _n( 'invoice', 'invoices', count($order_ids), 'woocommerce-pdf-invoices-packing-slips' );
    		$new_prefix = _n( 'invoice-FREE', 'invoices-FREE', count($order_ids), 'woocommerce-pdf-invoices-packing-slips' );
    		$filename = str_replace($invoice_string, $new_prefix, $filename);
    	}
    	return $filename;
    }
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Customize pdf filename based on amount’ is closed to new replies.