Assuming you registered or enqueued your jQuery script using wp_enqueue_scripts
and assigned it a proper handle, this function will allow you access to the postage_prices
fields in jQuery via a variable postagePricesArray.prices
. You need to replace %YOUR_SCRIPT_HANDLE%
with your jQuery script’s actual handle or this variable may not become accessible.
function localize_postage_prices() {
global $post;
$id = $post->ID;
if(have_rows('postage_prices', $id)) {
$postage_prices_array = array();
foreach(get_field('postage_prices', $id) as $price) {
$postage_prices_array[] = array(
'order_qty' => $price['order_qty'],
'strips_rp' => $price['strips_rp'],
'strips_ep' => $price['strips_ep'],
'roll_rp' => $price['roll_rp'],
'roll_ep' => $price['roll_ep']
);
}
//
// You need to replace %YOUR_SCRIPT_HANDLE% with your script's actual handle!
//
wp_localize_script('%YOUR_SCRIPT_HANDLE%', 'postagePricesArray', array(
'prices' => $postage_prices_array
));
}
}
add_action('wp_enqueue_scripts', 'localize_postage_prices');
Then in your jQuery file (which is enqueued and has a handle of %YOUR_SCRIPT_HANDLE%
) you can access the array:
$(function() {
console.log(postagePricesArray.prices);
});