Support

Account

Home Forums ACF PRO Nested loop flexible and repeater – Organizing datas Reply To: Nested loop flexible and repeater – Organizing datas

  • Thanks to a friend, i finally managed to organize my datas.

    When you’re creating an array to store your values in a loop, this array doesn’t reset at each loop.
    You just have to reset it after each nested loop.

    And voilĂ ! You have some custom $cart_object datas, ready to use to modify prices based on whatever you want.
    Maybe there is a cleaner way to do it, but for now, it’s working as attended for WordPress 4.9.x/5.x.x and Woocommerce 3.5+.

    If it could be usefull for anyone here is the corrected code

    add_filter( 'woocommerce_add_cart_item_data', 'woocommunity_custom_cart_item_data', 20, 3 );
    function woocommunity_custom_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
    
    //Targeting right product using function parameter
    //And counting main loop to organize datas in their matrix
    if(have_rows('custom_prices_grids', $product_id)):
    $matrice_count = 0;
    while(have_rows('custom_prices_grids', $product_id)): the_row();
    	
    	//General limitation - already good to go
    	$var_limits = get_sub_field('custom_prices_variation_limitations');
    	$role_limits = get_sub_field('custom_prices_role_limitations');
    	
    	//Targeting layout - really facultative here as i have a single layout
    	if( get_row_layout() == 'custom_prices_grid_details' ):
    		
    		//Reorder each field in arrays
    		while(have_rows('custom_prices_quantity_steps')): the_row();
    
    			$min_qty[] = intval(get_sub_field('custom_prices_quantity_min'));
    			$max_qty[] = intval(get_sub_field('custom_prices_quantity_max'));
    			$custom_price[] = intval(get_sub_field('custom_prices_qty_new_price'));
    			$fixed_price_var[] = intval(get_sub_field('custom_prices_qty_fixed_value'));
    			$percent_price_var[] = intval(get_sub_field('custom_prices_qty_percent_value'));
    			$var_type[] = get_sub_field('price_type_calculation');
    		
    		endwhile;
    		
    		//Storing those fields in their matrice as $cart_item_datas
    		$cart_item_data['matrice-'.$matrice_count]['promo_type'] = $var_type;
    		$cart_item_data['matrice-'.$matrice_count]['min_qty'] = $min_qty;
    		$cart_item_data['matrice-'.$matrice_count]['max_qty'] = $max_qty;
    		$cart_item_data['matrice-'.$matrice_count]['new_price'] = $custom_price;
    		$cart_item_data['matrice-'.$matrice_count]['fixed_vars'] = $fixed_price_var;
    		$cart_item_data['matrice-'.$matrice_count]['percent_vars'] = $percent_price_var;
    		$cart_item_data['matrice-'.$matrice_count]['limits_by_var'] = $var_limits;
    		$cart_item_data['matrice-'.$matrice_count]['limits_by_role'] = $role_limits;
    		
    		//Cleaning arrays at each loop
    		//Contrary to single values stored - Our arrays don't reset at each loop
    		unset($var_type); 
    		unset($min_qty); 
    		unset($max_qty); 
    		unset($custom_price); 
    		unset($fixed_price_var); 
    		unset($percent_price_var); 
    		unset($var_limits); 
    		unset($role_limits);
    		
    	$matrice_count++;
    	endif;
    	
    endwhile; endif;
    
    return $cart_item_data;
    }