Support

Account

Home Forums Add-ons Repeater Field Updating Repeater Field in Options

Solving

Updating Repeater Field in Options

  • Hi!

    How can I best update a specific field value in an option repeater-field?
    As an example I have created an option repeater-field called ‘shops’, and the sub_fields ‘shop_id’ and ‘shop_amount’. For this I want to check if a specific ‘shop_id’ exist and update the ‘shop_amount’ (for instance when its shop_id == ‘2’).

    Any ideas on how I can do this?

  • 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 );
    	}
    
    }
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Updating Repeater Field in Options’ is closed to new replies.