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?

3 Answers 3

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());
1
  • Thanks, very uncomplicated solution that worked out-of-the-box even though direct use of ObjectManager is discouraged
    – cjohansson
    Jan 8, 2018 at 13:11
2

If you want to show product view of current product in product view page then,

i have simple and easy sollution, and you can place it in PHTML file,

/*get current product*/
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');

/*get product view*/
$id = $product->getId();
$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) {
            echo (int) $_product['views'];
        }
    }
}

Thank You @leo . work like a magic Thanks

Answer By Sandip Patel Suggested and help From leo.

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).

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.