0

I'm trying to create a loop through the products categories of WooCommerce in the admin panel of WordPress using the setting API but the following code output is:

Notice: Array to string conversion in C:\xampp\htdocs\wordpress2\wp-includes\general-template.php on line 3550

This is the function related to the error in general-template.php.

/**
 * Private helper function for checked, selected, and disabled.
 *
 * Compares the first two arguments and if identical marks as $type
 *
 * @since 2.8.0
 * @access private
 *
 * @param mixed  $helper  One of the values to compare
 * @param mixed  $current (true) The other value to compare if not just true
 * @param bool   $echo    Whether to echo or just return the string
 * @param string $type    The type of checked|selected|disabled we are doing
 * @return string html attribute or empty string
 */
function __checked_selected_helper( $helper, $current, $echo, $type ) {
    if ( (string) $helper === (string) $current )
        $result = " $type='$type'";
    else
        $result = '';

    if ( $echo )
        echo $result;

    return $result;
}

Line 3550 is this one: if ( (string) $helper === (string) $current )

I've read many similar questions but I can't find the error, I know that there's something wrong with the arrays of the loop. I've tried to var_dump() the variables and it seems that this code will output 2 arrays but I can't isolate the value ["name"] that I want to use. This is the output of the variable $terms

 array(2) { [0]=> object(WP_Term)#9234 (16) { ["term_id"]=> int(8) ["name"]=> string(6) "mobile" ["slug"]=> string(6) "mobile" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(8) ["taxonomy"]=> string(11) "product_cat" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(4) ["filter"]=> string(3) "raw" ["cat_ID"]=> int(8) ["category_count"]=> int(4) ["category_description"]=> string(0) "" ["cat_name"]=> string(6) "mobile" ["category_nicename"]=> string(6) "mobile" ["category_parent"]=> int(0) } [1]=> object(WP_Term)#9266 (16) { ["term_id"]=> int(9) ["name"]=> string(4) "vino" ["slug"]=> string(4) "vino" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(9) ["taxonomy"]=> string(11) "product_cat" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(1) ["filter"]=> string(3) "raw" ["cat_ID"]=> int(9) ["category_count"]=> int(1) ["category_description"]=> string(0) "" ["cat_name"]=> string(4) "vino" ["category_nicename"]=> string(4) "vino" ["category_parent"]=> int(0) } }

Thanks for any help.

<?php
function offwoo_checkbox_field_2_render() {
    $options = get_option( 'offwoo_settings' );
    global $woocommerce;

    $terms = get_categories( array(
        'taxonomy' => 'product_cat',
        'orderby'   =>'name',
        'parent'  => 0 
    ));

   var_dump($terms);

    foreach ($terms as $term) {
    ?>
    <input type='checkbox' name='offwoo_settings[offwoo_checkbox_field_2]' <?php if(isset($options['offwoo_checkbox_field_2'])) { checked( $options['offwoo_checkbox_field_2'], 1 ); } ?> value='<?php echo $term->name; ?>'>
    <label><?php echo $term->name; ?></label>
    <?php   
    }
}
?>
user6854344
  • 23
  • 1
  • 6
  • Please add `var_dump($terms);` before `foreach` and share the output with us. – EhsanT Dec 28 '16 at 01:49
  • Ok, I've added the variable informations. Thanks – user6854344 Dec 28 '16 at 10:38
  • Please take a look at [this answer](http://stackoverflow.com/a/18106696/1908331) the apply it to your code. I think it will solve your problem. If not please let me know – EhsanT Dec 28 '16 at 22:33
  • Thanks,I've tried the code you suggest but I'm still having the same error. Maybe I made a mistake in the custom loop or using `get_categories()` instead of another function. I'm still thinking of a solution. – user6854344 Dec 29 '16 at 16:13
  • Can you please edit your question and also add the actual content of the `$terms` to your post? I think your problem is with `$term->name`. What line is line 3550? – EhsanT Dec 29 '16 at 22:02
  • Question edited. I also want to add that this is a test site so, the categories that I have added to try this code area two, just to test the functionality, if I add more categories we will have `array(3)` in `var_dump($terms)`. – user6854344 Dec 30 '16 at 13:15
  • OK, now I can see where is the error: `checked( $options['offwoo_checkbox_field_2'], 1 );`. What is inside `$options['offwoo_checkbox_field_2']`? Can you also `var_dump($options['offwoo_checkbox_field_2'])` and share the result? – EhsanT Dec 30 '16 at 22:17
  • Aha, I see. `array(1) { [0]=> string(6) "mobile" }` is the output of `$options['offwoo_checkbox_field_2']`. I expect an array with all the categories that I've set. But the var_dump only print the first one. – user6854344 Dec 31 '16 at 10:35
  • the first parameter of the `checked()` function can not be an array. With your data, you can use `$options['offwoo_checkbox_field_2'][0]` – EhsanT Dec 31 '16 at 21:00

1 Answers1

0

I've found a solution to this question also thanks to this other one:

https://wordpress.stackexchange.com/questions/183007/checked-function-on-a-multidimensional-array

In this case the issue, as correctly addessed in the comments above, is that the checked() function is set in the wrong way, and can take only one parameter and not an array, so in_array() should be added in this situation. In this case the $term->name will output multiple values.The other issue is that the name checkbox parameter $options[off_woo_checkbox_field_2] is multidimentional so should be added [] in other to store the other parameters that the WooCommerce loop creates.

<input type='checkbox' name='offwoo_settings[offwoo_checkbox_field_2][]' 
<?php if(isset($options['offwoo_checkbox_field_2'])) { checked( in_array($term->name, $options['offwoo_checkbox_field_2']), true); }?> value='<?php echo $term->name; ?>'>
<label><?php echo $term->name; ?></label>
Community
  • 1
  • 1
user6854344
  • 23
  • 1
  • 6