Here is my block code
$filteredProducts = $productCollection->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', $parentID)
->addAttributeToFilter('type_id', array('eq' => "configurable"));
foreach($filteredProducts as $_product) {
$_children = $_product->getTypeInstance()->getUsedProducts($_product);
foreach ($_children as $child){
if ($child->getID()) {
$array = [
1 => 'Not Visible Individually',
2 => 'Catalog',
3 => 'Search',
4 => 'Catalog, Search'
];
$currentValue = array_search("Search",$array);
$child->setVisibility($currentValue);
$child->save();
}
echo "<pre>";
var_dump($child->debug());
}
}
This works only frontend and not apply in product grids.