Support

Account

Home Forums Add-ons Repeater Field Updating Repeater Field in Options Reply To: Updating Repeater Field in Options

  • I feel like I’m really close to a solution, but yet so far away! The function is working when the ‘import_id’ doesn’t exist, but it goes terribly (loopy) wrong when the ‘import_id’ does exists, and only needs to update the ‘last_updated’ field. Can anyone please help me with this?

    function shop_update() {
    	//test ID
    	$import_id = '2';
    
    	$key = 'field_54c0cd2019a8a'; //field key for 'shops' repeater
    	$date = date( 'F j, Y g:i a', current_time('timestamp') );
    	$post_id = 'option';
    	$exists = false;
    
    	$shops = get_field($key, $post_id);
    
    	if( have_rows($key, 'option') ) {
    
    		//check if import_id exists in current Shops
    		while (have_rows($key, 'option')) {
    			the_row();
    
    			if( get_sub_field('import_id') == $import_id )
    			{
    				$exists = true;
    			}
    		}
    
    	}
    
    	//edit or add repeater row if item does or doesn't exist
    	if(  $exists  == false )
    	{
    		//add the import_id in total
    		$shops[] = array('import_id' => $import_id, 'last_update' => $date );
    		update_field( $key, $shops, $post_id );
    	}
    	else
    	{
    		if( have_rows('field_54c0cd2019a8a', 'option') ) {
    
    			//update 'last_update' subfield
    			while (have_rows('field_54c0cd2019a8a', 'option')) {
    				the_row();
    
    				if( get_sub_field('import_id') == $import_id )
    				{
    					$shops[] = array("import_id" => $import_id, "last_update" => $date );
    				}
    
    			}
    
    		}
    
    		update_field( $key, $shops, $post_id );
    	}
    
    }