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;

    if($column_name=="order_actions"){
      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>";

    }
}

 

Explanation:

 

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 :

your_site/wp-admin/edit.php?post_type=shop_order

 

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'] );
	include('pdf-overwrite/fpdf.php'); 
	include('pdf-overwrite/fpdi.php'); 
        //echo $order_id;
        $order = wc_get_order( $order_id );
        $first_name=$order->billing_first_name;
        $last_name=$order->billing_last_name;
        $name=$first_name." ".$last_name;
	    
		$o1=$order->billing_company;
		$o2=$order->billing_address_1;
		$o3=$order->billing_address_2;
		$o4=$order->billing_city;
		$o5=$order->billing_state;
		$o6=$order->billing_postcode;
		$o7=$order->billing_country;
		$address1=$o1;
		$address2=$o2." ".$o3;
		$address3=$o4." ".$o5. " ".$o6;
		$country=$o7;
// initiate FPDI 
	$pdf = new FPDI(); 

// set the sourcefile 
	$pdf->setSourceFile('../wp-content/pdf-work/GYS.pdf');
//-----------------------------------------------------------------------------------	
	// add a page1 
	$pdf->AddPage(); 
	// 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->SetTextColor(0,0,0); 
	$pdf->SetXY(12, 28); 
	$pdf->Write(100, "$name");
	$pdf->SetXY(12, 32); 
    $pdf->Write(100,"Address:$address1");
	$pdf->SetXY(12, 36); 
	$pdf->Write(100,"$address2"); 
	$pdf->SetXY(12, 40); 
	$pdf->Write(100,"$address3"); 
	$pdf->SetXY(12, 44); 
	$pdf->Write(100,"$country"); 
	$pdf->SetXY(100, 55); 
	$pdf->Write(100,"$country"); 
	$pdf->SetXY(12, 123); 
	$pdf->Write(100,"Order number: $order_id"); 
//-------------------------------------------------------------------------------------	
	
	// add a page 2
	$pdf->AddPage(); 
	$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->SetFont('Arial'); 
	$pdf->SetTextColor(255,0,0); 
	$pdf->SetXY(25, 25); 
	$pdf->Write(100, "This is just a simple text on page 2 . The order id is : $order_id"); 
//---------------------------------------------------------------------------------------
	$newfile="../wp-content/pdf-work/order".$order_id.".pdf";
	$newfile1="woo".$order_id.".pdf";
	$pdf->Output($newfile1, 'D'); 
	wp_die(); // this is required to terminate immediately and return a proper response
	exit();
}

 

X

* : Required Field