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;
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
add a comment |
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
add a comment |
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
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
magento2 product product-collection
asked May 8 at 9:14
Waqar AliWaqar Ali
6110
6110
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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>";
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>";
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
add a comment |
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>";
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
add a comment |
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>";
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>";
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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