1

If anyone could help me.

My curent value that I get is '0' in my controller:

   foreach($productCollection  as $product)     
   {
        $_helper_views =$this->helper_views->getProductCount($id);
   }

and in my helper:

    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;
}

and Yes this product have views. I checked in report_viewed_product_aggregated_monthly and report_viewed_product_aggregated_weekly tables.

When I change return to:

return $prodData;

I get full array of data.

0

you can set the product_id to the report collection

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;
}

You can Apply This Login Your Code

reference Link

  • I think I checked this and had the same result. Should I put this in helper? I will try one more time. – BartZalas May 21 at 8:43
  • yes put in helper and check – Rk Rathod May 21 at 8:44
  • its return 0 o whatever i put in return. – BartZalas May 21 at 9:26
  • please share your code – Rk Rathod May 21 at 9:29
  • I just change my helper function to your from this link. – BartZalas May 21 at 9:36
0

Finally I get proper value

I change in helper return to:

return $prodData;

and in Controller:

$_helper_views =$this->helper_views->getProductCount($product['entity_id']);

I'm also using different construct than here so I will later paste full code of my helper that's works on Magento 2.3.

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.