Invalid argument supplied for foreach() in Swatches.php
-
Hi,
When we have a lot of swatches on products we get the invalid argument foreach() in swatches.php.
Any ideas why is this and how to fix it?
Ive managed to fix it temporarely by adding “if(is_array($available_variations)){” before foreach() on line 125 in Swatches.php
Hi,
Strange, but we can’t reproduce the problem on our side. Could you please send us your FTP access and disable all external plugins so we can check what is wrong there?
Regards
Try adding 50+ swatch variations.
Please note, that for now, the invalid argument is not present because ive modified the swatches.php file
See ftp and site logins below in private content:
Also, why is it that if i put swatches.php to my child theme in the same folder it does not work?
As of now ive modified swatches.php in woodmart theme core folder
Try to add the following PHP code snippet to the child theme functions.php
function woodmart_has_swatches( $id, $attr_name, $options, $available_variations, $swatches_use_variation_images = false ) {
$swatches = array();
foreach ($options as $key => $value) {
$swatch = woodmart_has_swatch($id, $attr_name, $value);
if( ! empty( $swatch ) ) {
if( $available_variations && $swatches_use_variation_images && woodmart_get_opt( 'grid_swatches_attribute' ) == $attr_name ) {
$variation = woodmart_get_option_variations( $attr_name, $available_variations, $value );
$swatch = array_merge( $swatch, $variation);
}
$swatches[$key] = $swatch;
}
}
return $swatches;
}