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

如何在WooCommerce的ShipStation插件中使用订单项自定义元数据?

解决ShipStation for WooCommerce自定义字段取值问题

为什么直接返回固定文本无效?

你之前用的woocommerce_shipstation_export_custom_field_2钩子,插件内部逻辑是把返回值当作wp_postmeta表的元键,自动调用get_post_meta()去查询对应值。所以直接返回字符串的话,插件会去查不存在的postmeta键,自然没输出。


方案1:返回固定文本到ShipStation

改用更灵活的woocommerce_shipstation_export_order_custom_fields钩子,直接自定义字段值:

add_filter( 'woocommerce_shipstation_export_order_custom_fields', 'shipstation_fixed_custom_field_2', 10, 2 );
function shipstation_fixed_custom_field_2( $custom_fields, $order ) {
    // 给custom_field_2设置固定文本
    $custom_fields['custom_field_2'] = '你的固定文本内容';
    return $custom_fields;
}

方案2:从订单项元数据取值并判断后返回

同样用上述钩子,先遍历订单的订单项,查询_wc_checkout_add_on_value的值,匹配目标值后设置字段内容:

add_filter( 'woocommerce_shipstation_export_order_custom_fields', 'shipstation_order_item_meta_field', 10, 2 );
function shipstation_order_item_meta_field( $custom_fields, $order ) {
    // 替换成你要检查的目标值
    $target_check_value = '你要找的特定值';
    $is_target_found = false;

    // 遍历订单所有订单项
    foreach ( $order->get_items() as $item ) {
        // 获取订单项的_wc_checkout_add_on_value元数据
        $add_on_value = $item->get_meta( '_wc_checkout_add_on_value' );
        
        // 匹配目标值则标记为找到
        if ( $add_on_value === $target_check_value ) {
            $is_target_found = true;
            break;
        }
    }

    // 根据结果设置custom_field_2的内容
    $custom_fields['custom_field_2'] = $is_target_found ? '要发送到ShipStation的固定文本' : '';
    
    return $custom_fields;
}

使用说明

  1. 把代码添加到你的主题functions.php文件,或者使用自定义代码插件(如Code Snippets)
  2. 替换代码中的占位文本为你实际需要的内容
  3. 测试订单导出,确认ShipStation能收到对应字段值

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 03:12:07