magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.56k stars 9.32k forks source link

Products image not showing when add ->addViewsCount() in most-viewed products #8095

Closed technoquo closed 5 years ago

technoquo commented 7 years ago

Products image not showing when add ->addViewsCount() in most-viewed products I have added a new module “Most Viewed Products” Block in product view page

public function getViewedProducts() { $storeId = $this->_storeManager->getStore()->getId(); $getViewedProducts = $this->_reportsCollectionFactory->create() ->addAttributeToSelect('*') ->addViewsCount() ->setStoreId($storeId) ->addStoreFilter($storeId);

return $getViewedProducts; } and success.phtml

<?php $products = $this->getViewedProducts(); if ($products->count()) { ?>

Most Viewed Products

<?php } ?>

Is working but Products image not showing when add ->addViewsCount() in most-viewed products

I hope your help, Thanks

Preconditions

Our version Magento CE 2.1.2

Steps to reproduce

for Most View Product create a block Lncnacion\Hello\Block namespace Lncnacion\Hello\Block;

use Magento\Framework\View\Element\Template;

class Hello extends Template {

protected $_coreRegistry = null;

/**

/**

/**

Crate a sample.phtml (magento2\app\code\Lncnacion\Hello\view\frontend\templates)

$products = $this->getViewedProducts();

if ($products->count()) { ?> Most Viewed Products

         <?php  foreach($products as $product) :                        
                  $productUrl   =  $product->getUrlModel()->getUrl($product);                        
                $_imagehelper = $this->helper('Magento\Catalog\Helper\Image');
                $productImage = $_imagehelper->init($product, 'category_page_list')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(400)->getUrl();
            ?>
                               <?php echo $product->getName(); ?>               
          <?php endforeach;?>

Show results all product without AddCountViews , that's ok then when add when add ->addViewsCount() with range date in most viewed products

$today = time(); $yesterday = $today - (606024*1);

$from = date("Y-m-d", $yesterday); $to = date("Y-m-d", $today); $collection = $this->_reportsCollectionFactory->create() ->addAttributeToSelect('*') ->addViewsCount() ->addViewsCount($from,$to) ->setStoreId($storeId) ->addStoreFilter($storeId) ->setPageSize($productCount);

Not showing product when added addViewsCount().

Expected result:

Show most viewed products on homepage

Actual result:

Show blank

That's problem without addViewsCount() yes show products but when add addViewsCount() for most view product not show products.

veloraven commented 7 years ago

@technoquo thank you for your feedback. Please, format this issue according to the Issue reporting guidelines: with steps to reproduce, actual result and expected result. Please, also identify which version of Magento you are running. If you are looking for an advice and discussion please refer to the Community Forums or the Magento Stack Exchange site as GitHub issue tracker is intended for technical issues only.

technoquo commented 7 years ago

Hi, Valoraven, sure I will explain follow with guidelines:

Title: "add ->addViewsCount()"

# Prediction Our version Magento CE 2.1.2

# Reproduce

  1. for Most View Product create a block Lncnacion\Hello\Block

namespace Lncnacion\Hello\Block;

use Magento\Framework\View\Element\Template;

class Hello extends Template {

protected $_coreRegistry = null;

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

/**
 * @param \Magento\Framework\View\Element\Template\Context $context
 * @param  \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $reportsCollectionFactory,
 * @param array $data
 */
public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
     \Magento\Framework\Registry $registry,
     \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $reportsCollectionFactory,
    array $data = []
) {
    $this->_reportsCollectionFactory = $reportsCollectionFactory;
     $this->_coreRegistry = $registry;
    parent::__construct($context, $data);
}

  public function _prepareLayout()
{
    return parent::_prepareLayout();
}

/**
 * Getting most viewed products
 */

public function getViewedProducts() {

   $storeId = $this->_storeManager->getStore()->getId();
   $productCount = 5; 

   $collection = $this->_reportsCollectionFactory->create()
            ->addAttributeToSelect('*')               
            ->setStoreId($storeId)
            ->addStoreFilter($storeId)             
            ->setPageSize($productCount);  
    return $collection;
}

}

  1. Crate a sample.phtml (magento2\app\code\Lncnacion\Hello\view\frontend\templates)

    $products = $this->getViewedProducts();

if ($products->count()) { ?> Most Viewed Products

             <?php  foreach($products as $product) :                        
                      $productUrl   =  $product->getUrlModel()->getUrl($product);                        
                    $_imagehelper = $this->helper('Magento\Catalog\Helper\Image');
                    $productImage = $_imagehelper->init($product, 'category_page_list')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(400)->getUrl();
                ?>
                                   <?php echo $product->getName(); ?>               
              <?php endforeach;?>
  1. Show results all product without AddCountViews , that's ok then when add when add ->addViewsCount() with range date in most viewed products

    $today = time(); $yesterday = $today - (606024*1);

    $from = date("Y-m-d", $yesterday);
    $to = date("Y-m-d", $today);

    $collection = $this->_reportsCollectionFactory->create() ->addAttributeToSelect('*') ->addViewsCount() ->addViewsCount($from,$to)
    ->setStoreId($storeId) ->addStoreFilter($storeId)
    ->setPageSize($productCount);

  2. Not showing product when added addViewsCount().

# Expected result: Show most viewed products on homepage

# Actual result: Show blank

That's problem without addViewsCount() yes show products but when add addViewsCount() for most view product not show products.

aheadwork commented 7 years ago

Any solution?

magento-engcom-team commented 7 years ago

@technoquo, thank you for your report. We've created internal ticket(s) MAGETWO-80087 to track progress on the issue.

keyurshah070 commented 7 years ago

@okorshenko it is working fine in Magento 2.2, may be It was creating an issue on older version @technoquo

miguelbalparda commented 7 years ago

@keyurshah070 I was able to reproduce this in 2.2. Can you double check please?

keyurshah070 commented 7 years ago

@miguelbalparda Still I can not reproduce this on magento 2.2 , I am trying on module router not on home page. You are trying to on home page ? This is my code

 $storeId = $this->_storeManager->getStore()->getId();
        $productCount = 5;
        $today = time();
        $yesterday = $today - (606024*1);
        $from = date("Y-m-d", $yesterday);
        $to = date("Y-m-d", $today);
        $collection = $this->_reportsCollectionFactory->create()
            ->addAttributeToSelect('*')
            ->addViewsCount()
            ->addViewsCount($from,$to)
            ->setStoreId($storeId)
            ->addStoreFilter($storeId)
            ->setPageSize($productCount);
        return $collection;

and here is my phtml code

<?php
$products = $this->getViewedProducts();
?>
<?php  foreach($products as $product) :
$productUrl   =  $product->getUrlModel()->getUrl($product);
$_imagehelper = $this->helper('Magento\Catalog\Helper\Image');
$productImage = $_imagehelper->init($product, 'category_page_list')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(400)->getUrl();
?>

    <?php echo $product->getName(); ?>
    <img src="<?php echo $productImage?>" alt="image" height="100"/>
<?php endforeach;?>
<?php exit;?>
keyurshah070 commented 7 years ago

@miguelbalparda did you get the chance to confirm or my code ?

sdzhepa commented 5 years ago

Due to no activities for a long time, the Issue should be re-verified and Confirmed is it still reproducible on 2.3-develop, 2.2-develop

darshan-khatri-16 commented 5 years ago

I am working on this at #dmcdindia1

m2-assistant[bot] commented 5 years ago

Hi @darshan-khatri-16. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

darshan-khatri-16 commented 5 years ago

Hi, Its working as expected but you need to pass entire date and time in $to like below: $storeId = $this->_storeManager->getStore()->getId(); $productCount = 5; $today = time(); $yesterday = $today - (606024 * 1); $from = date("Y-m-d", $yesterday); $to = date("Y-m-d", $today) . " 23:59:59"; $collection = $this->_reportsCollectionFactory->create() ->addAttributeToSelect('*') ->addViewsCount() ->addViewsCount($from, $to) ->setStoreId($storeId) ->addStoreFilter($storeId) ->setPageSize($productCount); return $collection;