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
}
}
?>
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, soin_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.