2

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?

2 Answers 2

0

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

  $collection->getSelect()->columns(['count' => new \Zend_Db_Expr('COUNT(*)')])->group('column name');
5
  • ->having( 'COUNT(*) >=? ',$this->getRecentViewedCollection($storeId) ) is this the correct way to use it afterwards in model query?
    – Trenox
    Sep 24, 2018 at 19:22
  • yes you can try @Lukas Sep 25, 2018 at 4:30
  • Unfortunately did not work, it returns product object with id 1 on Xdebug.
    – Trenox
    Sep 25, 2018 at 6:50
  • Have tried what i have suggest? Sep 25, 2018 at 11:46
  • Yes I tried it, I'm sorry but it did not work. Check the update, maybe you know the solution now.
    – Trenox
    Sep 25, 2018 at 12:06
0

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

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.