单个产品字段的 WooCommerce 多选 [英] WooCommerce Multi Select for Single Product Field

查看:33
本文介绍了单个产品字段的 WooCommerce 多选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在单个产品页面上添加列表框,我想知道 woocommerce 中没有多选选项,对于 get_option 字段 woocommerce 支持多选,但对于 post_metawoocommerce 仅支持单选,不确定 woocommerce 是否有任何限制,否则我可能会错过一些获得多选的东西?这是我尝试过的以下代码

I am trying to add the list box on the single product page, I was wondering no option for multiselect in woocommerce, For get_option fields woocommerce supporting multiselect but for post_meta woocommerce support only single select, Not sure is there any limitation in woocommerce or i could miss something to get multiselect? Here is the below code i tried

 function create_multiselect() { 

    woocommerce_wp_select(array(
                'id' => 'newoptions',
                'class' => 'newoptions',
                'label' => __('Testing Multiple Select', 'woocommerce'),
                'options' => array(
                    '1' => 'User1',
                    '2' => 'User2',
                    '3' => 'User3',
                ))
            );

    }

    add_action("woocommerce_product_options_pricing","create_multiselect");

任何建议都会很棒.

推荐答案

woocommerce_wp_select()函数不支持多选,可以打开wc-meta-box-functions.php</woocommerce/includes/admin 目录中的/code> 文件以查看默认行为.

woocommerce_wp_select() function does not support multiselect, you can open the wc-meta-box-functions.php file in /woocommerce/includes/admin directory to see the default behavior.

但是是什么阻止您创建自己的函数,该函数将基于默认的 Woo 函数,并添加所需的功能,甚至更改默认函数(但是,修改插件文件仍然不是最佳实践).这是一个如何编写具有多重支持的新函数的示例,与原始函数的唯一变化是添加了对名称和多个属性的支持,以及对所选项目的不同逻辑(因为 post meta 现在是一个数组).

But what's stopping you to create your own function that will be based on the default Woo function, and add the required features, or even change the default function ( but still, modifying the plugin files is not the best practice ). Here's an example of how to write a new function with multiple support, the only changes from original are added support for name and multiple attributes, and different logic for the selected items ( since post meta is now an array ).

function woocommerce_wp_select_multiple( $field ) {
    global $thepostid, $post, $woocommerce;

    $thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
    $field['class']         = isset( $field['class'] ) ? $field['class'] : 'select short';
    $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
    $field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
    $field['value']         = isset( $field['value'] ) ? $field['value'] : ( get_post_meta( $thepostid, $field['id'], true ) ? get_post_meta( $thepostid, $field['id'], true ) : array() );

    echo '<p class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label><select id="' . esc_attr( $field['id'] ) . '" name="' . esc_attr( $field['name'] ) . '" class="' . esc_attr( $field['class'] ) . '" multiple="multiple">';

    foreach ( $field['options'] as $key => $value ) {

        echo '<option value="' . esc_attr( $key ) . '" ' . ( in_array( $key, $field['value'] ) ? 'selected="selected"' : '' ) . '>' . esc_html( $value ) . '</option>';

    }

    echo '</select> ';

    if ( ! empty( $field['description'] ) ) {

        if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) {
            echo '<img class="help_tip" data-tip="' . esc_attr( $field['description'] ) . '" src="' . esc_url( WC()->plugin_url() ) . '/assets/images/help.png" height="16" width="16" />';
        } else {
            echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
        }

    }
    echo '</p>';
}

函数使用示例:

woocommerce_wp_select_multiple( array(
    'id' => 'newoptions',
    'name' => 'newoptions[]',
    'class' => 'newoptions',
    'label' => __('Testing Multiple Select', 'woocommerce'),
    'options' => array(
        '1' => 'User1',
        '2' => 'User2',
        '3' => 'User3',
    ))
);

这篇关于单个产品字段的 WooCommerce 多选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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