Asked  7 Months ago    Answers:  5   Viewed   29 times

Ive been trying to add a custom field in woocommerce backend where users can multiselect checkboxes for a certain level.

Is it possible to create multiple checkboxes? So far i have this:

woocommerce_wp_checkbox(
    array(
        'id' => '_custom_product_niveau_field',
        'type' => 'checkbox',
        'label' => __('Niveau', 'woocommerce'),
        'options' => array(
            'MBO'   => __( 'MBO', 'woocommerce' ),
            'HBO'   => __( 'HBO', 'woocommerce' ),
            'WO' => __( 'WO', 'woocommerce' )
        )
    )

But that doesnt work... Does woocommerce_wp_checkbox have support for this?

 Answers

31

2021 Update

2021 Update - Solved issues with:
in_array() where 2nd argument was a string on start*.
$thepostid as in some cases it was empty.

That is possible creating a custom function this way:

// New Multi Checkbox field for woocommerce backend
function woocommerce_wp_multi_checkbox( $field ) {
    global $thepostid, $post;

    if( ! $thepostid ) {
        $thepostid = $post->ID;
    }

    $field['value'] = get_post_meta( $thepostid, $field['id'], true );

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

    echo '<fieldset class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '">
    <legend>' . wp_kses_post( $field['label'] ) . '</legend>';

    if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) {
        echo wc_help_tip( $field['description'] );
    }

    echo '<ul class="wc-radios">';

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

        echo '<li><label><input
                name="' . esc_attr( $field['name'] ) . '"
                value="' . esc_attr( $key ) . '"
                type="checkbox"
                class="' . esc_attr( $field['class'] ) . '"
                style="' . esc_attr( $field['style'] ) . '"
                ' . ( is_array( $field['value'] ) && in_array( $key, $field['value'] ) ? 'checked="checked"' : '' ) . ' /> ' . esc_html( $value ) . '</label>
        </li>';
    }
    echo '</ul>';

    if ( ! empty( $field['description'] ) && false === $field['desc_tip'] ) {
        echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
    }

    echo '</fieldset>';
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Related: Multi Select fields in Woocommerce backend


Usage example (for a simple product):

// Add custom multi-checkbox field for product general option settings
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_settings_fields', 20 );
function add_custom_settings_fields() {
    global $post;

    echo '<div class="options_group hide_if_variable"">'; // Hidding in variable products

    woocommerce_wp_multi_checkbox( array(
        'id'    => '_custom_level',
        'name'  => '_custom_level[]',
        'label' => __('Levels', 'woocommerce'),
        'options' => array(
            'MBO'   => __( 'MBO', 'woocommerce' ),
            'HBO'   => __( 'HBO', 'woocommerce' ),
            'WO'    => __( 'WO', 'woocommerce' )
        )
    ) );

    echo '</div>';
}

// Save custom multi-checkbox fields to database when submitted in Backend (for all other product types)
add_action( 'woocommerce_process_product_meta', 'save_product_options_custom_fields', 30, 1 );
function save_product_options_custom_fields( $post_id ){
    if( isset( $_POST['_custom_level'] ) ){
        $post_data = $_POST['_custom_level'];
        // Data sanitization
        $sanitize_data = array();
        if( is_array($post_data) && sizeof($post_data) > 0 ){
            foreach( $post_data as $value ){
                $sanitize_data[] = esc_attr( $value );
            }
        }
        update_post_meta( $post_id, '_custom_level', $sanitize_data );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

enter image description here

The selected values are correctly saved and displayed. For info the value is an array.

Wednesday, March 31, 2021
 
RompelStompel
answered 7 Months ago
97

Updated:

I have revisited a bit your code, simplifying things, removing unnecessary code. I have added the necessary code to make this "Engraving field" to be displayed on single product pages only when the checkbox setting option has been checked.

For your last question check at the end (for a product category without checkbox settings).

Here is the code:

// Display Fields
add_action('woocommerce_product_options_general_product_data', 'product_custom_fields_add');
function product_custom_fields_add(){
    global $post;

    echo '<div class="product_custom_field">';

    // Custom Product Checkbox Field
    woocommerce_wp_checkbox( array(
        'id'        => '_engrave_text_option',
        'desc'      => __('set custom Engrave text field', 'woocommerce'),
        'label'     => __('Display custom Engrave text field', 'woocommerce'),
        'desc_tip'  => 'true'
    ));

    echo '</div>';
}

// Save Fields
add_action('woocommerce_process_product_meta', 'product_custom_fields_save');
function product_custom_fields_save($post_id){
    // Custom Product Text Field
    $engrave_text_option = isset( $_POST['_engrave_text_option'] ) ? 'yes' : 'no';
        update_post_meta($post_id, '_engrave_text_option', esc_attr( $engrave_text_option ));
}

add_action( 'woocommerce_before_add_to_cart_button', 'add_engrave_text_field', 0 );
function add_engrave_text_field() {
    global $post;

    // If is single product page and have the "engrave text option" enabled we display the field
    if ( is_product() && get_post_meta( $post->ID, '_engrave_text_option', true ) == 'yes' ) {

        ?>
        <div>
            <label class="product-custom-text-label" for="engrave_text"><?php _e( 'Engraving option:', 'woocommerce'); ?><br>
                <input style="min-width:220px" type="text" class="product-counter" name="engrave_text" placeholder="<?php _e( 'Enter Your Custom Letters ...', 'woocommerce'); ?>" maxlength="3" />
            </label>
        </div><br>
        <?php
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works.

Checkbox output and save: Adding programatically data option tab to admin product pages Metabox

You will get something like this when the checkbox option has been selected in the product settings:

enter image description here


Make it work for a product category or a product tag (without settings checkbox):

add_action( 'woocommerce_before_add_to_cart_button', 'add_engrave_text_field', 0 );
function add_engrave_text_field() {
    global $post;

    // HERE set the term Ids, slug or name (or an array of values)
    $categories = array( 'clothing', 'music' );

    $taxonomy = 'product_cat'; // (For product tags use 'product_tag' instead)

    // If is single product page and have the "engrave text option" enabled we display the field
    if ( is_product() && has_term( $categories, 'product_cat', $post->ID ) ) {

        ?>
        <div>
            <label class="product-custom-text-label" for="engrave_text"><?php _e( 'Engraving option:', 'woocommerce'); ?><br>
                <input style="min-width:220px" type="text" class="product-counter" name="engrave_text" placeholder="<?php _e( 'Enter Your Custom Letters ...', 'woocommerce'); ?>" maxlength="3" />
            </label>
        </div><br>
        <?php
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works.

Wednesday, March 31, 2021
 
huhushow
answered 7 Months ago
17

Untested, but should give you an idea of what you should do.

ANSWER REVISED!

$markers = str_repeat('?, ', count($_POST['check_list']) - 1) . '?';

$query = $conn->prepare('SELECT * FROM `people` WHERE `p_location` IN (' . $markers . ')');
$query->execute($_POST['check_list']);
Wednesday, March 31, 2021
 
Eugenie
answered 7 Months ago
55

The answer is very simple… you just forgot to add a key id for your array in the first function, like:

$product_type_options['evisa'] = array( // … …

So in your code:

add_filter( 'product_type_options', 'add_e_visa_product_option' );
function add_e_visa_product_option( $product_type_options ) {
    $product_type_options['evisa'] = array(
        'id'            => '_evisa',
        'wrapper_class' => 'show_if_simple show_if_variable',
        'label'         => __( 'eVisa', 'woocommerce' ),
        'description'   => __( '', 'woocommerce' ),
        'default'       => 'no'
    );

    return $product_type_options;
}

add_action( 'woocommerce_process_product_meta_simple', 'save_evisa_option_fields'  );
add_action( 'woocommerce_process_product_meta_variable', 'save_evisa_option_fields'  );
function save_evisa_option_fields( $post_id ) {
    $is_e_visa = isset( $_POST['_evisa'] ) ? 'yes' : 'no';
    update_post_meta( $post_id, '_evisa', $is_e_visa );
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

enter image description here

Saturday, May 29, 2021
 
Fernando
answered 5 Months ago
45

Figured out a work around. Since the SalesFlash image was the one being triggered, I just used a blank PNG image to overlay ontop of the product image. Turned all my products into sale items and it works. Not perfect, but I don't need the sale icon anyway.

But if anyone does know of a proper programming solution, I would change it. Thanks.

Saturday, May 29, 2021
 
JakeGR
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :