受Github 上的这段代码的启发(来自@nayemDevs),您将在下面找到重新访问的代码,其中包含一些应该处理您的一些要求的附加功能(已注释):
// Field Settings for Extra checkboxes
function get_work_option_field_settings(){
$text_domain = 'dokan-lite';
return array(
'name' => 'work_zone',
'title' => __("Zona de trabajo", $text_domain),
'fields' => array(
'1' => __("Capital Federal", $text_domain),
'2' => __("BsAs G.B.A Norte", $text_domain),
'3' => __("BsAs G.B.A Oeste", $text_domain),
'4' => __("BsAs G.B.A Sur", $text_domain),
),
);
}
// Adding extra field on New product popup/without popup form
add_action( 'dokan_new_product_after_product_tags', 'add_dokan_new_product_custom_fields', 10 );
function add_dokan_new_product_custom_fields(){
$settings = get_work_option_field_settings(); // Load field settings
$field_id = $settings['name']; // Get custom field metakey
echo '<div class="dokan-form-group">
<span class="field-title">' . $settings['title'] . '</span><br>';
// Loop through field option settings
foreach ( $settings['fields'] as $key => $label ) {
echo '<label><input type="checkbox" class="dokan-form-control" name="'.$field_id.$key.'" value="'.$key.'" /> '.$label.'</label><br>';
}
echo '</div>';
}
// Saving product field data for edit and update
add_action( 'dokan_new_product_added','save_dokan_product_custom_fields', 10, 2 );
add_action( 'dokan_product_updated', 'save_dokan_product_custom_fields', 10, 2 );
function save_dokan_product_custom_fields( $product_id, $postdata = null ){
if ( ! dokan_is_user_seller( get_current_user_id() ) ) {
return;
}
$settings = get_work_option_field_settings(); // Load field settings
$field_id = $settings['name']; // Get custom field metakey
$meta_value = array(); // Initializing
// Loop through field option settings
foreach ( $settings['fields'] as $key => $label ) {
if ( ! empty( $postdata[$field_id.$key] ) ) {
// add each selected option to the array
$meta_value[] = esc_attr( $postdata[$field_id.$key] );
}
}
update_post_meta( $product_id, $field_key, $meta_value ); // Save
}
// Showing field data on product edit page
add_action( 'dokan_product_edit_after_product_tags','add_dokan_edit_product_custom_fields', 99, 2 );
function add_dokan_edit_product_custom_fields( $post, $post_id ){
$settings = get_work_option_field_settings(); // Load field settings
$field_id = $settings['name']; // Get custom field metakey
$values = get_post_meta( $post_id, $field_id, true ); // Get custom field array of selected options
echo '<div class="dokan-form-group">
<span class="field-title">' . __("Zona de trabajo", 'dokan-lite') . '</span><br>';
// Loop through field option settings
foreach ( $settings['fields'] as $key => $label ) {
$checked = ! empty($values) && in_array($key, $values) && $post_id > 0 ? 'checked="checked" ' : '';
echo '<label><input type="checkbox" class="dokan-form-control" name="'.$field_id.$key.'" value="'.$key.'" '.$checked.'/>'.$label.'</label><br>';
}
echo '</div>';
}
// Display values on single product page
add_action( 'woocommerce_single_product_summary','display_single_product_extra_field_values', 13 );
function display_single_product_extra_field_values(){
global $product;
if ( empty( $product ) ) {
return;
}
$settings = get_work_option_field_settings(); // Load field settings
$field_id = $settings['name']; // Get custom field metakey
$meta_value = get_post_meta( $product->get_id(), $field_id, true ); // Get custom field array of selected options
$values = []; // Initializing
if ( ! empty( $meta_value ) ) {
// Loop through field option settings
foreach ( $settings['fields'] as $key => $label ) {
if ( in_array($key, $meta_value) ) {
$values[] = $label;
}
}
echo '<p class="' . $settings['name'] . '-details"><strong>' . $settings['title'] . '</strong>: <span>'. implode('</span>, <span>', $values) . '</span>.</p>';
}
}
代码位于活动子主题(或活动主题)的 functions.php 文件中。它可以工作。
由于我无法真正测试代码,因此您需要对其进行测试并可能对其进行一些更改。
第一个函数处理您的字段设置、标题、字段选项……</p>