I've created a custom taxonomy called "vendors" in Woocommerce through this:
function vendor_taxonomy() {
$labels = array(
'name' => 'Vendors',
'singular_name' => 'Vendor',
'menu_name' => 'Vendors',
'all_items' => 'All Vendors',
'parent_item' => 'Parent Vendor',
'parent_item_colon' => 'Parent Vendor:',
'new_item_name' => 'New Vendor Name',
'add_new_item' => 'Add New Vendor',
'edit_item' => 'Edit Vendor',
'update_item' => 'Update Vendor',
'separate_items_with_commas' => 'Separate Vendors with commas',
'search_items' => 'Search Vendors',
'add_or_remove_items' => 'Add or remove Vendors',
'choose_from_most_used' => 'Choose from the most used Vendors',
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'item', 'product', $args );
}
add_action( 'init', 'vendor_taxonomy', 0 );
Now I would like to customize the fields in the admin page. Default fields are Name, Slug and Description. Let's say I would like to add an extra text fields called "featured products", I'm trying something like this below, but nothing happens in the admin page of the taxonomy:
add_action('vendors_add_form_fields', 'vendors_featured_wp_editor_add', 10, 1);
function vendors_featured_wp_editor_add() {
?>
<div class="form-field">
<label for="featured_products"><?php _e('Featured Products', 'wh'); ?></label>
<input type="text" name="featured_products" id="featured_products">
<p class="description"><?php _e('Enter 4 featured products, Comma separated',
'featured'); ?></p>
</div>
<?php
}
...
add_action('vendors_edit_form_fields', 'vendors_featured_wp_editor_edit', 10, 1);
function vendors_featured_wp_editor_edit($term) {
$term_id = $term->term_id;
$featured_products = get_term_meta($term_id, 'featured_products', true);
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="featured_products"><?php _e('Featured
Products', 'featured'); ?></label></th>
<td>
<input type="text" name="featured_products" id="featured_products" value="<?
php echo esc_attr($featured_products) ? esc_attr($featured_products) : ''; ?
>">
<p class="description"><?php _e('Enter 4 featured products, Comma separated',
'featured'); ?></p>
</td>
</tr>
<?php
}
...
add_action('edited_vendors', 'vendors_featured_save_wp_editor', 10, 1);
add_action('create_vendors', 'vendors_featured_save_wp_editor', 10, 1);
function vendors_featured_save_wp_editor($term_id) {
$featured_products = filter_input(INPUT_POST, 'featured_products');
update_term_meta($term_id, 'featured_products', $featured_products);
}
My Answer:
"product_brand" for Woocommerce products. Now, I want to add custom fields to this taxonomy to store additional information about each brand.
To add custom fields to the "product_brand" taxonomy, you can use the following code snippet in your theme's functions.php file or in a custom plugin:
php
// Add custom fields to product_brand taxonomy
function add_brand_custom_fields() {
?>
}
add_action( 'product_brand_add_form_fields', 'add_brand_custom_fields' );
// Save custom fields data
function save_brand_custom_fields( $term_id ) {
if ( isset( $_POST['brand_logo'] ) ) {
update_term_meta( $term_id, 'brand_logo', sanitize_text_field( $_POST['brand_logo'] ) );
}
}
add_action( 'created_product_brand', 'save_brand_custom_fields' );
add_action( 'edited_product_brand', 'save_brand_custom_fields' );
// Display custom fields on edit page
function edit_brand_custom_fields( $term ) {
$brand_logo = get_term_meta( $term->term_id, 'brand_logo', true );
?>
}
add_action( 'product_brand_edit_form_fields', 'edit_brand_custom_fields' );
This code adds a custom field for "Brand Logo" to the "product_brand" taxonomy. The field is displayed on the add/edit term pages and the data is saved using term meta.
You can customize this code further to add more custom fields or modify the existing ones as needed.
Rate this post
3 of 5 based on 4714 votesComments