Untitled

mail@pastecode.io avatar
unknown
plain_text
23 days ago
1.7 kB
5
Indexable
Never
function add_custom_registration_fields() {
    // Get terms from the 'storecat' taxonomy
    $terms = get_terms(array(
        'taxonomy' => 'storecat',
        'hide_empty' => false,
    ));

    if (!empty($terms) && !is_wp_error($terms)) {
        echo '<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">';
        echo '<label for="marketking_select_storecategories">Select Store Categories<span class="required">*</span></label>';
        echo '<select name="marketking_select_storecategories[]" id="marketking_select_storecategories" class="woocommerce-Input woocommerce-Input--text input-text" multiple="multiple" style="width:100%;">';
        // Loop through terms and create options using term ID as value
        foreach ($terms as $term) {
            echo sprintf('<option value="%s">%s</option>', esc_attr($term->term_id), esc_html($term->name));
        }
        echo '</select>';
        echo '</p>';
    }
}
add_action('woocommerce_register_form', 'add_custom_registration_fields', 10000);

function save_custom_registration_fields($customer_id) {
    if (isset($_POST['marketking_select_storecategories'])) {
        $selectedcategories = $_POST['marketking_select_storecategories'];
        if (!empty($selectedcategories)) {
            if (is_array($selectedcategories)) {
                $arraycats = array_map('sanitize_text_field', $selectedcategories);
            } else {
                $arraycats = array(sanitize_text_field($selectedcategories));
            }
            update_user_meta($customer_id, 'marketking_store_categories', $arraycats);
        }
    }
}
add_action('woocommerce_created_customer', 'save_custom_registration_fields');
Leave a Comment