在dokan中为Woocommerce产品集成自定义复选框

问题描述

我是代码领域的新手,并且正在将Woocommerce与Dokan插件一起使用。

我在添加产品页面添加了以下代码,但是我需要将此代码与Woocommerce中的下一页集成为编辑页面或单个产品页面

itens = ['car','house','moon','sun']

v = 0
for item in itens:
    b = itens[1 + v]
    print(b)
    print('any other command')
    if b == itens[-1]:
        print('End')
        break
    v += 1

我想知道如何将带有钩子的代码页面的其余部分集成在一起。

我只有桌子,但它什么也不是。有什么帮助吗?

解决方法

灵感来自Github (来自this code上的@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",'2'  => __("BsAs G.B.A Norte",'3'  => __("BsAs G.B.A Oeste",'4'  => __("BsAs G.B.A Sur",),);
}

// 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',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(),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文件中。可以。

由于我无法真正地测试代码,因此您需要对其进行测试,并可能对其进行一些更改。

第一个功能处理您的字段设置,标题,字段选项...