dimanche 24 janvier 2021

woocommerce creating checkbox with multiple choises doesnt work [duplicate]

The following code uses a custom function woocommerce_wp_multi_checkbox() that I took from Multi checkbox fields in Woocommerce backend answer code.

It Should work just fine, because I already used that before and it worked without problems.

Here is my code attempt:

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

My code wasn't working properly as I got this error:

Fatal error: Uncaught Error: Call to undefined function woocommerce_wp_multi_checkbox() in /home/wordcamp16870/www/vladi/wp-content/themes/astra-child/functions.php:609 Stack trace:
#0 /home/wordcamp16870/www/vladi/wp-includes/class-wp-hook.php(287): add_custom_settings_fields('') 
#1 /home/wordcamp16870/www/vladi/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters('', Array) 
#2 /home/wordcamp16870/www/vladi/wp-includes/plugin.php(484): WP_Hook->do_action(Array) 
#3 /home/wordcamp16870/www/vladi/wp-content/plugins/woocommerce/includes/admin/meta-boxes/views/html-product-data-general.php(186): do_action('woocommerce_pro...') 
#4 /home/wordcamp16870/www/vladi/wp-content/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-product-data.php(42): include('/home/wordcamp1...') 
#5 /home/wordcamp16870/www/vladi/wp-content/plugins/woocommerce/includes/admin/meta-boxes/views/html-product-data-panel.php(51): WC_Meta_Box_Product_Data::output_tabs() 
#6 /home/wordcamp16870/www/vladi/wp-content/plugins/woocommerce/include in /home/wordcamp16870/www/vladi/wp-content/themes/astra-child/functions.php on line 609

Then I added missing woocommerce_wp_multi_checkbox() function code from the linked answer thread.

But now I get an error for in_array() where 2nd argument seems to be missing (see screenshot below):

enter image description here

I am student and I have this issues for first time.




Aucun commentaire:

Enregistrer un commentaire