How to get Latest products in Magento 2 programmatically in phtml file ?
Asked
Active
Viewed 2,225 times
2 Answers
4
You can retrieve latest 10 products with the Magento 2 API or Repository
Use API call (from javascript widget):
GET http://<magento_host>/rest/V1/products?
And pass your search criteria as an argument.
searchCriteria[sortOrders][0][field]=updated_at
searchCriteria[pageSize]=10
The full request :
GET http://<magento_host>/rest/V1/products?searchCriteria[sortOrders][0][field]=updated_at&searchCriteria[pageSize]=10
The documentation: http://devdocs.magento.com/guides/v2.1/howdoi/webapi/search-criteria.html
This prevents to manage the logic part which may change in the future Magento 2 release.
Use Repository in you template:
\Magento\Catalog\Api\ProductRepositoryInterface::getList
The complete code :
use Magento\Catalog\Api\ProductRepositoryInterface as ProductRepository;
use Magento\Framework\Api\SortOrder as SortOrder;
/**
* @var ProductRepository
*/
protected $productRepository;
public function __construct(
ProductRepository $productRepository,
) {
$this->productRepository = $productRepository;
}
public function getLatestProducts($limit = 1)
{
/** @var \Magento\Framework\Api\Search\FilterGroup $searchCriteriaGroup */
$searchCriteriaGroup = $objectManager->create('Magento\Framework\Api\Search\FilterGroup');
/** @var \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria */
$sortOrder = $objectManager->create('Magento\Framework\Api\SortOrder');
$sortOrder->setField('updated_at');
$sortOrder->setDirection(SortOrder::SORT_DESC);
$searchCriteria = $objectManager->create('Magento\Framework\Api\SearchCriteriaInterface');
$searchCriteria->setFilterGroups([$searchCriteriaGroup]);
$searchCriteria->setPageSize($limit);
$searchCriteria->setSortOrders([$sortOrder]);
return $this->productRepository->getList($searchCriteria)->getItems();
}
And use dependancy injection to avoid objectManager
Mohamed El Mrabet
- 1,522
- 13
- 23
Franck Garnier
- 159
- 3
1
Complete implementation of getting latest 10 product:
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$abstractProductBlock = $block->getLayout()->createBlock('\Magento\Catalog\Block\Product\AbstractProduct');
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create()
->addAttributeToSelect('*')
->addAttributeToSort('created_at', 'DESC')
->setPageSize(10)
->load();
?>
<div>
<?php foreach ($collection as $product) :?>
<div class="item-box">
<a href="<?php echo $product->getProductUrl(); ?>" >
<span class="imgbx">
<img src="<?php echo $abstractProductBlock->getImage($product, 'latest_collection_list')->getImageUrl(); ?>" alt="<?php echo $product->getName(); ?>" />
</span>
<h3><?php echo $product->getName(); ?></h3>
<span class="hm-price"><?php echo $abstractProductBlock->getProductPrice($product) ?></span>
</a>
</div>
<?php endforeach; ?>
</div>
?>
Arun Karnawat
- 2,948
- 7
- 34
- 62
-
4That will definitely work but I suggest you try to avoid using the ObjectManager directly: http://magento.stackexchange.com/q/117098/2380 – Raphael at Digital Pianism Oct 05 '16 at 09:12