Support

Account

Home Forums General Issues Calculated (dynamic) field won't update after bulk upload

Helping

Calculated (dynamic) field won't update after bulk upload

  • 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;
    }

  • What tool are you using to bulk upload your CSV?

    My guess is that the import tool is not setting the field_key reference in the database, which is why you need to edit and save every post after the import.

    The only types of fields that can be successfully imported without also adding the field_key reference to the DB are the field types listed under “Basic” when adding a field to a field group.

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

You must be logged in to reply to this topic.