I am working with a custom theme and would like to add a custom upload field in the woocommerce admin section. Particularly, the categories section. I want to add another field just like the thumbnail upload that stores the information.
raunak-gupta in Adding custom field to product category in WooCommerce shares a way forward which I took on and produced this. HELP???
Added uploader.js
jQuery(document).ready( function($){
var mediaUploader_woo;
$('#upload-button-woo').on('click',function(e) {
e.preventDefault();
if( mediaUploader_woo ){
mediaUploader_woo.open();
return;
}
mediaUploader_woo = wp.media.frames.file_frame = wp.media({
title: 'Choose an Image',
button: { text: 'Choose Image'},
multiple: false
});
mediaUploader_woo.on('select', function(){
attachment = mediaUploader_woo.state().get('selection').first().toJSON();
$('#meta-woo').val(attachment.url);
});
mediaUploader_woo.open();
});
});
I called the media uploader in functions.php
function my_admin_scripts() {
wp_enqueue_media();
wp_register_script( 'wina_classic-uadmin-js', get_template_directory_uri() . '/js/uploader.js', array('jquery','media-upload','thickbox'), '20130115', true );
wp_enqueue_script( 'wina_classic-uadmin-js');
}
add_action('admin_print_scripts', 'my_admin_scripts');
then voila..
/*-------------------------------------------------------------------
Add Custom metabox for woocommerce Category page
---------------------------------------------------------------------*/
function product_cat_add_video_field_rj() {
?>
<div class="form-field">
<label for="term_meta[video_link]"><?php _e( 'Video Link', 'flatsome' ); ?></label>
<input type="text" name="term_meta[video_link]" id="term_meta[video_link]" value="">
<p class="description"><?php _e( 'Enter a Video Link','flatsome' ); ?></p>
</div>
<?php
}
function product_cat_edit_video_field_rj($term) {
$t_id = $term->term_id;
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[video_link]"><?php _e( 'Video Link', 'flatsome' ); ?></label></th>
<td>
<input type="text" name="term_meta[video_link]" id="meta-woo" value="<?php echo esc_attr( $term_meta['video_link'] ) ? esc_attr( $term_meta['video_link'] ) : ''; ?>" style="margin-left: 0px; margin-right: 0px; width: 50%;" />
<input type="button" class="button button-secondary" value="Upload Image" id="upload-button-woo" />
<p class="description"><?php _e( 'Enter a Video Link','flatsome' ); ?></p>
</td>
</tr>
<?php
}
// this action use for add field in add form of taxonomy
add_action( 'product_cat_add_form_fields', 'product_cat_add_video_field_rj', 10, 2 );
// this action use for add field in edit form of taxonomy
add_action( 'product_cat_edit_form_fields', 'product_cat_edit_video_field_rj', 10, 2 );
function product_cat_video_link_save( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
update_option( "taxonomy_$t_id", $term_meta );
}
}
// this action use for save field value of edit form of taxonomy
add_action( 'edited_product_cat', 'product_cat_video_link_save', 10, 2 );
// this action use for save field value of add form of taxonomy
add_action( 'create_product_cat', 'product_cat_video_link_save', 10, 2 );
PS: This code does not store or reproduce the saved data in the text field.