Support

Account

Home Forums ACF PRO update_sub_field does nothing

Helping

update_sub_field does nothing

  • Hi!
    I’am getting issues with update_sub_field – it doesn’t do anything.
    In the same loop I have get_sub_field which returns correct value, same as correct calculation of $newPrice

    What I’am doing wrong?
    Thanks for the advice!

    Here is my code:

    if ($r->have_posts()) :
    		while ( $r->have_posts() ) : $r->the_post();
    			if(have_rows('object_additional')) {
    				while(have_rows('object_additional')) {
    					the_row();
    					//$projectSquareAll = get_sub_field('object_add_2');
    					$projectSquare = get_sub_field('object_add_1');
    					$projecFloor = get_sub_field('object_add_8');
    				}
    			}			
    			if(have_rows('object_main')) {
    				while(have_rows('object_main')) {
    					the_row();
    					$projectPrice = get_sub_field('object_price');
    					
    					error_log(print_r('---------------', true));
    					error_log(print_r('Проект '.get_the_title().' ID: '.$r->post->ID, true));
    					//от 50 до 100м2
    					if (($projectSquare >= 50) && ($projectSquare <= 100) && ($projecFloor[0] == '1 этаж')) {			
    						$priceForMeter = strtok($table['data'][1][1], 'тг');
    						$newPrice = $priceForMeter * $projectSquare;						
    						error_log(var_dump(update_sub_field('object_price', $newPrice, $r->post->ID)));
    					} elseif (($projectSquare >= 50) && ($projectSquare <= 100) && ($projecFloor[0] == '2 этажа')) {
    						$priceForMeter = strtok($table['data'][2][1], 'тг');
    						$newPrice = $priceForMeter * $projectSquare;
    						update_sub_field('object_price', $newPrice, $r->post->ID);
    					}
    
    					//от 100 до 150м2
    					if (($projectSquare > 100) && ($projectSquare <= 150) && ($projecFloor[0] == '1 этаж')) {			
    						$priceForMeter = strtok($table['data'][1][2], 'тг');
    						$newPrice = $priceForMeter * $projectSquare;
    						update_sub_field('object_price', $newPrice, $r->post->ID);
    						//error_log(print_r($projectSquare, true));
    					} elseif (($projectSquare > 100) && ($projectSquare <= 150) && ($projecFloor[0] == '2 этажа')) {
    						$priceForMeter = strtok($table['data'][2][2], 'тг');
    						$newPrice = $priceForMeter * $projectSquare;
    						update_sub_field('object_price', $newPrice, $r->post->ID);
    					}	
    
    					//от 150 до 200м2
    					if (($projectSquare > 150) && ($projectSquare <= 200) && ($projecFloor[0] == '1 этаж')) {			
    						$priceForMeter = strtok($table['data'][1][3], 'тг');
    						$newPrice = $priceForMeter * $projectSquare;
    						update_sub_field('object_price', $newPrice, $r->post->ID);
    						//error_log(print_r($projectSquare, true));
    					} elseif (($projectSquare > 150) && ($projectSquare <= 200) && ($projecFloor[0] == '2 этажа')) {
    						$priceForMeter = strtok($table['data'][2][3], 'тг');
    						$newPrice = $priceForMeter * $projectSquare;
    						update_sub_field('object_price', $newPrice, $r->post->ID);
    					}	
    
    					//от 200 до 400м2
    					if (($projectSquare > 200) && ($projectSquare <= 400) && ($projecFloor[0] == '1 этаж')) {			
    						$priceForMeter = strtok($table['data'][1][4], 'тг');
    						$newPrice = $priceForMeter * $projectSquare;
    						update_sub_field('object_price', $newPrice, $r->post->ID);
    						//error_log(print_r($projectSquare, true));
    					} elseif (($projectSquare > 200) && ($projectSquare <= 400) && ($projecFloor[0] == '2 этажа')) {
    						$priceForMeter = strtok($table['data'][2][4], 'тг');
    						$newPrice = $priceForMeter * $projectSquare;
    						update_sub_field('object_price', $newPrice, $r->post->ID);
    					}	
    
    					//от 400м2
    					if (($projectSquare > 400) && ($projecFloor[0] == '1 этаж')) {			
    						$priceForMeter = strtok($table['data'][1][5], 'тг');
    						$newPrice = $priceForMeter * $projectSquare;
    						update_sub_field('object_price', $newPrice, $r->post->ID);
    						//error_log(print_r($projectSquare, true));
    					} elseif (($projectSquare > 400) && ($projecFloor[0] == '2 этажа')) {
    						$priceForMeter = strtok($table['data'][2][5], 'тг');
    						$newPrice = $priceForMeter * $projectSquare;
    						update_sub_field('object_price', $newPrice, $r->post->ID);
    					}
    					
    					//Если данные не заданы
    					if (((!$projectSquare) && (!$projecFloor[0])) || ((!$projectSquare) && ($projecFloor[0])) ||
    					(($projectSquare) && (!$projecFloor[0]))) {
    						
    					}
    					//$res = return update_sub_field('object_price', $newPrice);
    					error_log(print_r('---------------', true));
    				}
    			} 
    		endwhile;
    		wp_reset_postdata();
    	endif;
  • The post ID is not needed here

    
    update_sub_field('object_price', $newPrice, $r->post->ID);
    

    just like it is not needed here

    
    $projectPrice = get_sub_field('object_price');
    

    In addition to this $r->post->ID in incorrect by your code, this value is probably not defined and it causing update_field() to work incorrectly.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.