Question

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?

Was it helpful?

Solution 2

Figured it out today:

/**
 * @var \Magento\Reports\Model\ResourceModel\Product\CollectionFactory
 */
protected $_productsFactory;

public function __construct(
    \Magento\Framework\Model\ResourceModel\Db\Context $context,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productsFactory,
    $connectionName = null
)
{
    $this->_storeManager = $storeManager;
    $this->_productsFactory = $productsFactory;
    parent::__construct($context, $connectionName);
}

/**
 * Viewed Product Data
 * @param null|string|bool|int|Store $storeId
 * {@inheritdoc}
 */
public function getRecentViewedCollection($storeId){
    /*@var $store Store*/
    $store = $this->_storeManager->getStore($storeId);
    if(!$store){
        return false;
    }

    $product = $this->_productsFactory->create()
        ->addAttributeToSelect('views')
        ->addViewsCount($storeId)
        ->setStoreId($storeId)
        ->setOrder('views ' . self::SORT_ORDER_DESC)
        ->getFirstItem();

    return $product->getViews();
}

This will return number of highest viewCount of product

OTHER TIPS

Before return $collection you have add below line of code:

  $collection->getSelect()->columns(['count' => new \Zend_Db_Expr('COUNT(*)')])->group('column name');
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top