产品变化的 ACF 中继器字段

ACF Repeater Field on Product Variations

根据 here and here 使用以下代码将 ACF 添加到单个产品变体。一切似乎都显示正确,除了转发器字段:

// Add product variations ACF rule
add_filter('acf/location/rule_values/post_type', 'acf_location_rule_values_Post');
function acf_location_rule_values_Post( $choices ) {
    $choices['product_variation'] = 'Product Variation';
    return $choices;
}

$GLOBALS['wc_loop_variation_id'] = null;

function is_field_group_for_variation($field_group, $variation_data, $variation_post) {
    return (preg_match( '/Variation/i', $field_group['title'] ) == true);
}

add_action( 'woocommerce_product_after_variable_attributes', function( $loop_index, $variation_data, $variation_post ) {
    $GLOBALS['wc_loop_variation_id'] = $variation_post->ID;

    foreach ( acf_get_field_groups() as $field_group ) {
        if ( is_field_group_for_variation( $field_group, $variation_data, $variation_post ) ) {
            acf_render_fields( $variation_post->ID, acf_get_fields( $field_group ) );
        }
    }

    $GLOBALS['wc_loop_variation_id'] = null;
}, 10, 3 );

add_action( 'woocommerce_save_product_variation', function( $variation_id, $loop_index ) {
    if ( !isset( $_POST['acf_variation'][$variation_id] ) ) {
        return;
    }

    $_POST['acf'] = $_POST['acf_variation'][$variation_id];

    acf()->input->save_post( $variation_id );
}, 10, 2 );

add_filter( 'acf/prepare_field', function ( $field ) {
    if ( !$GLOBALS['wc_loop_variation_id'] ) {
        return $field;
    }

    $field['name'] = preg_replace( '/^acf\[/', 'acf_variation[' . $GLOBALS['wc_loop_variation_id'] . '][', $field['name'] );

    return $field;
}, 10, 1);

在转发器字段上添加行时,它不会出现。

然后我在产品保存时收到以下 javascript 错误,无论我点击多少次都会重复 "Add row":

An invalid form control with name='acf_variation[37][field_5db9a1722bbe2][field_5db9a1de2bbe5][acfcloneindex][field_5db9a1ed2bbe6]' is not focusable.

当我检查只有一个 table 行时,有 class="acf-row acf-clone"。 ACF 的样式表显示:

.acf-repeater .acf-row.acf-clone {
    display: none !important;
}

当我添加管理员样式时:

.acf-repeater .acf-row.acf-clone {
    display: table-row !important;
}

显示第一个字段,但仍未添加其他字段(显示和 DOM 检查时。有什么想法吗?

渲染字段后添加脚本:

function rdv__after__render_field( $field ){
    //add these if you want to apply it on certain fields only 
    //if ($field['key'] != 'field_key') {
    //    return;
    //}
    echo "<script>
            (function($) {
                acf.doAction('append', $('#post'));
            })(jQuery);
          </script>";
}
add_action( 'acf/render_field/type=repeater', 'rdv__after__render_field', 10, 1 );

然后将您的 'woocommerce_save_product_variation'(在 https://support.advancedcustomfields.com/forums/topic/acf-on-product-variations-almost-works/ 上找到这篇文章)更改为

add_action( 'woocommerce_save_product_variation', function( $variation_id, $loop_index ) {
    if ( !isset( $_POST['acf_variation'][$variation_id] ) ) {
        return;
    }

    if ( ! empty( $_POST['acf_variation'][$variation_id] ) && is_array( $fields = $_POST['acf_variation'][$variation_id] )  ) {
        foreach ( $fields as $key => $val ) {
            update_field( $key, $val, $variation_id );
        }
    }

}, 10, 2 );