Hi can you please anyone give me the working code of best seller ,most viewed collection and Wishlist Product collection
I try with multiple codes but unfortunately, i am not getting any result
Index.php
<?php
namespace MPS\Test\Block\Bestproduct;
class Index extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface
{
/**
* Default value for products count that will be shown
*/
const DEFAULT_PRODUCTS_COUNT = 100;
/**
* Products count
*
* @var int
*/
protected $_productsCount;
/**
* @var \Magento\Framework\App\Http\Context
*/
protected $httpContext;
protected $_resourceFactory;
/**
* Catalog product visibility
*
* @var \Magento\Catalog\Model\Product\Visibility
*/
protected $_catalogProductVisibility;
/**
* Product collection factory
*
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
*/
protected $_productCollectionFactory;
/**
* Image helper
*
* @var Magento\Catalog\Helper\Image
*/
protected $_imageHelper;
/**
* @var \Magento\Checkout\Helper\Cart
*/
protected $_cartHelper;
/**
* @param Context $context
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
* @param \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility
* @param \Magento\Framework\App\Http\Context $httpContext
* @param array $data
*/
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Reports\Model\ResourceModel\Report\Collection\Factory $resourceFactory,
\Magento\Reports\Model\Grouped\CollectionFactory $collectionFactory,
\Magento\Reports\Helper\Data $reportsData,
array $data = []
) {
$this->_resourceFactory = $resourceFactory;
$this->_collectionFactory = $collectionFactory;
$this->_reportsData = $reportsData;
$this->_imageHelper = $context->getImageHelper();
$this->_cartHelper = $context->getCartHelper();
parent::__construct($context, $data);
}
/**
* get featured product collection
*/
public function getBestsellerProduct(){
$limit = self::DEFAULT_PRODUCTS_COUNT;
$resourceCollection = $this->_resourceFactory->create('Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection');
$resourceCollection->setPageSize($limit);
return $resourceCollection;
}
}
bestProducts.phtml
<?php $bestSellingCollection = $block->getBestsellerProduct();
echo $bestSellingCollection->count();
print_r($bestSellingCollection->getData());
?>
I got some random product. I deleted all order and products in backend that deleted products showing in Bestseller collection
Most Viewed product
Index.php
<?php
namespace MPS\Test\Block\Mostproduct;
class Index extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface
{
/**
* @var \Magento\Reports\Model\ResourceModel\Product\CollectionFactory
*/
protected $_productsFactory;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productsFactory
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productsFactory,
array $data = []
) {
$this->_productsFactory = $productsFactory;
parent::__construct($context, $data);
}
/**
* Getting most viewed products
*/
public function getCollection()
{
$currentStoreId = $this->_storeManager->getStore()->getId();
$collection = $this->_productsFactory->create()
->addAttributeToSelect(
'*'
)->addViewsCount()->setStoreId(
$currentStoreId
)->addStoreFilter(
$currentStoreId
);
$items = $collection->getItems();
return $items;
}
}
mostProduct.phtml
<?php $mostCollection = $block->getCollection();
echo $mostCollection->count();
?>
I din't get product how i get most view products
How to get all coustmer products wishlist collection
Best Answer
Here's an example to show how to get these collections:
and for the template:
You have to set
cacheable="false"
in the layout xml for this block in order for the wishlist to work. Otherwise you can get the wishlist items via javascript:JSON.parse(window.localStorage.getItem('mage-cache-storage')).wishlist
Refresh the statistics in the admin panel under 'reports' to update the best seller and most viewed items.