Support

Account

Home Forums General Issues Manipulate custom fields (created in acf) in a function with save_post action (o

Solved

Manipulate custom fields (created in acf) in a function with save_post action (o

  • Hi,
    in custom post type product of woocommerce, I have a some custom fields (created in ACF) and I need to take action when I save or update the post, that is, I want to add the values ​​of the fields in woocommerce attributes. This is my example code. I have a text field called : ISBN , and I try to pass the value in meta attribute

    //save_post_NAME_CPT
    add_action('save_post_product', 'update_post_meta_subito' , 10, 3 ); 
    
    function update_post_meta_subito( $post_id, $post, $update ) { 
    
            //get field isbn of current post
            $prova_termine = get_field( "isbn", $post_id );
    
             //add value field in global pa_autore attribute
            $term_taxonomy_ids = wp_set_object_terms( $post_id, $prova_termine, 'pa_autore', true );
    
            //add value field in current attribute post
            $data = Array(
    
            'pa_autore'=>Array( 
            'name'=>'pa_autore', 
            'value'=>$prova_termine, 
            'is_visible' => '1', 
            'is_variation' => '1',
            'is_taxonomy' => '1'
            ),
    
            );
    
            update_post_meta( $post_id, '_product_attributes',$data);
        }
    

    This not works! it does not pass any value! But in this case it works:

    
    add_action('save_post_product', 'update_post_meta_subito' , 10, 3 ); 
    
    function update_post_meta_subito( $post_id, $post, $update ) { 
    
            $prova_termine='ronaldo';
    
            $term_taxonomy_ids = wp_set_object_terms( $post_id, $prova_termine, 'pa_autore', true );
    
            $data = Array(
    
            'pa_autore'=>Array( 
            'name'=>'pa_autore', 
            'value'=>$prova_termine, 
            'is_visible' => '1', 
            'is_variation' => '1',
            'is_taxonomy' => '1'
            ),
    
            );
    
            update_post_meta( $post_id, '_product_attributes',$data);
        }
    

    So, in the first function does not take the value of the field, where am I wrong?

  • ACF does not fire on this hook save_post_product, it saves only on the save_post hook which happens after the post type specific hook you are using. This means that ACF has not saved any fields before you are trying to get the values of the field.

    Your choices here are
    1) use the acf/save_post hook https://www.advancedcustomfields.com/resources/acf-save_post/ priority > 10
    2) access the submitted values directly in the $_POST[‘acf’] varialble, information on this is also given in the doc link above.

  • @hube2
    Solved with your help, today I did the function I needed! Thanks!
    I write a piece, maybe someone can help.
    The function takes the title of a post-type related to products (woocommerce) and inserts this title as a woocommerce attribute when save_post action !

    
    add_action('acf/save_post', 'update_post_meta', 20); 
    	
    function update_post_meta( $post_id ) { 
    		
    $post_type = get_post_type($post_id);
    		
    global $product; 
    if ( 'product' == $post_type ) {
    					
    //TAKE RELATED INFO IN POST TYPE 
    $posts = get_field('serie_di_appartenenza');
    if( $posts ): 
    foreach( $posts as $p ):
    $scrittore=get_field("scrittore_appartenenza", $p->ID); 
    if( $posts ): 
    foreach( $scrittore as $s ): 
    $scrittore_ereditato= get_the_title( $s->ID );
    	
    //ADD INFO IN GLOBAL ATTRIBUTE WOOCOMMERCE				
    $term_taxonomy_ids = wp_set_object_terms( $post_id, $scrittore_ereditato, 'pa_scrittore-woo', true );
    					
    endforeach;
    endif; 
    endforeach;
    endif; 
    
    //ADD INFO IN CURRENT POST
    
    $data = Array(
    
    'pa_scrittore-woo'=>Array( 
    'name'=>'pa_scrittore-woo', 
    'value'=>$scrittore_ereditato,
    'is_visible' => '1',
    'is_variation' => '1',
    'is_taxonomy' => '1'
    ),
    );
    
    //UPDATE META					
    update_post_meta( $post_id, '_product_attributes',$data);
    					
    }
    }		
    
    
  • This reply has been marked as private.
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.