Hi,
I created 4 custom fields which are part of calculations that determine what value is placed in a “price” field.
base_price
promo_code
promo_flat
promo_pct
price (not a custom field)
I wrote up a bit of code and placed in my functions.php file and the calculation and the updating of the price field works great!
However, the problem is, if I bulk import products via a CSV file, all the custom field values get uploaded but the the price field does not update because the calculation of it’s determined value relies on updating the post via the update function within WP.
So the only way to update the prices after a bulk upload would be to go into each post individually and update the post.
So how do I get the calculated field to update after a bulk upload?
In case it’s helpful, here is the code I put in my functions.php file…
`
add_action( ‘save_post’, ‘update_price’ );
get_field(‘base_price’, $post->ID);
get_field(‘promo_code’, $post->ID);
get_field(‘promo_flat’, $post->ID);
get_field(‘promo_pct’, $post->ID);
function update_price($post_id) {
if ( get_field(‘promo_flat’) == true &&
get_field(‘promo_code’) == true &&
get_field(‘promo_pct’) == false ):
$num = preg_replace(‘/[\$,]/’, ”, get_field(‘promo_flat’));
$num = floatval($num);
$total = get_field(‘base_price’) – $num;
$value = $total;
$post_meta = “price”;
update_post_meta($post_id, $post_meta, $value);
elseif
( get_field(‘promo_pct’) == true &&
get_field(‘promo_code’) == true &&
get_field(‘promo_flat’) == false ):
$dec = str_replace(‘%’, ”, get_field(‘promo_pct’)) / 100;
$total = get_field(‘base_price’) – (get_field(‘base_price’) * $dec);
$value = $total;
$post_meta = “price”;
update_post_meta($post_id, $post_meta, $value);
else:
$total = get_field(‘base_price’);
$value = $total;
$post_meta = “price”;
update_post_meta($post_id, $post_meta, $value);
endif;
}