我的任务是将客户字段添加到添加新产品供应商选项。我成功将此字段添加到前端 tmpl-add-product-popup.php 代码:
<!-- Novo polje: Kolicina jednog pakovanja*/ -->
<div class="dokan-form-group">
<div> Količina jednog pakovanja (neobavezno)</div>
<div style = "color:gray;font-size:10px;"> L - litar, ml - Mililitar, Kg - Kilogram, g - Gram</div>
<div class="dokan-input-group">
<input class="dokan-form-control" name="_new_field" id="_new_field" type="text" placeholder="Primjer: 1L ili 500g" value="<?php echo dokan_posted_input( '_new_field' ); ?>">
</div>
</div>
并修改了functions.php:
add_action( ‘woocommerce_product_options_general_product_data’, ‘x_add_fields’ );
function x_add_fields() {
global $woocommerce, $post;
echo ‘<div class=”options_group”>’;
// Number Field
woocommerce_wp_text_input(
array(
‘id’ => ‘_new_field’,
‘label’ => __( ‘New Field’, ‘woocommerce’ ),
‘placeholder’ => ”,
‘description’ => __( ‘Primjer= 1L, 500ml, 1Kg’, ‘woocommerce’ ),
‘type’ => ‘number’,
‘custom_attributes’ => array(
‘step’ => ‘any’,
‘min’ => ‘0’
)
)
);
echo ‘</div>’;
}
// Save Fields
add_action( ‘woocommerce_process_product_meta’, ‘x_add_fields_save’ );
add_action( ‘dokan_process_product_meta’, ‘x_add_fields_save’ );
add_action( ‘dokan_new_product_added’, ‘x_add_fields_save’ );
function x_add_fields_save( $post_id ){
// Number Field
$woocommerce_new_field = $_POST[‘_new_field’];
if( !empty( $woocommerce_new_field) )
update_post_meta( $post_id, ‘_new_field’, esc_attr( $woocommerce_new_field ) );
}
当我尝试发布新产品时,不会为产品保存这个新字段。我在这里想念什么?
谢谢,德扬