Support

Account

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

  • @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);
    					
    }
    }