• Resolved fdcsaunar

    (@fdcsaunar)


    I’m trying to figure out how to create a custom file name for the PDF attachments. It needs to have the category name and the order number. For example: “Alemlube-00029.pdf”

    Wrote a custom function for it but the plugin doesn’t seem to recognize my code.

    Code:

    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);
     
    	if ( $count == 1 && $template_type == 'invoice') {
    		$order = wc_get_order($order_ids[0]);
            $items = $order->get_order_items();
            foreach( $items as $item_id => $item ){
                $terms = get_the_terms ( $item['product_id'], 'product_cat' );
                $supplier_name = sanitize_title( $terms[0]->name);
            }
    		# change the filename
    		$filename = $supplier_name.'-'.$order->get_order_number().'.pdf';
    
    	}
    
    	return $filename;
    }

    Thoughts?

    • This topic was modified 3 years, 11 months ago by fdcsaunar.
    • This topic was modified 3 years, 11 months ago by fdcsaunar.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor alexmigf

    (@alexmigf)

    Hi @fdcsaunar

    Try this instead:

    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);
     
    	if ( $count == 1 && $template_type == 'invoice') {
    		$order         = wc_get_order($order_ids[0]);
    		$items         = $order->get_items();
    		$supplier_name = '';
    		foreach( $items as $item_id => $item ) {
    			$terms         = get_the_terms ( $item['product_id'], 'product_cat' );
    			$supplier_name = sanitize_title( $terms[0]->name );
    		}
    		# change the filename
    		if( ! empty( $supplier_name ) ) {
    			$filename = $supplier_name.'-'.$order->get_order_number().'.pdf';
    		}
    	}
    
    	return $filename;
    }
    Thread Starter fdcsaunar

    (@fdcsaunar)

    Thank you so much! It worked!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom file name (product category + order number) for PDF attachments’ is closed to new replies.