You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WP 3.3后台查看订单按钮弹窗:模板位置及自定义修改咨询

Finding the Order Popup Template & Adding a PDF Button

Hey there! Since WordPress core doesn’t include an order system by default, I’m assuming you’re working with WooCommerce (the most common e-commerce plugin for WP, which had functionality aligned with WP 3.3 back in the day). Let’s break this down step by step:

1. Locate the Popup Template File

That order view popup loads its content from a template file in WooCommerce. For versions matching WordPress 3.3 (roughly WooCommerce 1.x), you’ll find it here:
wp-content/plugins/woocommerce/templates/admin/order-view.php

Pro Tip: Never edit core plugin files directly—they’ll get overwritten when you update WooCommerce. Instead, copy this file to your child theme’s directory, keeping the same folder structure:
wp-content/themes/your-child-theme/woocommerce/admin/order-view.php
Now your customizations will stay safe through updates.

2. Add the "Create PDF" Button

Open the copied template file and find a logical spot to add your button (like near the existing order action buttons at the top or bottom of the popup). Insert this code:

<button type="button" class="button button-primary create-pdf-button" data-order-id="<?php echo $order->id; ?>">Create PDF</button>

Add Functionality to the Button

Next, you need to make the button do something. Add this code to your child theme’s functions.php file:

// Add JS for the PDF button click
add_action('admin_footer', 'pdf_order_button_script');
function pdf_order_button_script() {
    ?>
    <script>
        jQuery(document).on('click', '.create-pdf-button', function() {
            const orderId = jQuery(this).data('order-id');
            // Redirect to a custom PDF generation endpoint
            window.location.href = '<?php echo admin_url('admin.php?action=generate_order_pdf&order_id='); ?>' + orderId;
        });
    </script>
    <?php
}

// Handle PDF generation
add_action('admin_action_generate_order_pdf', 'generate_order_pdf');
function generate_order_pdf() {
    // Check user permissions
    if (!current_user_can('manage_woocommerce')) {
        wp_die("Sorry, you don't have permission to generate order PDFs.");
    }

    $order_id = isset($_GET['order_id']) ? absint($_GET['order_id']) : 0;
    $order = wc_get_order($order_id);

    if (!$order) {
        wp_die("Invalid order ID.");
    }

    // Here, integrate a PDF library (like TCPDF or DOMPDF) to generate the PDF
    // Example skeleton (you'll need to set up the library first):
    ob_start();
    // Load WooCommerce's order details template for content
    wc_get_template('order/order-details.php', array('order' => $order));
    $order_html = ob_get_clean();

    // Generate and download the PDF (replace with your library's code)
    // require_once get_stylesheet_directory() . '/libs/tcpdf/tcpdf.php';
    // $pdf = new TCPDF();
    // $pdf->AddPage();
    // $pdf->writeHTML($order_html);
    // $pdf->Output('Order-' . $order_id . '.pdf', 'D');

    exit;
}

3. Bonus: Use Hooks Instead of Editing Templates (Safer!)

If you want to avoid editing template files entirely, you can use WooCommerce’s built-in action hooks to add the button directly. Add this to your functions.php:

add_action('woocommerce_admin_order_view_after_order_details', 'add_pdf_button_to_popup');
function add_pdf_button_to_popup($order) {
    echo '<button type="button" class="button button-primary create-pdf-button" data-order-id="' . $order->id . '">Create PDF</button>';
}

This will inject the button right after the order details in the popup, no template edits needed.


内容的提问来源于stack exchange,提问作者publikz.com

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:51:05