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

如何在Woocommerce结账订单审核及发票中显示自定义产品长度数据

问题描述

我的店铺产品价格由用户在产品页提交的**Length(长度)**字段计算得出,该长度是重要信息,需要在购物车、结账页、订单审核后台、发票以及全流程所有产品相关位置显示。目前已实现购物车展示,但不知道如何在其他页面/环节显示,求可用的Filter/Action钩子及具体实现方法。

现有代码

// Get custom field value Length, calculate new item price, save it as custom cart item data
add_filter('woocommerce_add_cart_item_data', 'add_custom_field_data', 20, 2 );
function add_custom_field_data( $cart_item_data, $product_id ){
    if (! isset($_POST['length']))
        return $cart_item_data;

    $length = (float) sanitize_text_field( $_POST['length'] );
    if( empty($length) )
        return $cart_item_data;

    $product    = wc_get_product($product_id); // The WC_Product Object
    $base_price = (float) $product->get_regular_price(); // Product reg price

    // New price calculation
    $new_price = (($base_price * $length)/1000)+1.90;

    // Set the custom amount in cart object
    $cart_item_data['custom_data']['extra_charge'] = (float) $length;
    $cart_item_data['custom_data']['new_price'] = (float) $new_price;
    $cart_item_data['custom_data']['unique_key'] = md5( microtime() . rand() ); // Make each item unique

    return $cart_item_data;
}

// Set the new calculated cart item price
add_action( 'woocommerce_before_calculate_totals', 'new_price_add_length', 20, 1 );
function new_price_add_length( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    foreach ( $cart->get_cart() as $cart_item ) {
        if( isset($cart_item['custom_data']['new_price']) )
            $cart_item['data']->set_price( (float) $cart_item['custom_data']['new_price'] );
    }
}

//Add length to product in cart
add_action( 'woocommerce_after_cart_item_name', 'wmw_display_length', 20  );
function wmw_display_length( $cart_item ) {
    if( isset($cart_item['custom_data']['extra_charge']) ) {
        $length = $cart_item['custom_data']['extra_charge'];
        echo '<dl class="variation"><dt class="variation-Length">Length(mm):</dt><dd class="variation-Length"><p>' . $cart_item['custom_data']['extra_charge'] . '</dd></dl>';
    }
}
解决方案

要实现全流程显示长度数据,核心是先把购物车中的自定义长度数据同步保存到订单项目中,之后通过对应钩子在各个页面调用显示:

1. 保存长度数据到订单项目

添加以下代码,将购物车中的长度数据保存为订单项的元数据,这是后续所有页面显示的基础:

// 保存自定义长度数据到订单项目
add_action('woocommerce_checkout_create_order_line_item', 'save_length_to_order_item', 20, 4);
function save_length_to_order_item($item, $cart_item_key, $cart_item, $order) {
    if (isset($cart_item['custom_data']['extra_charge'])) {
        $length = $cart_item['custom_data']['extra_charge'];
        // 添加订单项元数据,第一个参数是元键,第二个是显示名称,第三个是是否在后台可见
        $item->add_meta_data('Length(mm)', $length, true);
    }
}

2. 结账页面显示长度数据

在结账页的购物项目中显示长度,使用woocommerce_checkout_cart_item_quantity钩子:

// 结账页面显示长度
add_filter('woocommerce_checkout_cart_item_quantity', 'display_length_checkout', 20, 3);
function display_length_checkout($quantity_html, $cart_item, $cart_item_key) {
    if (isset($cart_item['custom_data']['extra_charge'])) {
        $length = $cart_item['custom_data']['extra_charge'];
        $quantity_html .= '<br><span class="length-info">Length(mm): ' . $length . '</span>';
    }
    return $quantity_html;
}

3. 后台订单审核页面显示长度

在后台订单详情的订单项中显示长度,使用woocommerce_admin_order_item_values钩子:

// 后台订单审核页显示长度
add_action('woocommerce_admin_order_item_values', 'display_length_admin_order', 20, 3);
function display_length_admin_order($_product, $item, $item_id) {
    if ($item->get_meta('Length(mm)')) {
        echo '<td><strong>Length(mm):</strong> ' . $item->get_meta('Length(mm)') . '</td>';
    }
}

4. 前台订单详情页显示长度

在用户查看的前台订单详情中显示长度,使用woocommerce_order_item_meta_start钩子:

// 前台订单详情页显示长度
add_action('woocommerce_order_item_meta_start', 'display_length_frontend_order', 20, 4);
function display_length_frontend_order($item_id, $item, $order, $plain_text) {
    if ($length = $item->get_meta('Length(mm)')) {
        echo '<div class="length-meta"><strong>Length(mm):</strong> ' . $length . '</div>';
    }
}

5. 发票显示长度数据

如果使用WooCommerce自带的发票功能,或者常用的WooCommerce PDF Invoices & Packing Slips插件,可使用以下钩子:

// PDF发票中显示长度(适配WooCommerce PDF Invoices插件)
add_action('wpo_wcpdf_before_item_meta', 'display_length_pdf_invoice', 10, 3);
function display_length_pdf_invoice($template_type, $item, $order) {
    if ($length = $item->get_meta('Length(mm)')) {
        echo '<tr><th>Length(mm):</th><td>' . $length . '</td></tr>';
    }
}

// 通用发票/订单详情中显示长度(兼容更多场景)
add_filter('woocommerce_get_item_data', 'display_length_general_order', 20, 2);
function display_length_general_order($item_data, $cart_item) {
    if (isset($cart_item['custom_data']['extra_charge'])) {
        $item_data[] = array(
            'key' => 'Length(mm)',
            'value' => $cart_item['custom_data']['extra_charge']
        );
    }
    // 订单场景下从订单项元数据取
    if (isset($cart_item['item']) && $length = $cart_item['item']->get_meta('Length(mm)')) {
        $item_data[] = array(
            'key' => 'Length(mm)',
            'value' => $length
        );
    }
    return $item_data;
}

6. 订单邮件通知中显示长度

在所有订单相关的邮件通知里显示长度,使用woocommerce_email_order_items_args钩子:

// 订单邮件中显示长度
add_filter('woocommerce_email_order_items_args', 'include_length_in_email', 20);
function include_length_in_email($args) {
    $args['show_meta'] = true; // 确保订单项元数据显示在邮件中
    return $args;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 04:54:50