在 dokan 中为 Woocommerce 产品集成自定义复选框 [英] Integrate custom checkboxes in dokan for Woocommerce product

查看:27
本文介绍了在 dokan 中为 Woocommerce 产品集成自定义复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是代码世界的新手,我正在使用带有 Dokan 插件的 Woocommerce.

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

add_action('dokan_new_product_after_product_tags','new_product_field',10);函数 new_product_field(){ ?><div class="dokan-form-group"><div><span>特拉巴霍区</span><br><label><input type="checkbox"value=首都联邦"名称=opcion1"/>首都联邦</label><br><label><input type="checkbox"value=BsAs G.B.A Norte"名称=opcion2";/>BsAs G.B.A Norte</label><br><label><input type="checkbox"值=BsAs G.B.A Oeste"名称=opcion3"/>BsAs G.B.A Oeste</label><br><label><input type="checkbox"值=BsAs G.B.A Sur"名称=opcion4"/>BsAs G.B.A Sur</label><br>

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

我只有桌子,但它什么都没有.有什么帮助吗?

解决方案

灵感来自这段代码 在 Github 上找到 (来自 @nayemDevs),您会在下面找到您的重新审视了一些附加功能的代码,这些功能应该可以处理您的一些需求(评论):

//额外复选框的字段设置函数 get_work_option_field_settings(){$text_domain = 'dokan-lite';返回数组('名称' =>'工作区','标题' =>__(Zona de trabajo", $text_domain),'字段' =>大批('1' =>__(首都联邦", $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),),);}//在新产品弹出/无弹出表单上添加额外的字段add_action( 'dokan_new_product_after_product_tags', 'add_dokan_new_product_custom_fields', 10 );函数 add_dokan_new_product_custom_fields(){$settings = get_work_option_field_settings();//加载字段设置$field_id = $settings['name'];//获取自定义字段元键echo '

<span class="field-title">'.$settings['title'] .'</span><br>';//循环字段选项设置foreach ( $settings['fields'] as $key => $label ) {echo '
<span class="field-title">'.__(Zona de trabajo", 'dokan-lite') .'</span><br>';//循环字段选项设置foreach ( $settings['fields'] as $key => $label ) {$检查=!空($值)&&in_array($key, $values) &&$post_id >0 ?'检查=检查"' : '';echo '
';}回声'</div>';}//在单个产品页面上显示值add_action('woocommerce_single_product_summary','display_single_product_extra_field_values', 13);函数 display_single_product_extra_field_values(){全球$产品;如果(空($产品)){返回;}$settings = get_work_option_field_settings();//加载字段设置$field_id = $settings['name'];//获取自定义字段元键$meta_value = get_post_meta( $product->get_id(), $field_id, true );//获取所选选项的自定义字段数组$values = [];//初始化如果(!空($meta_value)){//循环字段选项设置foreach ( $settings['fields'] as $key => $label ) {if ( in_array($key, $meta_value) ) {$values[] = $label;}}echo '<p class="'.$settings['name'] .-详细信息"><strong>'.$settings['title'] .'</strong>:<span>'.implode('</span>, <span>', $values) .'</span>.</p>';}}

代码位于活动子主题(或活动主题)的functions.php 文件中.它可以工作.

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

第一个函数处理你的字段设置、标题、字段选项......

I'm new in the code world and I'm using Woocommerce with the Dokan plugin.

I added the code below to the add product page, but I need this code to be integrate with nexts pages in Woocommerce as edit page or single product page:

add_action( 'dokan_new_product_after_product_tags','new_product_field',10 );

function new_product_field(){ ?>

     <div class="dokan-form-group">

     <div>
              <span>Zona de trabajo</span><br>
   <label><input type="checkbox" value="Capital Federal" name="opcion1" />Capital Federal</label><br>
   <label><input type="checkbox" value="BsAs G.B.A Norte" name="opcion2" />BsAs G.B.A Norte</label><br>
   <label><input type="checkbox" value="BsAs G.B.A Oeste" name="opcion3" />BsAs G.B.A Oeste</label><br>
   <label><input type="checkbox" value="BsAs G.B.A Sur" name="opcion4" />BsAs G.B.A Sur</label><br>
     
       </div>

I am wondering, how can I make to integrate the code with hooks with the rest of the page.

I only have the table, but its doesn't nothing. Any help please?

解决方案

Inspired from this code found on Github (from @nayemDevs), you will find below your revisited code with some additional functions that should handle some of your requirements (commented):

// 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>';
    }
}

Code goes in functions.php file of the active child theme (or active theme). It could work.

As I can't really test the code for real, you will need to test it and maybe make some changes to it.

The first function handle your fields settings, title, field options…

这篇关于在 dokan 中为 Woocommerce 产品集成自定义复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆