WooCommerce修改functions.php过滤器未同步至Integromat/Make数据问题
问题分析与解决方案
原代码不生效的原因
你添加的woocommerce_countries和woocommerce_states过滤器仅作用于WooCommerce的前端页面、后台订单编辑页等渲染场景,不会修改WooCommerce REST API返回的数据结构——而Integromat/Make是通过调用WooCommerce REST API来获取订单数据的,所以自然拿不到转换后的完整名称。
需求完全可行,解决方案:修改REST API返回数据
要让API返回国家/州的完整名称,需要通过woocommerce_rest_prepare_shop_order过滤器来修改订单的响应数据,将缩写替换为对应全名。以下是具体代码示例:
add_filter('woocommerce_rest_prepare_shop_order', 'modify_order_address_full_names_in_api', 10, 3); function modify_order_address_full_names_in_api($response, $order, $request) { // 获取WooCommerce内置的国家和州列表 $countries = WC()->countries->get_countries(); $states = WC()->countries->get_states(); // 处理账单地址 if (!empty($response->data['billing']['country']) && isset($countries[$response->data['billing']['country']])) { $response->data['billing']['country_full_name'] = $countries[$response->data['billing']['country']]; } if (!empty($response->data['billing']['state']) && isset($states[$response->data['billing']['country']][$response->data['billing']['state']])) { $response->data['billing']['state_full_name'] = $states[$response->data['billing']['country']][$response->data['billing']['state']]; } // 处理收货地址 if (!empty($response->data['shipping']['country']) && isset($countries[$response->data['shipping']['country']])) { $response->data['shipping']['country_full_name'] = $countries[$response->data['shipping']['country']]; } if (!empty($response->data['shipping']['state']) && isset($states[$response->data['shipping']['country']][$response->data['shipping']['state']])) { $response->data['shipping']['state_full_name'] = $states[$response->data['shipping']['country']][$response->data['shipping']['state']]; } return $response; }
代码说明
- 这段代码会在订单API响应中新增
country_full_name和state_full_name字段(分别对应账单和收货地址),保留原缩写字段的同时补充完整名称,避免影响其他依赖原字段的功能。 - 依赖WooCommerce内置的
WC_Countries类获取标准的国家/州全名映射,无需手动维护映射表。
验证与使用步骤
- 将代码添加到主题的
functions.php文件或自定义插件中; - 调用WooCommerce订单API(比如
GET /wp-json/wc/v3/orders/{id})查看响应,确认新增字段已包含完整名称; - 在Integromat/Make的WooCommerce模块中,选择新增的
country_full_name和state_full_name字段同步到Airtable即可。
补充提示
如果需要直接替换原缩写字段(比如把billing->country直接改成全名),可以修改代码中的赋值逻辑,但不建议这么做——部分第三方工具或内部功能可能依赖原缩写字段,替换后可能引发兼容性问题。
内容的提问来源于stack exchange,提问作者Anthony
相关产品推荐
相关产品推荐

