Skip to main content
added 161 characters in body
Source Link
Msquare
  • 9.3k
  • 7
  • 27
  • 68

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

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';
}

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
Source Link
amitshree
  • 7.1k
  • 12
  • 64
  • 120

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';
}