How to get a list of all products in Magento 2? The 2019 Stack Overflow Developer Survey Results Are InMagento 2 programatically get special price product collectionHow to get *every* products (in stock, out of stock, enabled, disabled, etc..)How to load a model with relationships via repository in Magento 2?Magento 2 - how to get models, config(s), and order info similar to Magento 1 style (Mage::getBaseUrl, Mage::getModel, etc…))How to get all products previously ordered by a customer?Get all products instead of a specific category?How to list all disabled products with sql?Magento 2: List all productsGet quantity attribute in products list pageHow to get all list of customer group?Magento : Get RETURNED/EXCHANGED Items List From DatabaseGet list of disabled products in Magento 2 with SQL query?Magento 2.2.3 - Get all products in product grid/listHow to get all current category products in magento 2

Where to refill my bottle in India?

Is this app Icon Browser Safe/Legit?

Output the Arecibo Message

Is flight data recorder erased after every flight?

What is the most effective way of iterating a std::vector and why?

For what reasons would an animal species NOT cross a *horizontal* land bridge?

Is a "Democratic" Feudal System Possible?

Sci-fi book where a human is taken from Earth to help man an alien ship in a fight against other aliens and rises through the ranks to command

Am I thawing this London Broil safely?

How to support a colleague who finds meetings extremely tiring?

Apparent duplicates between Haynes service instructions and MOT

Do these rules for Critical Successes and Critical Failures seem Fair?

The difference between dialogue marks

Multiply Two Integer Polynomials

Does the shape of a die affect the probability of a number being rolled?

Are there incongruent pythagorean triangles with the same perimeter and same area?

Is three citations per paragraph excessive for undergraduate research paper?

What could be the right powersource for 15 seconds lifespan disposable giant chainsaw?

Is "plugging out" electronic devices an American expression?

What are the motivations for publishing new editions of an existing textbook, beyond new discoveries in a field?

What is the meaning of the verb "bear" in this context?

Why did Acorn's A3000 have red function keys?

How can I autofill dates in Excel excluding Sunday?

Right tool to dig six foot holes?



How to get a list of all products in Magento 2?



The 2019 Stack Overflow Developer Survey Results Are InMagento 2 programatically get special price product collectionHow to get *every* products (in stock, out of stock, enabled, disabled, etc..)How to load a model with relationships via repository in Magento 2?Magento 2 - how to get models, config(s), and order info similar to Magento 1 style (Mage::getBaseUrl, Mage::getModel, etc…))How to get all products previously ordered by a customer?Get all products instead of a specific category?How to list all disabled products with sql?Magento 2: List all productsGet quantity attribute in products list pageHow to get all list of customer group?Magento : Get RETURNED/EXCHANGED Items List From DatabaseGet list of disabled products in Magento 2 with SQL query?Magento 2.2.3 - Get all products in product grid/listHow to get all current category products in magento 2



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








10















How can I get a list of all available products in Magento 2?










share|improve this question






















  • My answer hasn't helped you?

    – Siarhey Uchukhlebau
    Sep 9 '16 at 12:51






  • 1





    Mark now @SiarheyUchukhlebau answer as correct....

    – P0lT10n
    Mar 15 '17 at 14:28

















10















How can I get a list of all available products in Magento 2?










share|improve this question






















  • My answer hasn't helped you?

    – Siarhey Uchukhlebau
    Sep 9 '16 at 12:51






  • 1





    Mark now @SiarheyUchukhlebau answer as correct....

    – P0lT10n
    Mar 15 '17 at 14:28













10












10








10


6






How can I get a list of all available products in Magento 2?










share|improve this question














How can I get a list of all available products in Magento 2?







magento2 product






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 5 '16 at 14:12









nuwausnuwaus

93432046




93432046












  • My answer hasn't helped you?

    – Siarhey Uchukhlebau
    Sep 9 '16 at 12:51






  • 1





    Mark now @SiarheyUchukhlebau answer as correct....

    – P0lT10n
    Mar 15 '17 at 14:28

















  • My answer hasn't helped you?

    – Siarhey Uchukhlebau
    Sep 9 '16 at 12:51






  • 1





    Mark now @SiarheyUchukhlebau answer as correct....

    – P0lT10n
    Mar 15 '17 at 14:28
















My answer hasn't helped you?

– Siarhey Uchukhlebau
Sep 9 '16 at 12:51





My answer hasn't helped you?

– Siarhey Uchukhlebau
Sep 9 '16 at 12:51




1




1





Mark now @SiarheyUchukhlebau answer as correct....

– P0lT10n
Mar 15 '17 at 14:28





Mark now @SiarheyUchukhlebau answer as correct....

– P0lT10n
Mar 15 '17 at 14:28










4 Answers
4






active

oldest

votes


















18














You should use MagentoCatalogModelProductRepository or MagentoCatalogModelResourceModelProductCollection according your needs. You can use both methods to get product instances with all data.



Example 1 (Repository):



/**
* @param MagentoCatalogModelProductRepository $productRepository
* @param MagentoFrameworkApiSearchCriteriaInterface $criteria
* @param MagentoFrameworkApiSearchFilterGroup $filterGroup
* @param MagentoFrameworkApiFilterBuilder $filterBuilder
* @param MagentoCatalogModelProductAttributeSourceStatus $productStatus
* @param MagentoCatalogModelProductVisibility $productVisibility
*/
public function __construct(
MagentoCatalogModelProductRepository $productRepository,
MagentoFrameworkApiSearchCriteriaInterface $criteria,
MagentoFrameworkApiSearchFilterGroup $filterGroup,
MagentoFrameworkApiFilterBuilder $filterBuilder,
MagentoCatalogModelProductAttributeSourceStatus $productStatus,
MagentoCatalogModelProductVisibility $productVisibility
)
$this->productRepository = $productRepository;
$this->searchCriteria = $criteria;
$this->filterGroup = $filterGroup;
$this->filterBuilder = $filterBuilder;
$this->productStatus = $productStatus;
$this->productVisibility = $productVisibility;

$this->getProductData();


/**
* @return MagentoCmsModelBlock|null
* @throws MagentoFrameworkExceptionNoSuchEntityException
*/
protected function getProductData()


$this->filterGroup->setFilters([
$this->filterBuilder
->setField('status')
->setConditionType('in')
->setValue($this->productStatus->getVisibleStatusIds())
->create(),
$this->filterBuilder
->setField('visibility')
->setConditionType('in')
->setValue($this->productVisibility->getVisibleInSiteIds())
->create(),
]);

$this->searchCriteria->setFilterGroups([$this->filterGroup]);
$products = $this->productRepository->getList($this->searchCriteria);
$productItems = $products->getItems();

return $productItems;



Result:



repository



Example 2 (Collection):



/**
* @param MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory
* @param MagentoCatalogModelProductAttributeSourceStatus $productStatus
* @param MagentoCatalogModelProductVisibility $productVisibility
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function __construct(
MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
MagentoCatalogModelProductAttributeSourceStatus $productStatus,
MagentoCatalogModelProductVisibility $productVisibility
)
$this->productCollectionFactory = $productCollectionFactory;
$this->productStatus = $productStatus;
$this->productVisibility = $productVisibility;


/**
* @return MagentoFrameworkDataObject[]
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function getProducts()

/** @var MagentoCatalogModelResourceModelProductCollection $collection */
$collection = $this->productCollectionFactory->create();
$collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
$collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()])
->addAttributeToFilter('visibility', ['in' => $this->productVisibility->getVisibleInSiteIds()]);

return $collection->getItems();



Result:



collection






share|improve this answer

























  • hi @siarhey, I am getting an empty result with your code. Do you have any tips about it? Could it be because of I am trying to retrieve the list from a cron task?

    – Lorenzo S
    Feb 10 '17 at 11:22











  • @LorenzoS Hi, which example you are using 1 or 2? Does any product exist when you remove all filters/conditions?

    – Siarhey Uchukhlebau
    Feb 10 '17 at 11:36











  • How do get this "Variables" tab with objects tree in your screenshot ?

    – maxagaz
    May 15 '17 at 7:12











  • @maxagaz Using xDebug in PHPStorm

    – Siarhey Uchukhlebau
    May 15 '17 at 7:15











  • great answer, @SiarheyUchukhlebau but what do you mean by If you need just product data use the first class. If you need to get the product instances - use second class.

    – Yehia A.Salam
    May 31 '17 at 18:57


















0














You can do this through api request. Take a look at the default m2 api



think your better shoot is catalogProductRepositoryV1






share|improve this answer
































    0














    Using a REST API call:



    https://host/rest/V1/products/?searchCriteria=


    Retrieves all products. "searchCriteria" is a required param, but can be left blank, as above.






    share|improve this answer






























      0














      The most upvoted answer is working but I would like to mention that here by injecting a product repository implementation directly will cause the breach of service contract principle & that is something Magento wants developers to correct.
      You should inject MagentoCatalogApiProductRepositoryInterface $productRepository instead of it's implementation which is MagentoCatalogModelProductRepository $productRepository.
      That way you you will have a space for future upgrade ability. Bottom line is use Service contracts as much as possible.






      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%2f130185%2fhow-to-get-a-list-of-all-products-in-magento-2%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









        18














        You should use MagentoCatalogModelProductRepository or MagentoCatalogModelResourceModelProductCollection according your needs. You can use both methods to get product instances with all data.



        Example 1 (Repository):



        /**
        * @param MagentoCatalogModelProductRepository $productRepository
        * @param MagentoFrameworkApiSearchCriteriaInterface $criteria
        * @param MagentoFrameworkApiSearchFilterGroup $filterGroup
        * @param MagentoFrameworkApiFilterBuilder $filterBuilder
        * @param MagentoCatalogModelProductAttributeSourceStatus $productStatus
        * @param MagentoCatalogModelProductVisibility $productVisibility
        */
        public function __construct(
        MagentoCatalogModelProductRepository $productRepository,
        MagentoFrameworkApiSearchCriteriaInterface $criteria,
        MagentoFrameworkApiSearchFilterGroup $filterGroup,
        MagentoFrameworkApiFilterBuilder $filterBuilder,
        MagentoCatalogModelProductAttributeSourceStatus $productStatus,
        MagentoCatalogModelProductVisibility $productVisibility
        )
        $this->productRepository = $productRepository;
        $this->searchCriteria = $criteria;
        $this->filterGroup = $filterGroup;
        $this->filterBuilder = $filterBuilder;
        $this->productStatus = $productStatus;
        $this->productVisibility = $productVisibility;

        $this->getProductData();


        /**
        * @return MagentoCmsModelBlock|null
        * @throws MagentoFrameworkExceptionNoSuchEntityException
        */
        protected function getProductData()


        $this->filterGroup->setFilters([
        $this->filterBuilder
        ->setField('status')
        ->setConditionType('in')
        ->setValue($this->productStatus->getVisibleStatusIds())
        ->create(),
        $this->filterBuilder
        ->setField('visibility')
        ->setConditionType('in')
        ->setValue($this->productVisibility->getVisibleInSiteIds())
        ->create(),
        ]);

        $this->searchCriteria->setFilterGroups([$this->filterGroup]);
        $products = $this->productRepository->getList($this->searchCriteria);
        $productItems = $products->getItems();

        return $productItems;



        Result:



        repository



        Example 2 (Collection):



        /**
        * @param MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory
        * @param MagentoCatalogModelProductAttributeSourceStatus $productStatus
        * @param MagentoCatalogModelProductVisibility $productVisibility
        * @throws MagentoFrameworkExceptionLocalizedException
        */
        public function __construct(
        MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
        MagentoCatalogModelProductAttributeSourceStatus $productStatus,
        MagentoCatalogModelProductVisibility $productVisibility
        )
        $this->productCollectionFactory = $productCollectionFactory;
        $this->productStatus = $productStatus;
        $this->productVisibility = $productVisibility;


        /**
        * @return MagentoFrameworkDataObject[]
        * @throws MagentoFrameworkExceptionLocalizedException
        */
        public function getProducts()

        /** @var MagentoCatalogModelResourceModelProductCollection $collection */
        $collection = $this->productCollectionFactory->create();
        $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
        $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
        $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()])
        ->addAttributeToFilter('visibility', ['in' => $this->productVisibility->getVisibleInSiteIds()]);

        return $collection->getItems();



        Result:



        collection






        share|improve this answer

























        • hi @siarhey, I am getting an empty result with your code. Do you have any tips about it? Could it be because of I am trying to retrieve the list from a cron task?

          – Lorenzo S
          Feb 10 '17 at 11:22











        • @LorenzoS Hi, which example you are using 1 or 2? Does any product exist when you remove all filters/conditions?

          – Siarhey Uchukhlebau
          Feb 10 '17 at 11:36











        • How do get this "Variables" tab with objects tree in your screenshot ?

          – maxagaz
          May 15 '17 at 7:12











        • @maxagaz Using xDebug in PHPStorm

          – Siarhey Uchukhlebau
          May 15 '17 at 7:15











        • great answer, @SiarheyUchukhlebau but what do you mean by If you need just product data use the first class. If you need to get the product instances - use second class.

          – Yehia A.Salam
          May 31 '17 at 18:57















        18














        You should use MagentoCatalogModelProductRepository or MagentoCatalogModelResourceModelProductCollection according your needs. You can use both methods to get product instances with all data.



        Example 1 (Repository):



        /**
        * @param MagentoCatalogModelProductRepository $productRepository
        * @param MagentoFrameworkApiSearchCriteriaInterface $criteria
        * @param MagentoFrameworkApiSearchFilterGroup $filterGroup
        * @param MagentoFrameworkApiFilterBuilder $filterBuilder
        * @param MagentoCatalogModelProductAttributeSourceStatus $productStatus
        * @param MagentoCatalogModelProductVisibility $productVisibility
        */
        public function __construct(
        MagentoCatalogModelProductRepository $productRepository,
        MagentoFrameworkApiSearchCriteriaInterface $criteria,
        MagentoFrameworkApiSearchFilterGroup $filterGroup,
        MagentoFrameworkApiFilterBuilder $filterBuilder,
        MagentoCatalogModelProductAttributeSourceStatus $productStatus,
        MagentoCatalogModelProductVisibility $productVisibility
        )
        $this->productRepository = $productRepository;
        $this->searchCriteria = $criteria;
        $this->filterGroup = $filterGroup;
        $this->filterBuilder = $filterBuilder;
        $this->productStatus = $productStatus;
        $this->productVisibility = $productVisibility;

        $this->getProductData();


        /**
        * @return MagentoCmsModelBlock|null
        * @throws MagentoFrameworkExceptionNoSuchEntityException
        */
        protected function getProductData()


        $this->filterGroup->setFilters([
        $this->filterBuilder
        ->setField('status')
        ->setConditionType('in')
        ->setValue($this->productStatus->getVisibleStatusIds())
        ->create(),
        $this->filterBuilder
        ->setField('visibility')
        ->setConditionType('in')
        ->setValue($this->productVisibility->getVisibleInSiteIds())
        ->create(),
        ]);

        $this->searchCriteria->setFilterGroups([$this->filterGroup]);
        $products = $this->productRepository->getList($this->searchCriteria);
        $productItems = $products->getItems();

        return $productItems;



        Result:



        repository



        Example 2 (Collection):



        /**
        * @param MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory
        * @param MagentoCatalogModelProductAttributeSourceStatus $productStatus
        * @param MagentoCatalogModelProductVisibility $productVisibility
        * @throws MagentoFrameworkExceptionLocalizedException
        */
        public function __construct(
        MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
        MagentoCatalogModelProductAttributeSourceStatus $productStatus,
        MagentoCatalogModelProductVisibility $productVisibility
        )
        $this->productCollectionFactory = $productCollectionFactory;
        $this->productStatus = $productStatus;
        $this->productVisibility = $productVisibility;


        /**
        * @return MagentoFrameworkDataObject[]
        * @throws MagentoFrameworkExceptionLocalizedException
        */
        public function getProducts()

        /** @var MagentoCatalogModelResourceModelProductCollection $collection */
        $collection = $this->productCollectionFactory->create();
        $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
        $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
        $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()])
        ->addAttributeToFilter('visibility', ['in' => $this->productVisibility->getVisibleInSiteIds()]);

        return $collection->getItems();



        Result:



        collection






        share|improve this answer

























        • hi @siarhey, I am getting an empty result with your code. Do you have any tips about it? Could it be because of I am trying to retrieve the list from a cron task?

          – Lorenzo S
          Feb 10 '17 at 11:22











        • @LorenzoS Hi, which example you are using 1 or 2? Does any product exist when you remove all filters/conditions?

          – Siarhey Uchukhlebau
          Feb 10 '17 at 11:36











        • How do get this "Variables" tab with objects tree in your screenshot ?

          – maxagaz
          May 15 '17 at 7:12











        • @maxagaz Using xDebug in PHPStorm

          – Siarhey Uchukhlebau
          May 15 '17 at 7:15











        • great answer, @SiarheyUchukhlebau but what do you mean by If you need just product data use the first class. If you need to get the product instances - use second class.

          – Yehia A.Salam
          May 31 '17 at 18:57













        18












        18








        18







        You should use MagentoCatalogModelProductRepository or MagentoCatalogModelResourceModelProductCollection according your needs. You can use both methods to get product instances with all data.



        Example 1 (Repository):



        /**
        * @param MagentoCatalogModelProductRepository $productRepository
        * @param MagentoFrameworkApiSearchCriteriaInterface $criteria
        * @param MagentoFrameworkApiSearchFilterGroup $filterGroup
        * @param MagentoFrameworkApiFilterBuilder $filterBuilder
        * @param MagentoCatalogModelProductAttributeSourceStatus $productStatus
        * @param MagentoCatalogModelProductVisibility $productVisibility
        */
        public function __construct(
        MagentoCatalogModelProductRepository $productRepository,
        MagentoFrameworkApiSearchCriteriaInterface $criteria,
        MagentoFrameworkApiSearchFilterGroup $filterGroup,
        MagentoFrameworkApiFilterBuilder $filterBuilder,
        MagentoCatalogModelProductAttributeSourceStatus $productStatus,
        MagentoCatalogModelProductVisibility $productVisibility
        )
        $this->productRepository = $productRepository;
        $this->searchCriteria = $criteria;
        $this->filterGroup = $filterGroup;
        $this->filterBuilder = $filterBuilder;
        $this->productStatus = $productStatus;
        $this->productVisibility = $productVisibility;

        $this->getProductData();


        /**
        * @return MagentoCmsModelBlock|null
        * @throws MagentoFrameworkExceptionNoSuchEntityException
        */
        protected function getProductData()


        $this->filterGroup->setFilters([
        $this->filterBuilder
        ->setField('status')
        ->setConditionType('in')
        ->setValue($this->productStatus->getVisibleStatusIds())
        ->create(),
        $this->filterBuilder
        ->setField('visibility')
        ->setConditionType('in')
        ->setValue($this->productVisibility->getVisibleInSiteIds())
        ->create(),
        ]);

        $this->searchCriteria->setFilterGroups([$this->filterGroup]);
        $products = $this->productRepository->getList($this->searchCriteria);
        $productItems = $products->getItems();

        return $productItems;



        Result:



        repository



        Example 2 (Collection):



        /**
        * @param MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory
        * @param MagentoCatalogModelProductAttributeSourceStatus $productStatus
        * @param MagentoCatalogModelProductVisibility $productVisibility
        * @throws MagentoFrameworkExceptionLocalizedException
        */
        public function __construct(
        MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
        MagentoCatalogModelProductAttributeSourceStatus $productStatus,
        MagentoCatalogModelProductVisibility $productVisibility
        )
        $this->productCollectionFactory = $productCollectionFactory;
        $this->productStatus = $productStatus;
        $this->productVisibility = $productVisibility;


        /**
        * @return MagentoFrameworkDataObject[]
        * @throws MagentoFrameworkExceptionLocalizedException
        */
        public function getProducts()

        /** @var MagentoCatalogModelResourceModelProductCollection $collection */
        $collection = $this->productCollectionFactory->create();
        $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
        $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
        $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()])
        ->addAttributeToFilter('visibility', ['in' => $this->productVisibility->getVisibleInSiteIds()]);

        return $collection->getItems();



        Result:



        collection






        share|improve this answer















        You should use MagentoCatalogModelProductRepository or MagentoCatalogModelResourceModelProductCollection according your needs. You can use both methods to get product instances with all data.



        Example 1 (Repository):



        /**
        * @param MagentoCatalogModelProductRepository $productRepository
        * @param MagentoFrameworkApiSearchCriteriaInterface $criteria
        * @param MagentoFrameworkApiSearchFilterGroup $filterGroup
        * @param MagentoFrameworkApiFilterBuilder $filterBuilder
        * @param MagentoCatalogModelProductAttributeSourceStatus $productStatus
        * @param MagentoCatalogModelProductVisibility $productVisibility
        */
        public function __construct(
        MagentoCatalogModelProductRepository $productRepository,
        MagentoFrameworkApiSearchCriteriaInterface $criteria,
        MagentoFrameworkApiSearchFilterGroup $filterGroup,
        MagentoFrameworkApiFilterBuilder $filterBuilder,
        MagentoCatalogModelProductAttributeSourceStatus $productStatus,
        MagentoCatalogModelProductVisibility $productVisibility
        )
        $this->productRepository = $productRepository;
        $this->searchCriteria = $criteria;
        $this->filterGroup = $filterGroup;
        $this->filterBuilder = $filterBuilder;
        $this->productStatus = $productStatus;
        $this->productVisibility = $productVisibility;

        $this->getProductData();


        /**
        * @return MagentoCmsModelBlock|null
        * @throws MagentoFrameworkExceptionNoSuchEntityException
        */
        protected function getProductData()


        $this->filterGroup->setFilters([
        $this->filterBuilder
        ->setField('status')
        ->setConditionType('in')
        ->setValue($this->productStatus->getVisibleStatusIds())
        ->create(),
        $this->filterBuilder
        ->setField('visibility')
        ->setConditionType('in')
        ->setValue($this->productVisibility->getVisibleInSiteIds())
        ->create(),
        ]);

        $this->searchCriteria->setFilterGroups([$this->filterGroup]);
        $products = $this->productRepository->getList($this->searchCriteria);
        $productItems = $products->getItems();

        return $productItems;



        Result:



        repository



        Example 2 (Collection):



        /**
        * @param MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory
        * @param MagentoCatalogModelProductAttributeSourceStatus $productStatus
        * @param MagentoCatalogModelProductVisibility $productVisibility
        * @throws MagentoFrameworkExceptionLocalizedException
        */
        public function __construct(
        MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
        MagentoCatalogModelProductAttributeSourceStatus $productStatus,
        MagentoCatalogModelProductVisibility $productVisibility
        )
        $this->productCollectionFactory = $productCollectionFactory;
        $this->productStatus = $productStatus;
        $this->productVisibility = $productVisibility;


        /**
        * @return MagentoFrameworkDataObject[]
        * @throws MagentoFrameworkExceptionLocalizedException
        */
        public function getProducts()

        /** @var MagentoCatalogModelResourceModelProductCollection $collection */
        $collection = $this->productCollectionFactory->create();
        $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
        $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
        $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()])
        ->addAttributeToFilter('visibility', ['in' => $this->productVisibility->getVisibleInSiteIds()]);

        return $collection->getItems();



        Result:



        collection







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jun 1 '17 at 8:40

























        answered Aug 5 '16 at 14:53









        Siarhey UchukhlebauSiarhey Uchukhlebau

        10k93058




        10k93058












        • hi @siarhey, I am getting an empty result with your code. Do you have any tips about it? Could it be because of I am trying to retrieve the list from a cron task?

          – Lorenzo S
          Feb 10 '17 at 11:22











        • @LorenzoS Hi, which example you are using 1 or 2? Does any product exist when you remove all filters/conditions?

          – Siarhey Uchukhlebau
          Feb 10 '17 at 11:36











        • How do get this "Variables" tab with objects tree in your screenshot ?

          – maxagaz
          May 15 '17 at 7:12











        • @maxagaz Using xDebug in PHPStorm

          – Siarhey Uchukhlebau
          May 15 '17 at 7:15











        • great answer, @SiarheyUchukhlebau but what do you mean by If you need just product data use the first class. If you need to get the product instances - use second class.

          – Yehia A.Salam
          May 31 '17 at 18:57

















        • hi @siarhey, I am getting an empty result with your code. Do you have any tips about it? Could it be because of I am trying to retrieve the list from a cron task?

          – Lorenzo S
          Feb 10 '17 at 11:22











        • @LorenzoS Hi, which example you are using 1 or 2? Does any product exist when you remove all filters/conditions?

          – Siarhey Uchukhlebau
          Feb 10 '17 at 11:36











        • How do get this "Variables" tab with objects tree in your screenshot ?

          – maxagaz
          May 15 '17 at 7:12











        • @maxagaz Using xDebug in PHPStorm

          – Siarhey Uchukhlebau
          May 15 '17 at 7:15











        • great answer, @SiarheyUchukhlebau but what do you mean by If you need just product data use the first class. If you need to get the product instances - use second class.

          – Yehia A.Salam
          May 31 '17 at 18:57
















        hi @siarhey, I am getting an empty result with your code. Do you have any tips about it? Could it be because of I am trying to retrieve the list from a cron task?

        – Lorenzo S
        Feb 10 '17 at 11:22





        hi @siarhey, I am getting an empty result with your code. Do you have any tips about it? Could it be because of I am trying to retrieve the list from a cron task?

        – Lorenzo S
        Feb 10 '17 at 11:22













        @LorenzoS Hi, which example you are using 1 or 2? Does any product exist when you remove all filters/conditions?

        – Siarhey Uchukhlebau
        Feb 10 '17 at 11:36





        @LorenzoS Hi, which example you are using 1 or 2? Does any product exist when you remove all filters/conditions?

        – Siarhey Uchukhlebau
        Feb 10 '17 at 11:36













        How do get this "Variables" tab with objects tree in your screenshot ?

        – maxagaz
        May 15 '17 at 7:12





        How do get this "Variables" tab with objects tree in your screenshot ?

        – maxagaz
        May 15 '17 at 7:12













        @maxagaz Using xDebug in PHPStorm

        – Siarhey Uchukhlebau
        May 15 '17 at 7:15





        @maxagaz Using xDebug in PHPStorm

        – Siarhey Uchukhlebau
        May 15 '17 at 7:15













        great answer, @SiarheyUchukhlebau but what do you mean by If you need just product data use the first class. If you need to get the product instances - use second class.

        – Yehia A.Salam
        May 31 '17 at 18:57





        great answer, @SiarheyUchukhlebau but what do you mean by If you need just product data use the first class. If you need to get the product instances - use second class.

        – Yehia A.Salam
        May 31 '17 at 18:57













        0














        You can do this through api request. Take a look at the default m2 api



        think your better shoot is catalogProductRepositoryV1






        share|improve this answer





























          0














          You can do this through api request. Take a look at the default m2 api



          think your better shoot is catalogProductRepositoryV1






          share|improve this answer



























            0












            0








            0







            You can do this through api request. Take a look at the default m2 api



            think your better shoot is catalogProductRepositoryV1






            share|improve this answer















            You can do this through api request. Take a look at the default m2 api



            think your better shoot is catalogProductRepositoryV1







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 5 '16 at 16:14

























            answered Aug 5 '16 at 14:32









            9562395623

            1545




            1545





















                0














                Using a REST API call:



                https://host/rest/V1/products/?searchCriteria=


                Retrieves all products. "searchCriteria" is a required param, but can be left blank, as above.






                share|improve this answer



























                  0














                  Using a REST API call:



                  https://host/rest/V1/products/?searchCriteria=


                  Retrieves all products. "searchCriteria" is a required param, but can be left blank, as above.






                  share|improve this answer

























                    0












                    0








                    0







                    Using a REST API call:



                    https://host/rest/V1/products/?searchCriteria=


                    Retrieves all products. "searchCriteria" is a required param, but can be left blank, as above.






                    share|improve this answer













                    Using a REST API call:



                    https://host/rest/V1/products/?searchCriteria=


                    Retrieves all products. "searchCriteria" is a required param, but can be left blank, as above.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Apr 3 '18 at 19:10









                    JamezJamez

                    1




                    1





















                        0














                        The most upvoted answer is working but I would like to mention that here by injecting a product repository implementation directly will cause the breach of service contract principle & that is something Magento wants developers to correct.
                        You should inject MagentoCatalogApiProductRepositoryInterface $productRepository instead of it's implementation which is MagentoCatalogModelProductRepository $productRepository.
                        That way you you will have a space for future upgrade ability. Bottom line is use Service contracts as much as possible.






                        share|improve this answer





























                          0














                          The most upvoted answer is working but I would like to mention that here by injecting a product repository implementation directly will cause the breach of service contract principle & that is something Magento wants developers to correct.
                          You should inject MagentoCatalogApiProductRepositoryInterface $productRepository instead of it's implementation which is MagentoCatalogModelProductRepository $productRepository.
                          That way you you will have a space for future upgrade ability. Bottom line is use Service contracts as much as possible.






                          share|improve this answer



























                            0












                            0








                            0







                            The most upvoted answer is working but I would like to mention that here by injecting a product repository implementation directly will cause the breach of service contract principle & that is something Magento wants developers to correct.
                            You should inject MagentoCatalogApiProductRepositoryInterface $productRepository instead of it's implementation which is MagentoCatalogModelProductRepository $productRepository.
                            That way you you will have a space for future upgrade ability. Bottom line is use Service contracts as much as possible.






                            share|improve this answer















                            The most upvoted answer is working but I would like to mention that here by injecting a product repository implementation directly will cause the breach of service contract principle & that is something Magento wants developers to correct.
                            You should inject MagentoCatalogApiProductRepositoryInterface $productRepository instead of it's implementation which is MagentoCatalogModelProductRepository $productRepository.
                            That way you you will have a space for future upgrade ability. Bottom line is use Service contracts as much as possible.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Apr 7 at 15:09

























                            answered Apr 7 at 8:45









                            SarvagyaSarvagya

                            77521638




                            77521638



























                                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%2f130185%2fhow-to-get-a-list-of-all-products-in-magento-2%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

                                Bulk add to cart function issuecart vs. mini cart issue … rwd themeRedirect Add to cart button to cart pageAdd to cart issue - Magento 2.1The requested Payment Method is not available When creating an orderM2: reason add-to-cart might not function in production modeAdd to cart issue in some android devicesMagento 2 - custom price can not add to subtotal and grand total after add to cartAdd to cart codeIssue with my cart module on pdp and cart pages, just keeps spinningBulk price and quantity update using rest api

                                Magento2 - How to hide price filter only in specific categories?Multiselect price filter attribute in layered navigationhide only some categories from layered navigation in magentoRemove Price Filter on certain categoriescustomize layered price filter?Hide Price for a particular customer groupPrice filter in layered navigation not working correctly with price including tax in magento 2.2.3Magento 2 how to hide attribute at Layered navigation?Magento 2. how to hide price only for specific categoriesMagento 2 How can I hide the price and total from cart and checkout summary?Magento2: Can we add navigation layered filter like price filter for other attribute?