Detect price group term period on CRM_Member_Form_MembershipRenewal or CRM_Member_Form_Membership
Use this function:

There is a field price_ which contains the ID of the price set selected

Iterate through price sets to find this ID, and if membership matches, use this number of terms

Be sure to set membership type ID as $membership_type_id

                $membership_num_terms = 1;

                if (isset($form-&gt;_values['fee'])) {

                    foreach ($form-&gt;_values['fee'] as $index =&gt; $values) {

                        if (isset($form-&gt;_params['price_' . $index])) {
                            $id = intval($form-&gt;_params['price_' . $index]);
                        } else {
                            $id = null;
                        }

                        foreach ($values['options'] as $option_index =&gt; $option_values) {
                            if (($option_index == $id) &amp;&amp; ($option_values['membership_type_id'] == $membership_type_id)) {
                                $membership_num_terms = $option_values['membership_num_terms'];
                            }
                        }
                    }
                }