Trying to get most viewed product view count value, I need to use it in another method calculations. E.g.(if most viewed product view count value is 100 then I need this value to be returned) What am I missing?
/**
* @var \Magento\Reports\Model\ResourceModel\Product\CollectionFactory
*/
protected $_productCollectionFactory;
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
$connectionName = null
)
{
$this->_storeManager = $storeManager;
$this->_productCollectionFactory = $productCollectionFactory;
parent::__construct($context, $connectionName);
}
/**
* Viewed Product Data
* @param null|string|bool|int|Store $storeId
*/
public function getRecentViewedCollection($storeId){
/*@var $store Store*/
$store = $this->_storeManager->getStore($storeId);
if(!$store){
return false;
}
$collection = $this->_productCollectionFactory->create()
->addAttributeToSelect('views')
->addViewsCount($storeId)
->setStoreId($storeId)
->setOrder('views ' . self::SORT_ORDER_DESC)
->setPageSize(1);
foreach ($collection as $product){
$product;
}
return $this;
}
Now _data on Xdebug return this:
views = "3"
entity_id = "163"
attribute_set_id = "9"
etc..
Tried these in foreach:
$product->getViews();
$product->getValue('views');
$product->getData('views');
Didn't worked
What is the final step to get views value in return?