Status Change Jobs Code Samples
Need PDF exports that look like real invoices? With addresses above the product list?
Here’s how.
What this code does
Your standard PDF export shows products first. Then order details at the bottom.
This flips that. Billing address, shipping address, phone, and email appear at the top. Just like an invoice.
The complete code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
// PDF format , just like invoice // output order detailsa bove item list add_action("woe_pdf_started", function ($pdf, $formatter) { $reflection = new ReflectionClass($formatter); $property = $reflection->getProperty('storage'); $property->setAccessible(true); $storage = $property->getValue($formatter); $addText = function ($hookRow) use ($pdf, &$addText, $storage) { $storage->loadFull(); $rowObj = $storage->getRow(0); if ( ! $rowObj) { return $hookRow; } $offset = 0; $row = $rowObj->getData(); $order = new WC_Order( $rowObj->getMetaItem( "order_id" )); $billing = WC()->countries->get_formatted_address($order->get_address('billing'), PHP_EOL); $shipping = WC()->countries->get_formatted_address($order->get_address('shipping'), PHP_EOL); $x = $pdf->GetX(); $y = $pdf->GetY(); $rowHeight = 5; $xOffset = 120; $pdf->SetFont('', 'B'); $pdf->Cell(20, $rowHeight, "Billing address:"); $pdf->SetFont(''); $pdf->SetXY($x, $pdf->GetY() + $rowHeight); $pdf->MultiCell(120, 120, $billing); if($order->get_billing_phone()) { $pdf->SetFont('', 'B'); $pdf->Cell(7, $rowHeight, "Tel:"); $pdf->SetFont(''); $pdf->Cell(10, $rowHeight, $order->get_billing_phone()); $pdf->SetXY($x, $pdf->GetY() + $rowHeight); $offset += 20; } if($order->get_billing_email()) { $pdf->SetFont('', 'B'); $pdf->Cell(10, $rowHeight, "Email: "); $pdf->SetFont(''); $pdf->Cell(10, $rowHeight, $order->get_billing_email()); $pdf->SetXY($x, $pdf->GetY() + $rowHeight); $offset += 20; } $pdf->SetXY($x + $xOffset, $y); $pdf->SetFont('', 'B'); $pdf->Cell(20, $rowHeight, "Shipping address:"); $pdf->SetFont(''); $pdf->SetXY($x + $xOffset, $pdf->GetY() + $rowHeight); $pdf->MultiCell(120, 120, $shipping); $pdf->SetXY($x, $y + $offset); remove_filter('woe_row_before_format_pdf', $addText); return $hookRow; }; add_filter('woe_row_before_format_pdf', $addText, 10, 1); }, 10, 2); |
What appears at the top
- Billing address (formatted nicely)
- Shipping address (right column)
- Phone number (if exists)
- Email address (if exists)
All above your product rows. Clean and professional.
When to use this
You send PDF invoices to customers. Need addresses visible immediately.
Your warehouse uses PDF packing slips. Need shipping info before products.
You automate order processing. External system expects invoice-style format.
Setup steps
- Copy the entire code block
- Paste into your theme’s
functions.php - Create or edit a PDF export profile in Advanced Order Export for WooCommerce
- Run a test export on a single order
What you’ll see
First page shows addresses at top. Products below. Phone and email right under billing address.
Second page (if needed) continues products. Addresses don’t repeat.
Customize the layout
Change column width: Edit $xOffset = 120. Lower number moves shipping address left. Higher moves it right.
Remove phone or email: Delete the if($order->get_billing_phone()) block entirely.
Add order number: Insert this after the email block:
php
|
1 2 3 4 5 6 |
$pdf->SetFont('', 'B'); $pdf->Cell(15, $rowHeight, "Order #: "); $pdf->SetFont(''); $pdf->Cell(10, $rowHeight, $order->get_order_number()); $pdf->SetXY($x, $pdf->GetY() + $rowHeight); $offset += 20; |
Change address format: Replace PHP_EOL with "<br/>" for HTML-style line breaks. Or ", " for single line.
Common mistake
PDF formatting is fragile. One wrong character breaks the layout.
Test on one order. Check spacing. Verify addresses don’t overlap.
Also, this only runs for the first order. Subsequent orders in the same PDF won’t show addresses again. That’s by design.
Pro tip
Want addresses on every page? Move the code to woe_pdf_header action instead. It repeats on each page.
Need different formatting for packing slips vs invoices? Duplicate the code. Change the condition. Run based on order status.
Real talk
This code uses PHP reflection. That’s advanced. Copy exactly. Don’t change variable names unless you know what you’re doing.
Works with Advanced Order Export for WooCommerce PDF format. Tested on versions 3.x and above.
Your PDFs will look like real invoices. No more confusing layouts.