php - WooCommerce product category archive, add additional details -
hi code below adds field in woocommerce product categories section, need more fields added besides one. need change in code below add more fields 1 please.
thank in advance.
// add term page add_action( 'product_cat_add_form_fields', 'wpm_taxonomy_add_new_meta_field', 10, 2 ); function wpm_taxonomy_add_new_meta_field() { // add custom meta field add new term page ?> <div class="form-field"> <label for="term_meta[custom_term_meta]"><?php _e( 'details', 'wpm' ); ?></label> <textarea name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" rows="5" cols="40"></textarea> <p class="description"><?php _e( 'detailed category info appear below product list','wpm' ); ?></p> </div> <?php } // edit term page add_action( 'product_cat_edit_form_fields', 'wpm_taxonomy_edit_meta_field', 10, 2 ); function wpm_taxonomy_edit_meta_field($term) { // put term id variable $t_id = $term->term_id; // retrieve existing value(s) meta field. returns array $term_meta = get_option( "taxonomy_$t_id" ); $content = $term_meta['custom_term_meta'] ? wp_kses_post( $term_meta['custom_term_meta'] ) : ''; $settings = array( 'textarea_name' => 'term_meta[custom_term_meta]' ); ?> <tr class="form-field"> <th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'details', 'wpm' ); ?></label></th> <td> <?php wp_editor( $content, 'product_cat_details', $settings ); ?> <p class="description"><?php _e( 'detailed category info appear below products list','wpm' ); ?></p> </td> </tr> <?php } // save taxonomy fields callback function add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 ); add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 ); function save_taxonomy_custom_meta( $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 $key ) { if ( isset ( $_post['term_meta'][$key] ) ) { $term_meta[$key] = wp_kses_post( stripslashes($_post['term_meta'][$key]) ); } } // save option array. update_option( "taxonomy_$t_id", $term_meta ); } } // display details on product category archive pages add_action( 'woocommerce_after_subcategory_title', 'wpm_product_cat_archive_add_meta' ); function wpm_product_cat_archive_add_meta() { $t_id = get_queried_object()->term_id; $term_meta = get_option( "taxonomy_$t_id" ); $term_meta_content = $term_meta['custom_term_meta']; if ( $term_meta_content != '' ) { echo '<div class="woo-sc-box normal rounded full">'; echo apply_filters( 'the_content', $term_meta_content ); echo '</div>'; } }
for adding more field have add more form field in function , save in save function.look below function there created fieldproduct_cat_thumbnailbig_id
creating custom image upload option in category section. here detais section working. follow code , create many field want.
// add term page add_action( 'product_cat_add_form_fields', 'wpm_taxonomy_add_new_meta_field', 10, 2 ); function wpm_taxonomy_add_new_meta_field() { // add custom meta field add new term page ?> <div class="form-field"> <label for="term_meta[custom_term_meta]"><?php _e( 'details', 'wpm' ); ?></label> <textarea name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" rows="5" cols="40"></textarea> <p class="description"><?php _e( 'detailed category info appear below product list','wpm' ); ?></p> </div> <div class="form-field"> <label><?php _e( 'thumbnail', 'woocommerce' ); ?></label> <div id="product_cat_thumbnailbig" style="float: left; margin-right: 10px;"><img src="<?php echo esc_url( wc_placeholder_img_src() ); ?>" width="60px" height="60px" /></div> <div style="line-height: 60px;"> <input type="hidden" id="product_cat_thumbnailbig_id" name="product_cat_thumbnailbig_id" /> <button type="button" class="upload_image_buttonbig button"><?php _e( 'upload/add image', 'woocommerce' ); ?></button> <button type="button" class="remove_image_buttonbig button"><?php _e( 'remove image', 'woocommerce' ); ?></button> </div> <script type="text/javascript"> // show "remove image" button when needed if ( ! jquery( '#product_cat_thumbnailbig_id' ).val() ) { jquery( '.remove_image_buttonbig' ).hide(); } // uploading files var file_frame; jquery( document ).on( 'click', '.upload_image_buttonbig', function( event ) { event.preventdefault(); // if media frame exists, reopen it. if ( file_frame ) { file_frame.open(); return; } // create media frame. file_frame = wp.media.frames.downloadable_file = wp.media({ title: '<?php _e( "choose image", "woocommerce" ); ?>', button: { text: '<?php _e( "use image", "woocommerce" ); ?>' }, multiple: false }); // when image selected, run callback. file_frame.on( 'select', function() { var attachment = file_frame.state().get( 'selection' ).first().tojson(); jquery( '#product_cat_thumbnailbig_id' ).val( attachment.id ); jquery( '#product_cat_thumbnailbig img' ).attr( 'src', attachment.sizes.thumbnail.url ); jquery( '.remove_image_buttonbig' ).show(); }); // finally, open modal. file_frame.open(); }); jquery( document ).on( 'click', '.remove_image_buttonbig', function() { jquery( '#product_cat_thumbnailbig img' ).attr( 'src', '<?php echo esc_js( wc_placeholder_img_src() ); ?>' ); jquery( '#product_cat_thumbnailbig_id' ).val( '' ); jquery( '.remove_image_buttonbig' ).hide(); return false; }); </script> <div class="clear"></div> </div> <?php } // edit term page add_action( 'product_cat_edit_form_fields', 'wpm_taxonomy_edit_meta_field', 10, 2 ); function wpm_taxonomy_edit_meta_field($term) { $thumbnail_idb = absint( get_woocommerce_term_meta( $term->term_id, 'thumbnail_idb', true ) ); if ( $thumbnail_idb ) { $imageb = wp_get_attachment_thumb_url( $thumbnail_idb ); } else { $imageb = wc_placeholder_img_src(); } // put term id variable $t_id = $term->term_id; // retrieve existing value(s) meta field. returns array $term_meta = get_option( "taxonomy_$t_id" ); $content = $term_meta['custom_term_meta'] ? wp_kses_post($term_meta['custom_term_meta'] ) : ''; $settings = array( 'textarea_name' => 'term_meta[custom_term_meta]' ); ?> <tr class="form-field"> <th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'details', 'wpm' ); ?></label></th> <td> <?php wp_editor( $content, 'product_cat_details', $settings ); ?> <p class="description"><?php _e( 'detailed category info appear below products list','wpm' ); ?></p> </td> </tr> <tr class="form-field"> <th scope="row" valign="top"><label><?php _e( 'thumbnail', 'woocommerce' ); ?></label></th> <td> <div id="product_cat_thumbnailbig" style="float: left; margin-right: 10px;"><img src="<?php echo esc_url( $imageb ); ?>" width="60px" height="60px" /></div> <div style="line-height: 60px;"> <input type="hidden" id="product_cat_thumbnailbig_id" name="product_cat_thumbnailbig_id" value="<?php echo $thumbnail_id; ?>" /> <button type="button" class="upload_image_buttonbig button"><?php _e( 'upload/add image', 'woocommerce' ); ?></button> <button type="button" class="remove_image_buttonbig button"><?php _e( 'remove image', 'woocommerce' ); ?></button> </div> <script type="text/javascript"> // show "remove image" button when needed if ( '0' === jquery( '#product_cat_thumbnailbig_id' ).val() ) { jquery( '.remove_image_buttonbig' ).hide(); } // uploading files var file_frame; jquery( document ).on( 'click', '.upload_image_buttonbig', function( event ) { event.preventdefault(); // if media frame exists, reopen it. if ( file_frame ) { file_frame.open(); return; } // create media frame. file_frame = wp.media.frames.downloadable_file = wp.media({ title: '<?php _e( "choose image", "woocommerce" ); ?>', button: { text: '<?php _e( "use image", "woocommerce" ); ?>' }, multiple: false }); // when image selected, run callback. file_frame.on( 'select', function() { var attachment = file_frame.state().get( 'selection' ).first().tojson(); jquery( '#product_cat_thumbnailbig_id' ).val( attachment.id ); jquery( '#product_cat_thumbnailbig img' ).attr( 'src', attachment.sizes.thumbnail.url ); jquery( '.remove_image_buttonbig' ).show(); }); // finally, open modal. file_frame.open(); }); jquery( document ).on( 'click', '.remove_image_buttonbig', function() { jquery( '#product_cat_thumbnailbig img' ).attr( 'src', '<?php echo esc_js( wc_placeholder_img_src() ); ?>' ); jquery( '#product_cat_thumbnailbig_id' ).val( '' ); jquery( '.remove_image_buttonbig' ).hide(); return false; }); </script> <div class="clear"></div> </td> </tr> <?php } // save taxonomy fields callback function add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 ); add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 ); function save_taxonomy_custom_meta( $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 $key ) { if ( isset ( $_post['term_meta'][$key] ) ) { $term_meta[$key] = wp_kses_post( stripslashes($_post['term_meta'][$key]) ); } } // save option array. update_option( "taxonomy_$t_id", $term_meta ); } if ( isset( $_post['product_cat_thumbnailbig_id'] ) ) { update_woocommerce_term_meta( $term_id, 'thumbnail_idb', absint( $_post['product_cat_thumbnailbig_id'] ) ); } }
Comments
Post a Comment