How can I set the value of an ACF, dependent upon the woocommerce product type?
I have this currently:
function whatis_single_product_type( $field ) {
$field['value'] = __('initialize', 'txtdomain');
global $post;
if( function_exists('get_product') ){
$product = get_product( $post->ID );
if( $product->is_type( 'variable' ) ){
$field['value'] = __('variable', 'txtdomain');
} elseif( $product->is_type( 'simple' ) ){
$field['value'] = __('simple', 'txtdomain');
}
}
return $field;
}
add_filter( 'acf/load_field/name=product_type_set', whatis_single_product_type );
Although this seems to work – saving the text, either ‘variable’ or ‘simple’ to the acf product_type_set (otherwise it stays set to ‘initialize’) – does anyone have a better way?
So I’ve changed it to:
/* sets the acf to product type text */
add_filter('acf/load_field/name=product_type', function($field) {
global $post;
$field['value'] = get_product( $post->ID )->get_type();
return $field;
});
… which works, but I get an error (There has been a critical error on this website) when attempting to edit the field group for the acf. What can I do to avoid that (other than temporarily removing the filter above)?
Use acf/prepare_field instead
That did the trick – thank you so much!