Quality Website design & Development Services

Hand-coded development for conversion into Wordpress

WordPress Woocommerce add button in order list Actions to overwrite PDF

In this Example we will discuss how to implement PDF overwrite inside a WordPress theme. We will be using a button to perform our task of overwriting an Adobe-PDF document for the orders list – actions column (post type order) in Woocommerce.


Add a Button in WordPress Woocommerce shop order actions column to overwrite PDF


Step 1. How to add a button in the wordpress backend’s custom post type’s column?


In our example we will add a button in the Woocommerce Order list Action Column.



You have to use the following code in functions.php to perform the above task:


add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 10, 2 );
function custom_orders_list_column_content($column_name, $post_id)
   global $post, $woocommerce, $the_order;

      echo "<p><a href='/wp-admin/admin-ajax.php?action=my_action&order_id=$post_id' class='pdf-over button' target='blank' style='float:left; margin-top:0px; height:27px;' data-order='$post_id'>PDF</a></p>";





To add any html in the post list, WordPress has an action hook called “manage_posttype_posts_cutom_column” . Here posttype is the custom post type slug.


Woocommerce order has a post type slug “shop_order”. You can see in the url that looks :



What we get after invoking this action hook?


We get $column name and $post_id in the invoked function after we call this action. You can see those two variables in the function parameter.

After clicking the button You browser will open admin-ajax.php in a new tab . Also we are sending the order-id as a query string in the admin-ajax.php to overwrite my adobe document with the details related to certain order based on order-id.


Step 2. How to perform PDF overwrite through admin-ajax.php and download the new overwrited file ?


First make a folder in wp-content folder named “pdf-work” . Here we will put the document, what we will use to overwrite. Please download the sample file , it has two pages. GYS


Your url of the file will be “yoursite/wp-content/pdf-work/GYS.pdf” .


Also make a folder in your selected theme folder named “pdf-overwrite”. Here we will add the library what we will use to perform our overwriting work. We will use fpdf with fpdi – 2 libraries .

You can download this zip file pdf-overwrite . It contains the libraries.


Your url will be “yoursite/wp-content/themes/yourtheme/pdf-overwrite/the_files_under_the_zip_file” .


Now in functions.php file we will write the code to handle admin-ajax.php in which we will write into the original file  and download the new file.

Here is the code:


add_action( 'wp_ajax_my_action', 'my_action' );

function my_action() {
	global $wpdb; // this is how you get access to the database
	$order_id = intval( $_GET['order_id'] );
        //echo $order_id;
        $order = wc_get_order( $order_id );
        $name=$first_name." ".$last_name;
		$address2=$o2." ".$o3;
		$address3=$o4." ".$o5. " ".$o6;
// initiate FPDI 
	$pdf = new FPDI(); 

// set the sourcefile 
	// add a page1 
	// import page 1 
	$tplIdx = $pdf->importPage(1); 
	// use the imported page as the template 
	$pdf->useTemplate($tplIdx, 0, 0); 
	// now write some text above the imported page 
	$pdf->SetFont('Arial' , '' , 9); 
	$pdf->SetXY(12, 28); 
	$pdf->Write(100, "$name");
	$pdf->SetXY(12, 32); 
	$pdf->SetXY(12, 36); 
	$pdf->SetXY(12, 40); 
	$pdf->SetXY(12, 44); 
	$pdf->SetXY(100, 55); 
	$pdf->SetXY(12, 123); 
	$pdf->Write(100,"Order number: $order_id"); 
	// add a page 2
	$tplIdx = $pdf->importPage(2); 
	// use the imported page as the template 
	$pdf->useTemplate($tplIdx, 0, 0); 

	// now write some text above the imported page 
	$pdf->SetXY(25, 25); 
	$pdf->Write(100, "This is just a simple text on page 2 . The order id is : $order_id"); 
	$pdf->Output($newfile1, 'D'); 
	wp_die(); // this is required to terminate immediately and return a proper response



* : Required Field