如何在管理中添加自定义字段上传Wooccommerce产品中的PDF文件



如果我需要添加简单的文本输入,我可以在woocommerce_product_options_advanced内部使用类似woocommerce_wp_text_input的函数。然后我使用woocommerce_process_product_meta,在那里我可以使用update_post_meta。这是清晰方便的!

现在-我想添加一个带有PDF上传选项的自定义字段,因为我想将PDF文件附加到产品中。

那么,有可能以类似的方式添加这样一个字段吗?

注意:这不是一个虚拟下载产品。这只是一个简单的产品

您可以添加自定义元框来上传pdf。您可以使用前端的<?php get_post_meta( get_the_ID(), 'advanced_options_pdf', true ); ?>来检索它。只需将以下内容粘贴到function.php文件中即可。

class Advanced_Options {
private $config = '{"title":"Advanced Options","prefix":"advanced_options_","domain":"advanced-options","class_name":"Advanced_Options","post-type":["product"],"context":"normal","priority":"default","fields":[{"type":"media","label":"PDF","return":"url","id":"advanced_options_pdf"}]}';
public function __construct() {
$this->config = json_decode( $this->config, true );
add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
add_action( 'admin_head', [ $this, 'admin_head' ] );
add_action( 'save_post', [ $this, 'save_post' ] );
}
public function add_meta_boxes() {
foreach ( $this->config['post-type'] as $screen ) {
add_meta_box(
sanitize_title( $this->config['title'] ),
$this->config['title'],
[ $this, 'add_meta_box_callback' ],
$screen,
$this->config['context'],
$this->config['priority']
);
}
}
public function admin_enqueue_scripts() {
global $typenow;
if ( in_array( $typenow, $this->config['post-type'] ) ) {
wp_enqueue_media();
}
}
public function admin_head() {
global $typenow;
if ( in_array( $typenow, $this->config['post-type'] ) ) {
?><script>
jQuery.noConflict();
(function($) {
$(function() {
$('body').on('click', '.rwp-media-toggle', function(e) {
e.preventDefault();
let button = $(this);
let rwpMediaUploader = null;
rwpMediaUploader = wp.media({
title: button.data('modal-title'),
button: {
text: button.data('modal-button')
},
multiple: true
}).on('select', function() {
let attachment = rwpMediaUploader.state().get('selection').first().toJSON();
button.prev().val(attachment[button.data('return')]);
}).open();
});
});
})(jQuery);
</script><?php
}
}
public function save_post( $post_id ) {
foreach ( $this->config['fields'] as $field ) {
switch ( $field['type'] ) {
default:
if ( isset( $_POST[ $field['id'] ] ) ) {
$sanitized = sanitize_text_field( $_POST[ $field['id'] ] );
update_post_meta( $post_id, $field['id'], $sanitized );
}
}
}
}
public function add_meta_box_callback() {
$this->fields_table();
}
private function fields_table() {
?><table class="form-table" role="presentation">
<tbody><?php
foreach ( $this->config['fields'] as $field ) {
?><tr>
<th scope="row"><?php $this->label( $field ); ?></th>
<td><?php $this->field( $field ); ?></td>
</tr><?php
}
?></tbody>
</table><?php
}
private function label( $field ) {
switch ( $field['type'] ) {
case 'media':
printf(
'<label class="" for="%s_button">%s</label>',
$field['id'], $field['label']
);
break;
default:
printf(
'<label class="" for="%s">%s</label>',
$field['id'], $field['label']
);
}
}
private function field( $field ) {
switch ( $field['type'] ) {
case 'media':
$this->input( $field );
$this->media_button( $field );
break;
default:
$this->input( $field );
}
}
private function input( $field ) {
if ( $field['type'] === 'media' ) {
$field['type'] = 'text';
}
printf(
'<input class="regular-text %s" id="%s" name="%s" %s type="%s" value="%s">',
isset( $field['class'] ) ? $field['class'] : '',
$field['id'], $field['id'],
isset( $field['pattern'] ) ? "pattern='{$field['pattern']}'" : '',
$field['type'],
$this->value( $field )
);
}
private function media_button( $field ) {
printf(
' <button class="button rwp-media-toggle" data-modal-button="%s" data-modal-title="%s" data-return="%s" id="%s_button" name="%s_button" type="button">%s</button>',
isset( $field['modal-button'] ) ? $field['modal-button'] : __( 'Select this file', 'advanced-options' ),
isset( $field['modal-title'] ) ? $field['modal-title'] : __( 'Choose a file', 'advanced-options' ),
$field['return'],
$field['id'], $field['id'],
isset( $field['button-text'] ) ? $field['button-text'] : __( 'Upload', 'advanced-options' )
);
}
private function value( $field ) {
global $post;
if ( metadata_exists( 'post', $post->ID, $field['id'] ) ) {
$value = get_post_meta( $post->ID, $field['id'], true );
} else if ( isset( $field['default'] ) ) {
$value = $field['default'];
} else {
return '';
}
return str_replace( 'u0027', "'", $value );
}
}
new Advanced_Options;

最新更新