在dokan template new-product.php中添加图像上传自定义字段



我正在为多供应商网站使用dokan插件,我想在模板中添加图像上传自定义字段new-product.php,我使用CMB2插件创建了WooCommerce的图像上传自定义字段,就像这个

function themebox_metaboxes() {
// Start with an underscore to hide fields from custom fields list
$prefix = 'themebox_met_';
// Product Settings
$header_settings = new_cmb2_box( array(
'id'            => 'Extra_settings',
'title'         => esc_html__( 'Extra Settings', 'themebox' ),
'object_types'  => array( 'product'), // Post type
'context'       => 'normal',
'priority'      => 'high',
'show_names'    => true,
) );
$header_settings->add_field( array(
'name'       => esc_html__( 'Add Image Detail size 590x300 px', 'themebox' ),
'id'         => $prefix . 'img_detail',
'type'             => 'file'
) );
}

我想在模板表单new-product.php中添加这个自定义图像上传字段,并在dokan中保存表单时图像上传自定义字段更新,并在dokan中添加图像。。。。。与WooCommerce 中的特色产品图像完全相同

您可以覆盖new-product.phpnew-product-single.php文件,然后在产品上传/编辑文件上添加您的字段。要添加新字段进行产品编辑/添加模板,您必须在此模板中添加一个新字段(通过子主题覆盖(-dokan lite/templates/products/new product single.php。但是,还需要其他一些步骤才能成功保存它们。

您需要修改Dokan产品上传模板,然后必须通过覆盖该模板来添加一个额外的字段。添加输入字段后,您必须保存字段的值。在那个地方,您必须使用do_action( 'dokan_new_product_added', $product_id, $post_data );这个钩子来保存字段数据。

当您要编辑产品时,您必须使用do_action( 'dokan_product_updated', $post_id );重新保存。

谢谢:(

function save_add_product_meta($product_id, $postdata){
if ( ! dokan_is_user_seller( get_current_user_id() ) ) {
return;
}
if ( ! empty( $postdata['title'] ) ) {
update_post_meta( $product_id, 'title', $postdata['title'] );
}
if ( ! empty( $postdata['subtitle'] ) ) {
update_post_meta( $product_id, 'subtitle', $postdata['subtitle'] );
}
if ( ! empty( $postdata['subdescription'] ) ) {
update_post_meta( $product_id, 'subdescription', $postdata['subdescription'] );
}
if ( ! empty( $postdata['vidimg'] ) ) {
update_post_meta( $product_id, 'vidimg', $postdata['vidimg'] );
}
}
/*
* Showing field data on product edit page
*/
add_action('dokan_product_edit_after_product_tags','show_on_edit_page',99,8);
function show_on_edit_page($post, $post_id){
$subtitle         = get_post_meta( $post_id, 'subtitle', true );
$title         = get_post_meta( $post_id, 'title', true );
$subdesc        = get_post_meta( $post_id, 'subdescription', true );
$vidimg = get_post_meta( $post_id, 'vidimg', true );
?>

<div class="dokan-form-group">
<h6 class="auto">Ajoutez du contenu pour mettre en valeur cette oeuvre !</h6>
<input type="hidden" name="title" id="dokan-edit-product-id" value="<?php echo esc_attr( $post_id ); ?>"/>
<label for="new_field" class="form-label"><?php esc_html_e( 'Autre Titre', 'dokan-lite' ); ?></label>
<?php dokan_post_input_box( $post_id, 'title', array( 'placeholder' => __( 'product code', 'dokan-lite' ), 'value' => $title ) ); ?>
<p class="help-block">50 caractères maximum (conseillé)</p>
</div> 

<div class="dokan-form-group">
<input type="hidden" name="subtitle" id="dokan-edit-product-id" value="<?php echo esc_attr( $post_id ); ?>"/>
<label for="subtitle" class="form-label"><?php esc_html_e( 'Sous titre', 'dokan-lite' ); ?></label>
<?php dokan_post_input_box( $post_id, 'subtitle', array( 'placeholder' => __( 'product code', 'dokan-lite' ), 'value' => $subtitle ) ); ?>
<p class="help-block">80 caractères maximum (conseillé)</p>
</div>   
<div class="dokan-form-group">
<label for="subdescription" class="form-label">Paragraphe d'introduction</label>
<div class="dokan-rich-text-wrap">
<?php dokan_post_input_box( $post_id, 'subdescription',  array('placeholder' => 'ajouter une description', 'value' => $subdesc ), 'textarea' ); ?>         
</div> 
</div>
<div class="dokan-feat-image-upload">
<?php
$wrap_class        = ' dokan-hide';
$instruction_class = '';
$feat_image_id     = 0;
if (!empty($vidimg) ) {
$wrap_class        = '';
$instruction_class = ' dokan-hide';
$imaid =attachment_url_to_postid($vidimg);
}
?>
<div class="instruction-inside<?php echo esc_attr( $instruction_class ); ?>">
<input type="hidden" name="vidimg" class="dokan-feat-image-id" value="<?php echo esc_attr($vidimg ); ?>">
<i class="fa fa-cloud-upload"></i>
<a href="#" class="dokan-feat-image-btn btn btn-sm"><?php esc_html_e( 'Upload a product cover image', 'dokan-lite' ); ?></a>
</div>
<div class="image-wrap<?php echo esc_attr( $wrap_class ); ?>">
<a class="close dokan-remove-feat-image">&times;</a>
<?php if ( ! empty($vidimg) ) { ?>
<img src="<?php echo esc_url(wp_get_attachment_url($vidimg )  ); ?>" alt="">
<?php } else { ?>
<img height="" width="" src="" alt="">
<?php } ?>
</div>
</div><!-- .dokan-feat-image-upload -->
<?php
}

最新更新