Try this way:
public function __construct(
.....
\Magento\Framework\App\Action\Context $context
\Magento\Catalog\Api\ProductRepositoryInterface $productRepositoryInterface,
\Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection
) {
......
$this->productRepository = $productRepositoryInterface;
$this->productCollection = $productCollection;
parent::__construct($context);
}
public function execute(){
$items = $this->productCollection->getItems();
foreach($items as $item){
$productId = $item->getEntityId();
$product = $this->productRepository->getById($productId);
$product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
$product->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
$this->productRepository->save($product);
}
echo 'finish';
}
Visibility Type:
VISIBILITY_NOT_VISIBLE => 1
VISIBILITY_IN_CATALOG => 2
VISIBILITY_IN_SEARCH => 3
VISIBILITY_BOTH => 4