How to get Latest Products Collection in my costum Module Magento 2Magento2: New Product collectionGet most viewed productsHow to get only out of stock products in magento 2Magento get latest products added in the storeMagento 1 to Magento 2 models Mage::getModel('sales/order_shipment_api') and Mage::getModel('sales/order_shipment')Get Wishlist Collection By CategoryMagento 2: Get disabled products for a category in a collection programaticallyHow to get recently viewed products collection of Customer Magento 2?Magento 1.9 addAttributeToFilter() returning 0 productsMagento2: How to get simple products Assosiate to configurable productsMagento 2 - How get extension attributes from product collectionMagento 2 : Get last five order information using DI on homepage?

Drawing lines to nearest point

Why can't RGB or bicolour LEDs produce a decent yellow?

Why in a Ethernet LAN, a packet sniffer can obtain all packets sent over the LAN?

What is the significance of 4200 BCE in context of farming replacing foraging in Europe?

Is it a bad idea to replace pull-up resistors with hard pull-ups?

Why does my circuit work on a breadboard, but not on a perfboard? I am new to soldering

How are one-time password generators like Google Authenticator different from having two passwords?

Exception propagation: When should I catch exceptions?

Can the sorting of a list be verified without comparing neighbors?

How to prevent cooked noodles & dumplings from getting soggy

51% attack - apparently very easy? refering to CZ's "rollback btc chain" - How to make sure such corruptible scenario can never happen so easily?

Smallest Guaranteed hash collision cycle length

How do I tell my supervisor that he is choosing poor replacements for me while I am on maternity leave?

Proof that the inverse image of a single element is a discrete space

How to slow yourself down (for playing nice with others)

Why is this int array not passed as an object vararg array?

Is the schwa sound consistent?

Anatomically Correct Carnivorous Tree

How did Thanos not realise this had happened at the end of Endgame?

How can this pool heater gas line be disconnected?

Would an 8% reduction in drag outweigh the weight addition from this custom CFD-tested winglet?

What does a comma mean inside an 'if' statement?

Why does the Earth follow an elliptical trajectory rather than a parabolic one?

Why not just directly invest in the holdings of an ETF?



How to get Latest Products Collection in my costum Module Magento 2


Magento2: New Product collectionGet most viewed productsHow to get only out of stock products in magento 2Magento get latest products added in the storeMagento 1 to Magento 2 models Mage::getModel('sales/order_shipment_api') and Mage::getModel('sales/order_shipment')Get Wishlist Collection By CategoryMagento 2: Get disabled products for a category in a collection programaticallyHow to get recently viewed products collection of Customer Magento 2?Magento 1.9 addAttributeToFilter() returning 0 productsMagento2: How to get simple products Assosiate to configurable productsMagento 2 - How get extension attributes from product collectionMagento 2 : Get last five order information using DI on homepage?






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








2















I want to get Collection of Latest Products in Magento 2
i have some restriction



  • Without using Object Manager

  • Without using API

i have searched on internet found these two methods but i have to avoid using them










share|improve this question




























    2















    I want to get Collection of Latest Products in Magento 2
    i have some restriction



    • Without using Object Manager

    • Without using API

    i have searched on internet found these two methods but i have to avoid using them










    share|improve this question
























      2












      2








      2








      I want to get Collection of Latest Products in Magento 2
      i have some restriction



      • Without using Object Manager

      • Without using API

      i have searched on internet found these two methods but i have to avoid using them










      share|improve this question














      I want to get Collection of Latest Products in Magento 2
      i have some restriction



      • Without using Object Manager

      • Without using API

      i have searched on internet found these two methods but i have to avoid using them







      magento2 product product-collection






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked May 8 at 9:14









      Waqar AliWaqar Ali

      6110




      6110




















          1 Answer
          1






          active

          oldest

          votes


















          1














          You can create a Block class and inject MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory in the constructor and you can get/display product collection in phtml file.



          Check below code:



          <?php
          namespace VendorModuleBlock;

          class ProductDisplay extends MagentoFrameworkViewElementTemplate

          protected $_productCollectionFactory;

          public function __construct(
          MagentoBackendBlockTemplateContext $context,
          MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
          array $data = []
          )

          $this->_productCollectionFactory = $productCollectionFactory;
          parent::__construct($context, $data);


          public function getProductCollection()

          $collection = $this->_productCollectionFactory->create();
          $collection->addAttributeToSelect('*');
          $collection->setPageSize(5); // fetching only 3 products
          $colletion->setOrder('created_at','DESC'); //sort by date created
          return $collection;




          In your phtml file:



          $productCollection = $block->getProductCollection();
          foreach ($productCollection as $product)
          print_r($product->getData());
          echo "<br>";






          share|improve this answer

























          • please explain how you have used logic for latest products.. i am not getting that

            – Waqar Ali
            May 8 at 9:41











          • updated the answer with sort order

            – magefms
            May 8 at 9:46






          • 1





            thank you its working

            – Waqar Ali
            May 8 at 9:53











          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%2f273805%2fhow-to-get-latest-products-collection-in-my-costum-module-magento-2%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You can create a Block class and inject MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory in the constructor and you can get/display product collection in phtml file.



          Check below code:



          <?php
          namespace VendorModuleBlock;

          class ProductDisplay extends MagentoFrameworkViewElementTemplate

          protected $_productCollectionFactory;

          public function __construct(
          MagentoBackendBlockTemplateContext $context,
          MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
          array $data = []
          )

          $this->_productCollectionFactory = $productCollectionFactory;
          parent::__construct($context, $data);


          public function getProductCollection()

          $collection = $this->_productCollectionFactory->create();
          $collection->addAttributeToSelect('*');
          $collection->setPageSize(5); // fetching only 3 products
          $colletion->setOrder('created_at','DESC'); //sort by date created
          return $collection;




          In your phtml file:



          $productCollection = $block->getProductCollection();
          foreach ($productCollection as $product)
          print_r($product->getData());
          echo "<br>";






          share|improve this answer

























          • please explain how you have used logic for latest products.. i am not getting that

            – Waqar Ali
            May 8 at 9:41











          • updated the answer with sort order

            – magefms
            May 8 at 9:46






          • 1





            thank you its working

            – Waqar Ali
            May 8 at 9:53















          1














          You can create a Block class and inject MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory in the constructor and you can get/display product collection in phtml file.



          Check below code:



          <?php
          namespace VendorModuleBlock;

          class ProductDisplay extends MagentoFrameworkViewElementTemplate

          protected $_productCollectionFactory;

          public function __construct(
          MagentoBackendBlockTemplateContext $context,
          MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
          array $data = []
          )

          $this->_productCollectionFactory = $productCollectionFactory;
          parent::__construct($context, $data);


          public function getProductCollection()

          $collection = $this->_productCollectionFactory->create();
          $collection->addAttributeToSelect('*');
          $collection->setPageSize(5); // fetching only 3 products
          $colletion->setOrder('created_at','DESC'); //sort by date created
          return $collection;




          In your phtml file:



          $productCollection = $block->getProductCollection();
          foreach ($productCollection as $product)
          print_r($product->getData());
          echo "<br>";






          share|improve this answer

























          • please explain how you have used logic for latest products.. i am not getting that

            – Waqar Ali
            May 8 at 9:41











          • updated the answer with sort order

            – magefms
            May 8 at 9:46






          • 1





            thank you its working

            – Waqar Ali
            May 8 at 9:53













          1












          1








          1







          You can create a Block class and inject MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory in the constructor and you can get/display product collection in phtml file.



          Check below code:



          <?php
          namespace VendorModuleBlock;

          class ProductDisplay extends MagentoFrameworkViewElementTemplate

          protected $_productCollectionFactory;

          public function __construct(
          MagentoBackendBlockTemplateContext $context,
          MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
          array $data = []
          )

          $this->_productCollectionFactory = $productCollectionFactory;
          parent::__construct($context, $data);


          public function getProductCollection()

          $collection = $this->_productCollectionFactory->create();
          $collection->addAttributeToSelect('*');
          $collection->setPageSize(5); // fetching only 3 products
          $colletion->setOrder('created_at','DESC'); //sort by date created
          return $collection;




          In your phtml file:



          $productCollection = $block->getProductCollection();
          foreach ($productCollection as $product)
          print_r($product->getData());
          echo "<br>";






          share|improve this answer















          You can create a Block class and inject MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory in the constructor and you can get/display product collection in phtml file.



          Check below code:



          <?php
          namespace VendorModuleBlock;

          class ProductDisplay extends MagentoFrameworkViewElementTemplate

          protected $_productCollectionFactory;

          public function __construct(
          MagentoBackendBlockTemplateContext $context,
          MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
          array $data = []
          )

          $this->_productCollectionFactory = $productCollectionFactory;
          parent::__construct($context, $data);


          public function getProductCollection()

          $collection = $this->_productCollectionFactory->create();
          $collection->addAttributeToSelect('*');
          $collection->setPageSize(5); // fetching only 3 products
          $colletion->setOrder('created_at','DESC'); //sort by date created
          return $collection;




          In your phtml file:



          $productCollection = $block->getProductCollection();
          foreach ($productCollection as $product)
          print_r($product->getData());
          echo "<br>";







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 8 at 9:46

























          answered May 8 at 9:39









          magefmsmagefms

          3,0502630




          3,0502630












          • please explain how you have used logic for latest products.. i am not getting that

            – Waqar Ali
            May 8 at 9:41











          • updated the answer with sort order

            – magefms
            May 8 at 9:46






          • 1





            thank you its working

            – Waqar Ali
            May 8 at 9:53

















          • please explain how you have used logic for latest products.. i am not getting that

            – Waqar Ali
            May 8 at 9:41











          • updated the answer with sort order

            – magefms
            May 8 at 9:46






          • 1





            thank you its working

            – Waqar Ali
            May 8 at 9:53
















          please explain how you have used logic for latest products.. i am not getting that

          – Waqar Ali
          May 8 at 9:41





          please explain how you have used logic for latest products.. i am not getting that

          – Waqar Ali
          May 8 at 9:41













          updated the answer with sort order

          – magefms
          May 8 at 9:46





          updated the answer with sort order

          – magefms
          May 8 at 9:46




          1




          1





          thank you its working

          – Waqar Ali
          May 8 at 9:53





          thank you its working

          – Waqar Ali
          May 8 at 9:53

















          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%2f273805%2fhow-to-get-latest-products-collection-in-my-costum-module-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

          Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

          Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

          Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form