Hello dear ACF developers,
I’m trying to modify Woocommerce prices, using a nested ACF loop.
An image worth thousand words, so here is the organization of my custom prices tables.
First, i’m storing those custom values as $cart_object datas.
(Those stored datas are used after, in my calculation functions).
Here is my storing function:
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 ) {
//The loop is well targeted using function parameter
if(have_rows('custom_prices_grids', $product_id)):
$matrice_count = 1; //Counting main loop to organize datas in their matrix
while(have_rows('custom_prices_grids', $product_id)): the_row();
$var_limits = get_sub_field('custom_prices_variation_limitations');
//Targeting 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 matrix 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;
$matrice_count++;
endif;
endwhile; endif;
//All datas are well returned but are not organized as i wanted
return $cart_item_data;
}
My first matrix is fine, but my second matrix get values of current and previous row.
If you add let’s say 3 matrix, the second loop will output values of loop 1 and 2 / third loop will output values of loop 1, 2 and 3 (and so on if you add more).
Here is a screen of my output second loop’s matrix (2 matrix added in this example).
This second loop should output only the values of the current loop (2).
How can i get rid of the previous loop ?
Am i missing something here ?
I hope i’m clear with my problem.
Thanks in advance,
RemSEO.
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;
}
The topic ‘Nested loop flexible and repeater – Organizing datas’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.