【问题标题】:WooCommerce Multi Select for Single Product Field单一产品领域的 WooCommerce 多选
【发布时间】:2014-04-25 07:57:13
【问题描述】:

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

 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");

任何建议都会很棒。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

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

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

    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',
        ))
    );
    

    【讨论】:

    • @Danijel 我们如何更改此自定义字段的保存功能。我有一个位置字段,用户可以在其中选择多个位置。如何使用相同的键保存两个元值?
    • 我通过序列化$_POST['newoptions'];来做到这一点
    【解决方案2】:

    不必创建新函数。 woocommerce_wp_select 开箱即用地处理这个问题。可用的属性之一是custom_attributes,它可以接受一个数组。如果你传入一个array('multiple'=&gt;'multiple),那么它会呈现一个多选。为了序列化和处理输入,您只需在名称字段中提供name[],它就像魔术一样工作。这是一个例子-

    woocommerce_wp_select(
        array(
            'id' => '_cb_days_available',
            'name' => '_cb_days_available[]',
            'label' => __('Days Offered', 'woocommerce'),
            'description' => 'Which days of the week is this charter available?',
            'default' => get_option('cb_open_days'),
            'desc_tip' => 'true',
            'class' => 'cb-admin-multiselect',
            'options' => array(
                'Mon' => 'Monday',
                'Tue' => 'Tuesday',
                'Wed' => 'Wednesday',
                'Thu' => 'Thursday',
                'Fri' => 'Friday',
                'Sat' => 'Saturday',
                'Sun' => 'Sunday'
            ),
            'custom_attributes' => array('multiple' => 'multiple')
        )
    );
    

    【讨论】:

      猜你喜欢
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-22
      • 1970-01-01
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多