Guest User

Untitled

a guest
Jan 16th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. report_viewed_product_aggregated_daily
  2. report_viewed_product_aggregated_monthly
  3.  
  4. $collection = $objectManager->create('MagentoReportsModelResourceModelReportProductViewedCollection')
  5.  
  6. MagentoReportsModelResourceModelReportProductViewedCollection
  7.  
  8. MagentoSalesModelResourceModelReportBestsellersCollection
  9.  
  10. MagentoSalesModelResourceModelReportCollectionAbstractCollection
  11.  
  12. /**
  13. * @param int $id
  14. *
  15. * @return mixed
  16. */
  17. public function getProductCount($id)
  18. {
  19. /**
  20. * @var MagentoCatalogModelProductInterceptor $product
  21. */
  22. //Get Object Manager Instance
  23. $objectManager = MagentoFrameworkAppObjectManager::getInstance();
  24. //Load product by product id
  25. $productObj = $objectManager->create('MagentoCatalogModelProduct')->load($id);
  26. $productcollection = $objectManager->create('MagentoReportsModelResourceModelProductCollection');
  27. $productcollection->setProductAttributeSetId($productObj->getAttributeSetId());
  28. $prodData = $productcollection->addViewsCount()->getData();
  29.  
  30. if (count($prodData) > 0) {
  31. foreach ($prodData as $product) {
  32. if ($product['entity_id'] == $id) {
  33. return (int) $product['views'];
  34. }
  35. }
  36. }
  37.  
  38. return 0;
  39. }
  40.  
  41. $blockObj= $block->getLayout()->createBlock('MymoduleViewcountBlockViewcount');
  42. echo $blockObj->getProductCount($_product->getId());
  43.  
  44. class ProductViewCount extends MagentoCatalogBlockProductViewAbstractView {
  45. /**
  46. * @var CollectionFactory
  47. */
  48. protected $_productRepo;
  49.  
  50.  
  51. /**
  52. * @var MagentoStoreModelStoreManagerInterface
  53. */
  54. protected $_storeManager;
  55.  
  56. /**
  57. * @var MagentoReportsModelResourceModelProductCollection
  58. */
  59. protected $_prodCollection;
  60.  
  61.  
  62. /**
  63. * ProductViewCount constructor.
  64. *
  65. * @param MagentoCatalogBlockProductContext $context
  66. * @param MagentoCatalogModelProductRepository $productRepo
  67. * @param MagentoFrameworkStdlibArrayUtils $arrayUtils
  68. * @param MagentoReportsModelResourceModelProductCollection $prodCollection
  69. * @param array $data
  70. */
  71. public function __construct(
  72. MagentoCatalogBlockProductContext $context,
  73. MagentoCatalogModelProductRepository $productRepo,
  74. MagentoFrameworkStdlibArrayUtils $arrayUtils,
  75. MagentoReportsModelResourceModelProductCollection $prodCollection,
  76. array $data = []
  77. ) {
  78. $this->_productRepo = $productRepo;
  79. $this->_prodCollection = $prodCollection;
  80. $this->_storeManager = $context->getStoreManager();
  81. parent::__construct($context, $arrayUtils, $data);
  82. }
  83.  
  84. /**
  85. * @param int $id
  86. *
  87. * @return mixed
  88. */
  89. public function getProductCount($id)
  90. {
  91. /**
  92. * @var MagentoCatalogModelProductInterceptor $product
  93. */
  94. $prodData = $this->_prodCollection->addViewsCount()->getData();
  95.  
  96. if (count($prodData) > 0) {
  97. foreach ($prodData as $product) {
  98. if ($product['entity_id'] == $id) {
  99. return (int) $product['views'];
  100. }
  101. }
  102. }
  103.  
  104. return 0;
  105. }}
  106.  
  107. $id = $_helper->productAttribute($_product, $_product->getId(), 'id');
  108. $fromDate = '2013-12-10';
  109. $toDate = now();
  110. $viewedProducts = Mage::getResourceModel('reports/product_collection')->addViewsCount($fromDate, $toDate);
  111. if (count($viewedProducts) > 0) {
  112. foreach ($viewedProducts as $product) {
  113. if ($product->getData('entity_id') == $id) {
  114. echo "Total View Count: " . $product->getData('views');
  115. }
  116. }
  117. }
Add Comment
Please, Sign In to add comment