WooCommerce产品类别存档,添加其他详细信息

时间:2015-05-14 19:23:35

标签: php wordpress woocommerce taxonomy hook-woocommerce

以下代码在WooCommerce产品类别部分添加了一个额外的字段,除此之外我还需要一些额外的字段。我需要在下面的代码中更改以添加更多类似的字段。

提前感谢您的帮助。

// 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() {
  // this will add the custom meta field to the 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 to appear below the 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 the term ID into a variable
  $t_id = $term->term_id;

  // retrieve the existing value(s) for this meta field. This returns an 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 to appear below the products list','wpm' ); ?></p>
    </td>
  </tr>
<?php
}

// Save extra 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 as $key ) {
      if ( isset ( $_POST['term_meta'][$key] ) ) {
        $term_meta[$key] = wp_kses_post( stripslashes($_POST['term_meta'][$key]) );
      }
    }
    // Save the 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>';
  }
}

2 个答案:

答案 0 :(得分:0)

要添加更多字段,您必须在函数中添加更多表单字段,然后将其保存在保存功能中。在我的下面的函数中创建了一个额外的字段product_cat_thumbnailbig_id,用于在类别部分创建自定义图像上传选项。您的 detais 部分也在这里工作。按照我的代码创建所需的字段。

// 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() {
  // this will add the custom meta field to the 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 to appear below the 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">

            // Only show the "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 the media frame already exists, reopen it.
                if ( file_frame ) {
                    file_frame.open();
                    return;
                }

                // Create the media frame.
                file_frame = wp.media.frames.downloadable_file = wp.media({
                    title: '<?php _e( "Choose an image", "woocommerce" ); ?>',
                    button: {
                        text: '<?php _e( "Use image", "woocommerce" ); ?>'
                    },
                    multiple: false
                });

                // When an image is selected, run a 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 the 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 the term ID into a variable
   $t_id = $term->term_id;

   // retrieve the existing value(s) for this meta field. This returns an 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 to appear below the 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">

                // Only show the "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 the media frame already exists, reopen it.
                    if ( file_frame ) {
                        file_frame.open();
                        return;
                    }

                    // Create the media frame.
                    file_frame = wp.media.frames.downloadable_file = wp.media({
                        title: '<?php _e( "Choose an image", "woocommerce" ); ?>',
                        button: {
                            text: '<?php _e( "Use image", "woocommerce" ); ?>'
                        },
                        multiple: false
                    });

                    // When an image is selected, run a 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 the 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 extra 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 as $key ) {
  if ( isset ( $_POST['term_meta'][$key] ) ) {
    $term_meta[$key] = wp_kses_post( stripslashes($_POST['term_meta'][$key]) );
  }
}
// Save the 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'] ) );
    }
  }

答案 1 :(得分:0)

  

您可以在单个函数中添加多个字段,如下所示。

这是代码

//Product Cat creation page
function wpm_taxonomy_add_new_meta_field() {
    ?>
    <div class="form-field">
        <label for="term_meta[wh_meta_title]"><?php _e('Meta Title', 'text_domain'); ?></label>
        <input type="text" name="term_meta[wh_meta_title]" id="term_meta[wh_meta_title]">
        <p class="description"><?php _e('Enter a meta title, <= 60 character', 'text_domain'); ?></p>
    </div>
    <div class="form-field">
        <label for="term_meta[wh_meta_desc]"><?php _e('Meta Description', 'text_domain'); ?></label>
        <textarea name="term_meta[wh_meta_desc]" id="term_meta[wh_meta_desc]"></textarea>
        <p class="description"><?php _e('Enter a meta description, <= 160 character', 'text_domain'); ?></p>
    </div>
    <?php
}

add_action('product_cat_add_form_fields', 'wpm_taxonomy_add_new_meta_field', 10, 2);

参考:

  1. 详细教程:Adding Custom Fields to WooCommerce Product Category
  2. 相关答案:Adding custom field to product category in WooCommerce
  3. 希望这有帮助。