5

I would like to load product view hit count for a specific product , i found that Magento database stores some counting in table e.g.

report_viewed_product_aggregated_daily
report_viewed_product_aggregated_monthly

How can I load this value for showing in frontend? Do I need to set date range on it?

Updates:

I try to use:

$collection = $objectManager->create('\Magento\Reports\Model\ResourceModel\Report\Product\Viewed\Collection')  

to get the collection according to what it can be done on Magento 1.

Magento\Reports\Model\ResourceModel\Report\Product\Viewed\Collection

is extending:

\Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection

which is extending:

\Magento\Sales\Model\ResourceModel\Report\Collection\AbstractCollection

where there is a method setDateRange(), but when I call it, it shows error "unknown method". Anyone have any idea about this?

4

For any product, you can set the product_id to the report collection

/**
 * @param int $id
 *
 * @return mixed
 */
public function getProductCount($id)
{
    /**
     * @var \Magento\Catalog\Model\Product\Interceptor $product
     */
    //Get Object Manager Instance
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    //Load product by product id
    $productObj = $objectManager->create('Magento\Catalog\Model\Product')->load($id);
    $productcollection = $objectManager->create('\Magento\Reports\Model\ResourceModel\Product\Collection');
    $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;
}

And then in your template phtml , use it by

$blockObj= $block->getLayout()->createBlock('Mymodule\Viewcount\Block\Viewcount');
                                echo $blockObj->getProductCount($_product->getId());
  • Thanks, very uncomplicated solution that worked out-of-the-box even though direct use of ObjectManager is discouraged – cjohansson Jan 8 '18 at 13:11
1

This should get you the Magento2 version of the code that MagickVishal posted.

class ProductViewCount extends \Magento\Catalog\Block\Product\View\AbstractView { 
 /**
 * @var CollectionFactory
 */
protected $_productRepo;


/**
 * @var \Magento\Store\Model\StoreManagerInterface
 */
protected $_storeManager;

/**
 * @var \Magento\Reports\Model\ResourceModel\Product\Collection
 */
protected $_prodCollection;


/**
 * ProductViewCount constructor.
 *
 * @param \Magento\Catalog\Block\Product\Context $context
 * @param \Magento\Catalog\Model\ProductRepository $productRepo
 * @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
 * @param \Magento\Reports\Model\ResourceModel\Product\Collection $prodCollection
 * @param array $data
 */
public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Catalog\Model\ProductRepository $productRepo,
    \Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
    \Magento\Reports\Model\ResourceModel\Product\Collection $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 \Magento\Catalog\Model\Product\Interceptor $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;
}}

If you want to add a date range for the views, yo would add them to the addViewsCount() function: addViewsCount($from, $to).

1

This is working for Magento 1 you need to convert this for Magento 2:

$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');
        }
    }
}
  • Can i use $collection = $objectManager->create('\Magento\Reports\Model\ResourceModel\Report\Product\Viewed\Collection') to get the collection? But after i found it is extending \Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection, and then in its AbstractCollection, there is a method setDateRange(), but when i call it , it shows error "unknown method". Do you have any idea abt this? – leo Aug 1 '16 at 3:26
  • i have converted it to the magento2 version. There is a bit difference, you can refer to my answer, thanks. – leo Feb 22 '17 at 4:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.