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

如何为WooCommerce我的账户单订单页添加带下载按钮的订单项区域

WooCommerce订单页商品发票下载按钮实现方案

以下是基于你的需求修改后的完整代码,直接替换你现有functions.php中的对应内容即可:

// 在订单详情页表格后添加每个商品的发票下载按钮
add_action('woocommerce_order_details_after_order_table', 'add_per_product_invoice_download_buttons');

function add_per_product_invoice_download_buttons($order) {
    // 仅订单所有者或管理员能看到按钮
    if (!current_user_can('manage_woocommerce') && $order->get_user_id() != get_current_user_id()) {
        return;
    }

    echo '<div class="product-invoice-section">';
    echo '<h3>商品发票</h3>';
    echo '<ul class="product-invoice-list">';

    // 遍历订单里的每个商品
    foreach ($order->get_items() as $item_id => $item) {
        $product = $item->get_product();
        if (!$product) continue;

        $product_name = $product->get_name();
        $product_id = $product->get_id();

        // 生成对应商品的下载表单
        echo '<li class="invoice-item">';
        echo '<span class="product-name">' . esc_html($product_name) . '</span>';
        echo '<form method="post" class="invoice-download-form">';
        wp_nonce_field('download_product_invoice_' . $product_id . '_' . $order->get_id(), 'invoice_nonce');
        echo '<input type="hidden" name="order_id" value="' . esc_attr($order->get_id()) . '">';
        echo '<input type="hidden" name="product_id" value="' . esc_attr($product_id) . '">';
        echo '<button type="submit" name="download_product_invoice" class="button">Download this product invoice</button>';
        echo '</form>';
        echo '</li>';
    }

    echo '</ul></div>';
}

// 处理发票下载请求
add_action('init', 'handle_product_invoice_download');

function handle_product_invoice_download() {
    // 检查必要参数是否存在
    if (!isset($_POST['download_product_invoice'], $_POST['order_id'], $_POST['product_id'], $_POST['invoice_nonce'])) {
        return;
    }

    $order_id = absint($_POST['order_id']);
    $product_id = absint($_POST['product_id']);
    $current_user_id = get_current_user_id();

    // 安全验证,防止恶意请求
    if (!wp_verify_nonce($_POST['invoice_nonce'], 'download_product_invoice_' . $product_id . '_' . $order_id)) {
        wp_die('安全验证失败');
    }

    // 获取订单对象
    $order = wc_get_order($order_id);
    if (!$order) {
        wp_die('无效订单');
    }

    // 验证用户是否为订单购买者或管理员
    if (!current_user_can('manage_woocommerce') && $order->get_user_id() != $current_user_id) {
        wp_die('你无权下载该发票');
    }

    // 获取商品信息
    $product = wc_get_product($product_id);
    if (!$product) {
        wp_die('无效商品');
    }
    $product_name = $product->get_name();
    $buyer_email = $order->get_billing_email();

    // 生成发票内容
    $invoice_content = "Hello, dear user Thank you for purchasing {$product_name}. You can receive your invoice by {$buyer_email}.";

    // 设置下载响应头,触发浏览器下载
    header('Content-Type: text/plain');
    header('Content-Disposition: attachment; filename="' . sanitize_file_name($product_name . '_invoice.txt') . '"');
    header('Content-Length: ' . strlen($invoice_content));

    // 输出发票内容并终止脚本
    echo $invoice_content;
    exit;
}

关键功能说明

  • 权限控制:渲染按钮和处理下载时都会验证用户身份,仅订单购买者或管理员能操作
  • 商品绑定:每个按钮对应单独的商品ID,确保下载的是对应商品的发票
  • 安全防护:添加nonce验证,防止跨站请求伪造攻击
  • 文件生成:自动以商品名称命名文本文件,内容包含商品名和购买者邮箱
  • 触发下载:通过设置HTTP头,直接让浏览器触发文件下载,无需跳转

内容的提问来源于stack exchange,提问作者Salvadora

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 11:17:48