function generate_and_download_pdf_invoice() { // Get order ID from request $order_id = isset($_GET['order_id']) ? intval($_GET['order_id']) : 0; // Verify nonce if (!isset($_GET['nonce']) || !wp_verify_nonce($_GET['nonce'], 'download_pdf_invoice_' . $order_id)) { wp_die('Security check failed'); } // Get order object $order = wc_get_order($order_id); if (!$order || !is_a($order, 'WC_Order')) { wp_die('Invalid order'); } ob_start(); // Start output buffering to prevent header issues // Load DomPDF require plugin_dir_path(__FILE__) . 'dompdf/vendor/autoload.php'; // Set DomPDF Options $options = new \Dompdf\Options(); $options->set('isRemoteEnabled', true); // Allow remote images // Initialize DomPDF with options $dompdf = new \Dompdf\Dompdf($options); // Get order details $order_number = $order->get_order_number(); $order_date = $order->get_date_created(); $order_date = $order_date ? $order_date->date('Y-m-d H:i:s') : date('Y-m-d H:i:s'); $customer_name = trim($order->get_billing_first_name() . ' ' . $order->get_billing_last_name()); $customer_email = $order->get_billing_email(); $customer_phone = $order->get_billing_phone(); $customer_address = $order->get_formatted_billing_address(); $payment_method = $order->get_payment_method_title(); // e.g., "Credit Card", "PayPal" $order_total = $order->get_total(); // Get the site logo URL $logo_url = get_theme_mod('custom_logo'); $logo_src = wp_get_attachment_image_src($logo_url, 'full'); $logo_image = $logo_src ? $logo_src[0] : ''; // Start HTML $html = '
Invoice |
|
Name: ' . esc_html($customer_name) . ' | |
Email: ' . esc_html($customer_email) . ' | Order No: #' . esc_html($order_number) . ' |
Phone: ' . esc_html($customer_phone) . ' | Date: ' . esc_html($order_date) . ' |
Address: ' . wp_kses_post($customer_address) . ' | Payment: ' . esc_html($payment_method) . ' |
Product | Prescription / Image | Total | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
' . $product_title; // Get custom options $custom_options = $item->get_meta('_custom_options_full', true); $pres_image = isset($custom_options['prescription_image']) ? $custom_options['prescription_image'] : ''; $prescription_table = isset($custom_options['prescription_table']) ? $custom_options['prescription_table'] : ''; if (!empty($custom_options)) { $html .= ' | '; } $html .= '';
// Create a better layout for prescription and image
$html .= ' ';
// Left side - Prescription table
$html .= ' '; // End prescription-layout
$html .= '';
if (!empty($prescription_table)) {
$html .= ' ';
// Right side - Prescription image
$html .= '
';
if (!empty($pres_image)) {
$html .= ' ';
$html .= ' |
' . $total . ' |