根据此处和此处,使用以下代码将 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 错误,但我点击“添加行”多次重复:
name='acf_variation[37][field_5db9a1722bbe2][field_5db9a1de2bbe5][acfcloneindex][field_5db9a1ed2bbe6]' 的无效表单控件不可聚焦。
当我检查只有一个表格行时,带有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 中。有什么想法吗?