Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- report_viewed_product_aggregated_daily
- report_viewed_product_aggregated_monthly
- $collection = $objectManager->create('MagentoReportsModelResourceModelReportProductViewedCollection')
- MagentoReportsModelResourceModelReportProductViewedCollection
- MagentoSalesModelResourceModelReportBestsellersCollection
- MagentoSalesModelResourceModelReportCollectionAbstractCollection
- /**
- * @param int $id
- *
- * @return mixed
- */
- public function getProductCount($id)
- {
- /**
- * @var MagentoCatalogModelProductInterceptor $product
- */
- //Get Object Manager Instance
- $objectManager = MagentoFrameworkAppObjectManager::getInstance();
- //Load product by product id
- $productObj = $objectManager->create('MagentoCatalogModelProduct')->load($id);
- $productcollection = $objectManager->create('MagentoReportsModelResourceModelProductCollection');
- $productcollection->setProductAttributeSetId($productObj->getAttributeSetId());
- $prodData = $productcollection->addViewsCount()->getData();
- if (count($prodData) > 0) {
- foreach ($prodData as $product) {
- if ($product['entity_id'] == $id) {
- return (int) $product['views'];
- }
- }
- }
- return 0;
- }
- $blockObj= $block->getLayout()->createBlock('MymoduleViewcountBlockViewcount');
- echo $blockObj->getProductCount($_product->getId());
- class ProductViewCount extends MagentoCatalogBlockProductViewAbstractView {
- /**
- * @var CollectionFactory
- */
- protected $_productRepo;
- /**
- * @var MagentoStoreModelStoreManagerInterface
- */
- protected $_storeManager;
- /**
- * @var MagentoReportsModelResourceModelProductCollection
- */
- protected $_prodCollection;
- /**
- * ProductViewCount constructor.
- *
- * @param MagentoCatalogBlockProductContext $context
- * @param MagentoCatalogModelProductRepository $productRepo
- * @param MagentoFrameworkStdlibArrayUtils $arrayUtils
- * @param MagentoReportsModelResourceModelProductCollection $prodCollection
- * @param array $data
- */
- public function __construct(
- MagentoCatalogBlockProductContext $context,
- MagentoCatalogModelProductRepository $productRepo,
- MagentoFrameworkStdlibArrayUtils $arrayUtils,
- MagentoReportsModelResourceModelProductCollection $prodCollection,
- array $data = []
- ) {
- $this->_productRepo = $productRepo;
- $this->_prodCollection = $prodCollection;
- $this->_storeManager = $context->getStoreManager();
- parent::__construct($context, $arrayUtils, $data);
- }
- /**
- * @param int $id
- *
- * @return mixed
- */
- public function getProductCount($id)
- {
- /**
- * @var MagentoCatalogModelProductInterceptor $product
- */
- $prodData = $this->_prodCollection->addViewsCount()->getData();
- if (count($prodData) > 0) {
- foreach ($prodData as $product) {
- if ($product['entity_id'] == $id) {
- return (int) $product['views'];
- }
- }
- }
- return 0;
- }}
- $id = $_helper->productAttribute($_product, $_product->getId(), 'id');
- $fromDate = '2013-12-10';
- $toDate = now();
- $viewedProducts = Mage::getResourceModel('reports/product_collection')->addViewsCount($fromDate, $toDate);
- if (count($viewedProducts) > 0) {
- foreach ($viewedProducts as $product) {
- if ($product->getData('entity_id') == $id) {
- echo "Total View Count: " . $product->getData('views');
- }
- }
- }
Add Comment
Please, Sign In to add comment