Magento 2: How to set visibility and status to product using collection?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















I'm creating a cron to pass through all the products and set them visible and active.



This is my execute of the cron who runs well:



public function execute(){
/**
* @var $item MagentoCatalogModelProduct
* @var MagentoCatalogModelResourceModelProductCollection $collection
*/

$items = $this->productCollection->getItems();

foreach($items as $item){
echo $item->getSku() .'<br>';
$item->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$item->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
$item->save();
}
echo 'finish';
}


I've got a product with none visibility and status disabled, but when I run the function the product is still with none visibility and status disabled.



What I'm doing wrong?










share|improve this question
















bumped to the homepage by Community 10 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.






















    1















    I'm creating a cron to pass through all the products and set them visible and active.



    This is my execute of the cron who runs well:



    public function execute(){
    /**
    * @var $item MagentoCatalogModelProduct
    * @var MagentoCatalogModelResourceModelProductCollection $collection
    */

    $items = $this->productCollection->getItems();

    foreach($items as $item){
    echo $item->getSku() .'<br>';
    $item->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
    $item->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
    $item->save();
    }
    echo 'finish';
    }


    I've got a product with none visibility and status disabled, but when I run the function the product is still with none visibility and status disabled.



    What I'm doing wrong?










    share|improve this question
















    bumped to the homepage by Community 10 mins ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















      1












      1








      1








      I'm creating a cron to pass through all the products and set them visible and active.



      This is my execute of the cron who runs well:



      public function execute(){
      /**
      * @var $item MagentoCatalogModelProduct
      * @var MagentoCatalogModelResourceModelProductCollection $collection
      */

      $items = $this->productCollection->getItems();

      foreach($items as $item){
      echo $item->getSku() .'<br>';
      $item->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
      $item->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
      $item->save();
      }
      echo 'finish';
      }


      I've got a product with none visibility and status disabled, but when I run the function the product is still with none visibility and status disabled.



      What I'm doing wrong?










      share|improve this question
















      I'm creating a cron to pass through all the products and set them visible and active.



      This is my execute of the cron who runs well:



      public function execute(){
      /**
      * @var $item MagentoCatalogModelProduct
      * @var MagentoCatalogModelResourceModelProductCollection $collection
      */

      $items = $this->productCollection->getItems();

      foreach($items as $item){
      echo $item->getSku() .'<br>';
      $item->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
      $item->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
      $item->save();
      }
      echo 'finish';
      }


      I've got a product with none visibility and status disabled, but when I run the function the product is still with none visibility and status disabled.



      What I'm doing wrong?







      magento2 module attributes collection






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 29 '17 at 3:02









      Rafael Corrêa Gomes

      4,75523366




      4,75523366










      asked May 26 '16 at 12:55









      ntzzntzz

      4441624




      4441624





      bumped to the homepage by Community 10 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 10 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          4 Answers
          4






          active

          oldest

          votes


















          0














          You should replace the $item->save(); with below code:



          $item->getResource()->saveAttribute($item, 'visibility');

          $item->getResource()->saveAttribute($item, 'status');





          share|improve this answer

































            0














            Try to change the Collection and set the AreaCode, I've created something similar and works.



            namespace NameSpaceModuleNameConsoleCommand;

            use SymfonyComponentConsoleCommandCommand;
            use SymfonyComponentConsoleInputInputArgument;
            use SymfonyComponentConsoleInputInputInterface;
            use SymfonyComponentConsoleOutputOutputInterface;

            class EAVMassUpdateCommand extends Command
            {
            public function __construct(
            MagentoFrameworkAppState $state,
            MagentoCatalogModelResourceModelProductCollectionFactory $collectionFactory
            ) {
            $state->setAreaCode('adminhtml');
            $this->_collectionFactory = $collectionFactory;
            parent::__construct();
            }

            ...

            public function execute(){

            $items = $this->_collectionFactory->create()
            ->addAttributeToSelect('*')
            ->load();

            foreach($items as $item){
            echo $item->getSku() .'<br>';
            $item->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
            $item->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
            $item->save();
            }
            echo 'finish';
            }





            share|improve this answer

































              0














              Try this way:



              public function __construct(
              .....
              MagentoFrameworkAppActionContext $context
              MagentoCatalogApiProductRepositoryInterface $productRepositoryInterface,
              MagentoCatalogModelResourceModelProductCollection $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(MagentoCatalogModelProductAttributeSourceStatus::STATUS_DISABLED);
              $product->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
              $this->productRepository->save($product);
              }
              echo 'finish';
              }





              share|improve this answer































                0














                For getting product collection with status enable and visibility:



                you have to keep below code in block file.



                protected $_coreRegistry = null;

                public function __construct(
                MagentoFrameworkViewElementTemplateContext $context,
                MagentoFrameworkRegistry $registry,
                MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
                MagentoCatalogModelProductAttributeSourceStatus $productStatus,
                MagentoCatalogModelProductVisibility $productVisibility,
                array $data =
                )
                {

                $this->_coreRegistry = $registry;
                $this->_productCollectionFactory = $productCollectionFactory;
                $this->productStatus = $productStatus;
                $this->productVisibility = $productVisibility;
                parent::__construct($context, $data);
                }

                public function getProductCollection()
                {
                $collection = $this->_productCollectionFactory->create();
                $collection->addAttributeToFilter('manufacturer', ['eq' => '20']);
                $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
                $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());

                return $collection;
                }


                Above code working for visibility type for:




                • VISIBILITY_IN_SEARCH

                • VISIBILITY_IN_CATALOG

                • VISIBILITY_BOTH






                share|improve this answer
























                  Your Answer








                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "479"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: false,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f117112%2fmagento-2-how-to-set-visibility-and-status-to-product-using-collection%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  0














                  You should replace the $item->save(); with below code:



                  $item->getResource()->saveAttribute($item, 'visibility');

                  $item->getResource()->saveAttribute($item, 'status');





                  share|improve this answer






























                    0














                    You should replace the $item->save(); with below code:



                    $item->getResource()->saveAttribute($item, 'visibility');

                    $item->getResource()->saveAttribute($item, 'status');





                    share|improve this answer




























                      0












                      0








                      0







                      You should replace the $item->save(); with below code:



                      $item->getResource()->saveAttribute($item, 'visibility');

                      $item->getResource()->saveAttribute($item, 'status');





                      share|improve this answer















                      You should replace the $item->save(); with below code:



                      $item->getResource()->saveAttribute($item, 'visibility');

                      $item->getResource()->saveAttribute($item, 'status');






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Apr 21 '17 at 21:10









                      Rafael Corrêa Gomes

                      4,75523366




                      4,75523366










                      answered Jan 19 '17 at 9:59









                      Cuong NguyenCuong Nguyen

                      1




                      1

























                          0














                          Try to change the Collection and set the AreaCode, I've created something similar and works.



                          namespace NameSpaceModuleNameConsoleCommand;

                          use SymfonyComponentConsoleCommandCommand;
                          use SymfonyComponentConsoleInputInputArgument;
                          use SymfonyComponentConsoleInputInputInterface;
                          use SymfonyComponentConsoleOutputOutputInterface;

                          class EAVMassUpdateCommand extends Command
                          {
                          public function __construct(
                          MagentoFrameworkAppState $state,
                          MagentoCatalogModelResourceModelProductCollectionFactory $collectionFactory
                          ) {
                          $state->setAreaCode('adminhtml');
                          $this->_collectionFactory = $collectionFactory;
                          parent::__construct();
                          }

                          ...

                          public function execute(){

                          $items = $this->_collectionFactory->create()
                          ->addAttributeToSelect('*')
                          ->load();

                          foreach($items as $item){
                          echo $item->getSku() .'<br>';
                          $item->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
                          $item->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
                          $item->save();
                          }
                          echo 'finish';
                          }





                          share|improve this answer






























                            0














                            Try to change the Collection and set the AreaCode, I've created something similar and works.



                            namespace NameSpaceModuleNameConsoleCommand;

                            use SymfonyComponentConsoleCommandCommand;
                            use SymfonyComponentConsoleInputInputArgument;
                            use SymfonyComponentConsoleInputInputInterface;
                            use SymfonyComponentConsoleOutputOutputInterface;

                            class EAVMassUpdateCommand extends Command
                            {
                            public function __construct(
                            MagentoFrameworkAppState $state,
                            MagentoCatalogModelResourceModelProductCollectionFactory $collectionFactory
                            ) {
                            $state->setAreaCode('adminhtml');
                            $this->_collectionFactory = $collectionFactory;
                            parent::__construct();
                            }

                            ...

                            public function execute(){

                            $items = $this->_collectionFactory->create()
                            ->addAttributeToSelect('*')
                            ->load();

                            foreach($items as $item){
                            echo $item->getSku() .'<br>';
                            $item->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
                            $item->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
                            $item->save();
                            }
                            echo 'finish';
                            }





                            share|improve this answer




























                              0












                              0








                              0







                              Try to change the Collection and set the AreaCode, I've created something similar and works.



                              namespace NameSpaceModuleNameConsoleCommand;

                              use SymfonyComponentConsoleCommandCommand;
                              use SymfonyComponentConsoleInputInputArgument;
                              use SymfonyComponentConsoleInputInputInterface;
                              use SymfonyComponentConsoleOutputOutputInterface;

                              class EAVMassUpdateCommand extends Command
                              {
                              public function __construct(
                              MagentoFrameworkAppState $state,
                              MagentoCatalogModelResourceModelProductCollectionFactory $collectionFactory
                              ) {
                              $state->setAreaCode('adminhtml');
                              $this->_collectionFactory = $collectionFactory;
                              parent::__construct();
                              }

                              ...

                              public function execute(){

                              $items = $this->_collectionFactory->create()
                              ->addAttributeToSelect('*')
                              ->load();

                              foreach($items as $item){
                              echo $item->getSku() .'<br>';
                              $item->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
                              $item->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
                              $item->save();
                              }
                              echo 'finish';
                              }





                              share|improve this answer















                              Try to change the Collection and set the AreaCode, I've created something similar and works.



                              namespace NameSpaceModuleNameConsoleCommand;

                              use SymfonyComponentConsoleCommandCommand;
                              use SymfonyComponentConsoleInputInputArgument;
                              use SymfonyComponentConsoleInputInputInterface;
                              use SymfonyComponentConsoleOutputOutputInterface;

                              class EAVMassUpdateCommand extends Command
                              {
                              public function __construct(
                              MagentoFrameworkAppState $state,
                              MagentoCatalogModelResourceModelProductCollectionFactory $collectionFactory
                              ) {
                              $state->setAreaCode('adminhtml');
                              $this->_collectionFactory = $collectionFactory;
                              parent::__construct();
                              }

                              ...

                              public function execute(){

                              $items = $this->_collectionFactory->create()
                              ->addAttributeToSelect('*')
                              ->load();

                              foreach($items as $item){
                              echo $item->getSku() .'<br>';
                              $item->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
                              $item->setStatus(MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
                              $item->save();
                              }
                              echo 'finish';
                              }






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited May 28 '17 at 14:14

























                              answered May 28 '17 at 14:07









                              Rafael Corrêa GomesRafael Corrêa Gomes

                              4,75523366




                              4,75523366























                                  0














                                  Try this way:



                                  public function __construct(
                                  .....
                                  MagentoFrameworkAppActionContext $context
                                  MagentoCatalogApiProductRepositoryInterface $productRepositoryInterface,
                                  MagentoCatalogModelResourceModelProductCollection $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(MagentoCatalogModelProductAttributeSourceStatus::STATUS_DISABLED);
                                  $product->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
                                  $this->productRepository->save($product);
                                  }
                                  echo 'finish';
                                  }





                                  share|improve this answer




























                                    0














                                    Try this way:



                                    public function __construct(
                                    .....
                                    MagentoFrameworkAppActionContext $context
                                    MagentoCatalogApiProductRepositoryInterface $productRepositoryInterface,
                                    MagentoCatalogModelResourceModelProductCollection $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(MagentoCatalogModelProductAttributeSourceStatus::STATUS_DISABLED);
                                    $product->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
                                    $this->productRepository->save($product);
                                    }
                                    echo 'finish';
                                    }





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      Try this way:



                                      public function __construct(
                                      .....
                                      MagentoFrameworkAppActionContext $context
                                      MagentoCatalogApiProductRepositoryInterface $productRepositoryInterface,
                                      MagentoCatalogModelResourceModelProductCollection $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(MagentoCatalogModelProductAttributeSourceStatus::STATUS_DISABLED);
                                      $product->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
                                      $this->productRepository->save($product);
                                      }
                                      echo 'finish';
                                      }





                                      share|improve this answer













                                      Try this way:



                                      public function __construct(
                                      .....
                                      MagentoFrameworkAppActionContext $context
                                      MagentoCatalogApiProductRepositoryInterface $productRepositoryInterface,
                                      MagentoCatalogModelResourceModelProductCollection $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(MagentoCatalogModelProductAttributeSourceStatus::STATUS_DISABLED);
                                      $product->setVisibility(MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
                                      $this->productRepository->save($product);
                                      }
                                      echo 'finish';
                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered May 28 '17 at 14:19









                                      amitshreeamitshree

                                      3,292103983




                                      3,292103983























                                          0














                                          For getting product collection with status enable and visibility:



                                          you have to keep below code in block file.



                                          protected $_coreRegistry = null;

                                          public function __construct(
                                          MagentoFrameworkViewElementTemplateContext $context,
                                          MagentoFrameworkRegistry $registry,
                                          MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
                                          MagentoCatalogModelProductAttributeSourceStatus $productStatus,
                                          MagentoCatalogModelProductVisibility $productVisibility,
                                          array $data =
                                          )
                                          {

                                          $this->_coreRegistry = $registry;
                                          $this->_productCollectionFactory = $productCollectionFactory;
                                          $this->productStatus = $productStatus;
                                          $this->productVisibility = $productVisibility;
                                          parent::__construct($context, $data);
                                          }

                                          public function getProductCollection()
                                          {
                                          $collection = $this->_productCollectionFactory->create();
                                          $collection->addAttributeToFilter('manufacturer', ['eq' => '20']);
                                          $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
                                          $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());

                                          return $collection;
                                          }


                                          Above code working for visibility type for:




                                          • VISIBILITY_IN_SEARCH

                                          • VISIBILITY_IN_CATALOG

                                          • VISIBILITY_BOTH






                                          share|improve this answer




























                                            0














                                            For getting product collection with status enable and visibility:



                                            you have to keep below code in block file.



                                            protected $_coreRegistry = null;

                                            public function __construct(
                                            MagentoFrameworkViewElementTemplateContext $context,
                                            MagentoFrameworkRegistry $registry,
                                            MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
                                            MagentoCatalogModelProductAttributeSourceStatus $productStatus,
                                            MagentoCatalogModelProductVisibility $productVisibility,
                                            array $data =
                                            )
                                            {

                                            $this->_coreRegistry = $registry;
                                            $this->_productCollectionFactory = $productCollectionFactory;
                                            $this->productStatus = $productStatus;
                                            $this->productVisibility = $productVisibility;
                                            parent::__construct($context, $data);
                                            }

                                            public function getProductCollection()
                                            {
                                            $collection = $this->_productCollectionFactory->create();
                                            $collection->addAttributeToFilter('manufacturer', ['eq' => '20']);
                                            $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
                                            $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());

                                            return $collection;
                                            }


                                            Above code working for visibility type for:




                                            • VISIBILITY_IN_SEARCH

                                            • VISIBILITY_IN_CATALOG

                                            • VISIBILITY_BOTH






                                            share|improve this answer


























                                              0












                                              0








                                              0







                                              For getting product collection with status enable and visibility:



                                              you have to keep below code in block file.



                                              protected $_coreRegistry = null;

                                              public function __construct(
                                              MagentoFrameworkViewElementTemplateContext $context,
                                              MagentoFrameworkRegistry $registry,
                                              MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
                                              MagentoCatalogModelProductAttributeSourceStatus $productStatus,
                                              MagentoCatalogModelProductVisibility $productVisibility,
                                              array $data =
                                              )
                                              {

                                              $this->_coreRegistry = $registry;
                                              $this->_productCollectionFactory = $productCollectionFactory;
                                              $this->productStatus = $productStatus;
                                              $this->productVisibility = $productVisibility;
                                              parent::__construct($context, $data);
                                              }

                                              public function getProductCollection()
                                              {
                                              $collection = $this->_productCollectionFactory->create();
                                              $collection->addAttributeToFilter('manufacturer', ['eq' => '20']);
                                              $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
                                              $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());

                                              return $collection;
                                              }


                                              Above code working for visibility type for:




                                              • VISIBILITY_IN_SEARCH

                                              • VISIBILITY_IN_CATALOG

                                              • VISIBILITY_BOTH






                                              share|improve this answer













                                              For getting product collection with status enable and visibility:



                                              you have to keep below code in block file.



                                              protected $_coreRegistry = null;

                                              public function __construct(
                                              MagentoFrameworkViewElementTemplateContext $context,
                                              MagentoFrameworkRegistry $registry,
                                              MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
                                              MagentoCatalogModelProductAttributeSourceStatus $productStatus,
                                              MagentoCatalogModelProductVisibility $productVisibility,
                                              array $data =
                                              )
                                              {

                                              $this->_coreRegistry = $registry;
                                              $this->_productCollectionFactory = $productCollectionFactory;
                                              $this->productStatus = $productStatus;
                                              $this->productVisibility = $productVisibility;
                                              parent::__construct($context, $data);
                                              }

                                              public function getProductCollection()
                                              {
                                              $collection = $this->_productCollectionFactory->create();
                                              $collection->addAttributeToFilter('manufacturer', ['eq' => '20']);
                                              $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
                                              $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());

                                              return $collection;
                                              }


                                              Above code working for visibility type for:




                                              • VISIBILITY_IN_SEARCH

                                              • VISIBILITY_IN_CATALOG

                                              • VISIBILITY_BOTH







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Jun 3 '18 at 14:24







                                              user68116





































                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Magento Stack Exchange!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f117112%2fmagento-2-how-to-set-visibility-and-status-to-product-using-collection%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  What other Star Trek series did the main TNG cast show up in?

                                                  Berlina muro

                                                  Berlina aerponto