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

如何在WP All Import中为ACF重复器字段新增行且不覆盖原有数据

问题

我正在使用WP All Import和Advanced Custom Fields (ACF),需要实现导入数据时为重复器字段新增行且不覆盖原有数据的功能。具体需求如下:

  • 父重复器字段名为sellers,子字段包括:company(商家名称)、price(价格)、link(链接)
  • 导入时需执行:
    1. 检查导入记录的{company_name[1]}是否与已有company字段值匹配
    2. 若已存在,不做任何操作
    3. 若不存在,新增一行重复器数据,赋值规则:
      company = {company_name[1]}
      price = {search_price[1]}
      link = {y_link[1]}
      
  • 当前导入会覆盖整个重复器字段,删除原有行,需要避免这种情况。

我尝试了以下代码,但无法正常运行:

add_action('pmxi_saved_post', function($post_id, $xml_data, $import_id) {
    if (get_post_type($post_id) !== 'product') return;

    $selector = 'sellers';
    $subfield1 = 'company';
    $subfield2 = 'prijs';
    $subfield3 = 'link';

    $merchant_name = $xml_data['company_name'][1] ?? '';
    $search_price = $xml_data['search_price'][1] ?? '';
    $aw_deep_link = $xml_data['company_name'][1] ?? '';

    if (empty($merchant_name)) return;

    $existing_rows = get_field($selector, $post_id) ?: [];
    foreach ($existing_rows as $row) {
        if ($row[$subfield1] === $merchant_name) return;
    }

    $existing_rows[] = [
        $subfield1 => $company_name,
        $subfield2 => $search_price,
        $subfield3 => $y_link,
    ];
    update_field($selector, $existing_rows, $post_id);
}, 10, 3);
解决方案

原代码存在变量名不匹配、字段名错误等问题,修正后即可实现需求:

错误点说明

  1. 变量名不匹配:定义了$merchant_name,但新增行时误用未定义的$company_name;$y_link未定义,需从$xml_data['y_link'][1]取值
  2. 子字段名错误:需求中子字段是price,代码里写成了prijs,需和ACF后台设置的字段slug一致
  3. 变量赋值错误:$aw_deep_link错误赋值为$xml_data['company_name'][1],应改为$xml_data['y_link'][1]

修正后的代码

add_action('pmxi_saved_post', function($post_id, $xml_data, $import_id) {
    // 仅处理product类型的文章
    if (get_post_type($post_id) !== 'product') return;

    // ACF重复器字段及子字段配置
    $repeater_field = 'sellers';
    $subfield_company = 'company';
    $subfield_price = 'price';
    $subfield_link = 'link';

    // 从导入数据中提取值,键名需和WP All Import模板中的自定义字段名对应
    $import_company = $xml_data['company_name'][1] ?? '';
    $import_price = $xml_data['search_price'][1] ?? '';
    $import_link = $xml_data['y_link'][1] ?? '';

    // 商家名称为空则直接退出
    if (empty($import_company)) return;

    // 获取已有的重复器行数据,默认空数组
    $existing_rows = get_field($repeater_field, $post_id) ?: [];

    // 检查是否已存在相同商家的行
    foreach ($existing_rows as $row) {
        if ($row[$subfield_company] === $import_company) {
            // 已存在,不做操作直接退出
            return;
        }
    }

    // 新增行数据
    $existing_rows[] = [
        $subfield_company => $import_company,
        $subfield_price => $import_price,
        $subfield_link => $import_link,
    ];

    // 更新ACF重复器字段
    update_field($repeater_field, $existing_rows, $post_id);
}, 10, 3);

关键注意事项

  • 确保ACF字段名完全匹配:重复器字段名sellers,子字段company、price、link必须和ACF后台创建的字段slug一致
  • WP All Import变量名要对应:$xml_data['company_name'][1]中的company_name需和导入模板里设置的自定义字段名一致
  • 导入设置调整:在WP All Import的字段映射中,不要直接映射到sellers重复器字段,否则仍会覆盖原有数据,只需确保导入时触发pmxi_saved_post钩子即可(默认会触发)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 23:49:56