0

Here is code but not getting collection

namespace [Vendor]\RecentlyViewed\Block\Customer;

use \Magento\Catalog\Block\Product\Context;
use \Magento\Catalog\Model\Product\Visibility;
use \Magento\Customer\Model\Session as CustomerSession;
use \Magento\Reports\Block\Product\Viewed;
use \Magento\Reports\Model\Product\Index\Factory as IndexFactory;

class RecentlyViewed extends Viewed
{

    /**
     * @var \Magento\Customer\Model\Session
     */
    private $customerSession;


    /**
     * @param \Magento\Customer\Model\Session $customerSession
     * @param \Magento\Catalog\Block\Product\Context $context
     * @param \Magento\Catalog\Model\Product\Visibility $productVisibility
     * @param \Magento\Reports\Model\Product\Index\Factory $indexFactory
     * @param array $data
     */
    public function __construct
    (
        CustomerSession $customerSession,
        Context $context,
        Visibility $productVisibility,
        IndexFactory $indexFactory,
        array $data = [])
    {
        parent::__construct($context, $productVisibility, $indexFactory, $data);
        $this->customerSession = $customerSession;
    }


    /**
     * @return \Magento\Reports\Model\ResourceModel\Product\Index\Collection\AbstractCollection
     */
    public function getRecentlyViewedProducts()
    {
        $collection = $this->getItemsCollection();
    echo '<pre>';
    print_r($collection->getData());
    exit;
        return $collection;
    }

}
0

We need to load the collection.

$this->getItemsCollection()->load();

There is a note here: $this->getItemsCollection() doesn't load the collection. But:

1) Foreach the collection items will load the collection

\Magento\Framework\Data\Collection::getIterator()

public function getIterator()
{
    $this->load();
    return new \ArrayIterator($this->_items);
}

2) Count the collection items will load the collection as well: $this->getItemsCollection()->count().

\Magento\Framework\Data\Collection::count()

public function count()
{
    $this->load();
    return count($this->_items);
}
| improve this answer | |
0

Here is solution.

namespace Test\Viewproduct\Block;

use Magento\Framework\View\Element\Template;

/**
 * Main contact form block
 */
class Viewproduct extends Template{

    protected $_recentlyViewed; 
    protected $_productloader;
    protected $storeManager;  
     /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */

    public function __construct(Template\Context $context,
         \Magento\Reports\Block\Product\Viewed $recentlyViewed,
         \Magento\Catalog\Model\ProductFactory $productloader,
         \Magento\Store\Model\StoreManagerInterface $storeManager,
         array $data = [])
    {
        $this->_recentlyViewed = $recentlyViewed;
        $this->_productloader = $productloader;
        $this->storeManager = $storeManager;
        parent::__construct($context, $data);
    } 
    public function getMostRecentlyViewed()
    {
        return  $this->_recentlyViewed->getItemsCollection();
    }
    public function getProductById($id)
    {
        return $this->_productloader->create()->load($id);

    }
    public function getMediaUrl()
    {
        $currentStore = $this->storeManager->getStore();
        $mediaUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
        return $mediaUrl;
    }
}

phtml

<div style="margin-bottom: 0;" class="owl-button-top above-padding below-padding block widget block-new-products grid">
        <div class="page-title-main">
            <strong role="heading" aria-level="2">Recently View Product</strong>
        </div>
        <div class="block-content">
            <!-- new_products_content_widget_grid-->
            <div class="products-grid grid">
            <div class="slider-outer owl-carousel">
              <ol style="margin-bottom: 0px; " class="product-widget-slider products list items product-items widget-new-grid owl-carousel owl-theme">
              <?php foreach ($viewProduct as $value)  {
                 $p_id=$value->getProductId();
                  $items=$block->getProductById($p_id);

               ?> 
              <div class="owl-item" >
          <li class="product-item product-slider" style="display: list-item;">
                                  <div class="product-item-info">
                                      <div class="product-img-main">
                                            <a href="<?php /* @escapeNotVerified */ echo $items->getProductUrl(); ?>" class="product-item-photo">

                  <span class="product-image-container" style="width:350px;">
                        <span class="product-image-wrapper" style="padding-bottom: 127.42857142857%;">
                          <img class="product-image-photo" src="<?php echo $media.'catalog/product'.$items->getImage();?>" alt="Reprehenrit voluptate " height="446" width="350">
                    </span>
                    </span>
                                            </a>
                                            <div class="product-item-inner">
                                                        <div class="product actions product-item-actions">
                                                <div class="actions-secondary" data-role="add-to-links">
                                                            <a href="#" data-post='<?php  echo $this->helper('Magento\Wishlist\Helper\Data')->getAddParams($items); ?>' class="action towishlist" data-action="add-to-wishlist"
                                                              title="Add to Wish List"
                                                            ><span>' .__('Add to Wish List') . '</span></a>


                                                            <?php $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare'); ?>
                                                        <a href="#" class="action tocompare"
                                                           data-post='<?php /* @escapeNotVerified */ echo $compareHelper->getPostDataParams($items); ?>'
                                                           title="<?php /* @escapeNotVerified */ echo __('Add to Compare') ?>">
                                                            <span><?php /* @escapeNotVerified */ echo __('Add to Compare') ?></span>
                                                        </a>

                                                  </div>
                                            </div>
          </div>
                                </div>

                                <div class="product-item-details">
                                    <strong class="product-item-name">
                                        <a title="Reprehenrit voluptate " href="<?php /* @escapeNotVerified */ echo $items->getProductUrl(); ?>" class="product-item-link">
                                               <?php echo $items->getName(); ?>                                         </a>
                                    </strong>
                                    <div class="price-box price-final_price" data-role="priceBox" data-product-id="10">
                <span class="price-container price-final_price tax weee">
                        <span id="old-price-10-widget-new-grid" data-price-amount="169" data-price-type="finalPrice" class="price-wrapper ">
                      <span class="price"><?php echo $items->getPrice(); ?></span>
                    </span>
                      </span>
                </div>
                                <div class="product-item-inner">
                                            <div class="product-item-actions">
                                               <div class="actions-primary">
                                               <?php if ($items->isSaleable()): ?>
                        <?php $postParams = $block->getAddToCartPostParams($items); ?>

                        <form data-role="tocart-form" action="<?php /* @escapeNotVerified */ echo $postParams['action']; ?>" method="post">
                            <input type="hidden" name="product" value="<?php /* @escapeNotVerified */ echo $postParams['data']['product']; ?>">
                            <input type="hidden" name="<?php /* @escapeNotVerified */ echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php /* @escapeNotVerified */ echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
                            <?php echo $block->getBlockHtml('formkey')?>
                            <?php $storeManager = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Store\Model\StoreManagerInterface'); ?>
                                <button type="submit"
                                        title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
                                        class="action tocart primary">
                                    <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
                                </button>
                        </form>
                        <?php else: ?>
                                <?php if ($items->getIsSalable()): ?>
                                                                <div class="stock available"><span><?php /* @escapeNotVerified */ echo __('In stock') ?></span></div>
                                                            <?php else: ?>
                                                                <div class="stock unavailable"><span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span></div>
                                                                    <?php endif; ?>
                                                                <?php endif; ?>

                    </div>
                                        </div>
                                       </div>
                                    </div>
                            </div>
      </li>
      </div>
      <?php }?>

              </ol>
            </div>
          </div>
        </div>

    </div>
| improve this answer | |

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.