0
votes

I have a custom post type called "Products" and I have a taxonomy called "Price range" and a custom field called "product-number".

In price range I have a term called "250 kr", "450kr", "580kr" and so on.

What I need is to have a way to add data to the custom field based on which term they are in.

Example of what I want:

All products in the "250 kr" taxonomy term need to have 200-"unique-number" added to the "product-number" custom field created from Advanced Custom Fields plugin.

So 200-1, 200-2, 200-3 etc. and in the 450 I need 400-1, 400-2 and so on.

I need it to do it for all the previous created products, and also when creating new. So when creating a new product, it needs to look at what taxonomy it is in and then what the previous "product-number" was.

So if the last product is created in the "250 kr" term and has the product number 200-68 the next product I create needs to automatically get 200-69 in the "product number" custom field.

I've looked at auto-infringement in ACF:

https://support.advancedcustomfields.com/forums/topic/need-help-for-auto-increment-field-with-update_field/

I've tried adding this with some terms, but does not work.

add_option('ebo_dernier_num_club','3005','','no');
function ebo_renseigne_num_affiliation($value, $post_id, $field) {
    if (! $value && get_post_type($post_id)=='club_type') {
        $last_num = (int) get_option('ebo_dernier_num_club', '3000');
        $last_num++;
        update_option('ebo_dernier_num_club', $last_num);
        $value = $last_num;
    }
    return $value;
}
add_filter('acf/update_value/name=numero_affiliation', 'ebo_renseigne_num_affiliation', 10, 3);

I expect it to auto increment based on the taxonomy term and previous products created.

1

1 Answers

0
votes

Use this function. update_field($selector, $value, [$post_id]);

You can follow this link: https://www.advancedcustomfields.com/resources/update_field/