好的,所以这可能是不可能的...但是我需要在WooCommerce变体字段中具有ACF灵活的内容。由于此表单的最后一个答案,我设法使该字段显示在管理面板中(还将在下面插入代码)here
但是我被卡在如何在variants.php中显示它上。
代码:
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);
// Add New Variation Settings
add_filter( 'woocommerce_available_variation','load_variation_settings_fields' );
我通常为获取字段而执行的代码,但我需要对其进行修改:
if( have_rows('test') ):
// loop through the rows of data
while ( have_rows('test') ) : the_row();
echo "test";
if( get_row_layout() == 'flexi_1' ):
the_sub_field('test_1');
elseif( get_row_layout() == 'flexi_2' ):
the_sub_field('test_2');
endif;
endwhile;
else :
// no layouts found
endif;
我在这里几乎一无所知,并且是PHP的新手。但是我认为我需要以某种方式使用function load_variation_settings_fields( $variations )
,也许吗?
我尝试使用此代码:
function load_variation_settings_fields( $variations ) {
$variations['text_field'] = get_post_meta( $variations[ 'variation_id' ], $field['name'], true );
return $variations;
}
使用{{{ data.variation.text_field }}}
感谢您的帮助!