Get parent category field from child category [closed]

You would need to either get the color of the direct parent or loop thorugh all ansestors until you find the color.
If no color found get the default and output.

I made a few changes to the structure and added all comments for each step.

Notice that I included two options to achive the same result, choose the one that works for your needs.

// get category terms of the current post
if ($terms = get_the_terms( get_the_ID(),'category')) {
    // prepare the output for sprintf
    // to avoid repeating ourselves we can create a variable to conatin the output
    // %s is a sting placeholder
    $output="<div class="post_loop-cat" style="background-color: %s;">%s</div>";

    // unless you always have one categpry for every post
    // this will always set $cat_id to the last $term of the loop, always
    // take this into account
    foreach ($terms as $term) {
        $cat_id = $term->term_id;
    }

    // get ACF category color
    $cat_color = get_field('category-color', 'category_' . $cat_id);

    // because we don't know the depth of the category
    // or how high do you want to go to get the parent color
    // we can do either of the following
    // in both options we will first check if $cat color exists

    // option 1
    // using the direct parent only

    if ($cat_color) { // color found, output the html
        echo sprintf($output, $cat_color, $term->name);
    } else {
        // get the parent category color
        $cat_color = get_field('category-color', 'category_' . $term->parent);

        // if not found, set color to default
        if (empty($cat_color)) $cat_color = get_field('primary-color', 'option');

        // output the html
        echo sprintf($output, $cat_color, $term->name);
    }

    // option 2
    // using the category ancestors to find the color
    // we loop each ancestor, once we found a color we stop and output

    if ($cat_color) { // color found, output the html
        echo sprintf($output, $cat_color, $term->name);
    } else {
        // get and loop all ansestors
        foreach (get_ancestors($cat_id, 'category') as $ancestor_id) {
            // get and check category color, if found, break out of the loop
            if ($cat_color = get_field('category-color', 'category_' . $ancestor_id)) break;
        }

        // if not found, set color to default
        if (empty($cat_color)) $cat_color = get_field('primary-color', 'option');

        // output the html
        echo sprintf($output, $cat_color, $term->name);
    }
}

In order to not repeat the output I used a variable that will later be used with sprintf, click the link for more information.
quick description, sprintf creates a formatted string.