0

These are my block methods:

    public function getProductsViewHistory() {
        $page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
        $pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 1;
        $fromDate = date('Y-m-d H:i:s', strtotime("-6 month"));
        $product = $this->customerProductViewHistory->create();
        $product->addFieldToFilter('subject_id',array('eq'=> $this->getLoggedInUserId() ))
        ->addFieldToFilter('logged_at',array('from'=> $fromDate ))
        ->addFieldToSelect('object_id')
        ->getSelect()->group('object_id');
        $product->setPageSize( self::PRODUCTS_PER_PAGE );
        $product->setCurPage($page);
        return $product;
    }     
    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        // $this->pageConfig->getTitle()->set(__('News'));
        if ($this->getProductsViewHistory()) {
            $pager = $this->getLayout()->createBlock(
                'Magento\Theme\Block\Html\Pager',
                'history.pager'
            )->setAvailableLimit(array( self::PRODUCTS_PER_PAGE=>self::PRODUCTS_PER_PAGE))
->setShowPerPage(true)->setCollection( $this->getProductsViewHistory() );
                $this->setChild('pager', $pager);
                $this->getProductsViewHistory()->load();
            }
            return $this;
        }
   public function getPagerHtml()
     {
         return $this->getChildHtml('pager');
     }

And this is my template file.

<?php if ($block->getPagerHtml()): ?>
    <div class="order-products-toolbar toolbar bottom"><?php echo $block->getPagerHtml(); ?></div>
<?php endif ?>

<?php
$collection = $this->getCollection();  
 foreach ($collection as $key => $value) {
    var_dump($value->getData());
 }
?>

I get this error:

Warning: Invalid argument supplied for foreach()

If I comment out the part to show collection, I'm successfully getting pagination as intended.
What am I missing?

  • have you found any solutions yet? – Rakesh Jesadiya Jun 5 '17 at 11:46
  • Exactly not. abdul gave me a solution which I was not looking for. Still looking for the best. – Purushotam Sangroula Jun 5 '17 at 11:50
0

Just Use below syntax,

<?php
$items = $block->getCollection();  
 foreach ($items as $value) {
    var_dump($value->getData());
 }
?>
| improve this answer | |
  • Please let me know if you have still issue. – Rakesh Jesadiya Jun 5 '17 at 5:13
  • No mate , the issue persists. – Purushotam Sangroula Jun 5 '17 at 5:17
  • @Purushotam Actually what do you want to do? – Rakesh Jesadiya Jun 5 '17 at 11:51
  • I want to get the collection set by pagination even if the collection is ordered randomly, I'm confused about the purpose of $this->getProductsViewHistory()->load(); and return $this; – Purushotam Sangroula Jun 5 '17 at 12:05
0

Try this code:

<?php $products = $block->getProductsViewHistory(); ?>
<?php if ($products && count($products)): ?>
<?php if ($block->getPagerHtml()): ?>
    <div class="order-products-toolbar toolbar bottom"><?php echo $block->getPagerHtml(); ?></div>
<?php endif ?>
<?php foreach ($products as $product): ?>
    <?php print_r($product->getData());?>  
<?php endforeach; ?> 

<?php endif ?>
| improve this answer | |
  • Thanks for your answer, I knew this technique. It is a way. But what if the collection I'm fetching is randomly ordered, the result will be different for the case of calling while in _prepareLayout(), and the call from template file. I need the same collection in both function call. – Purushotam Sangroula Jun 5 '17 at 5:23
  • are you need product collection in randomly right? – Abdul Jun 5 '17 at 5:27
  • But above code working fine? – Abdul Jun 5 '17 at 5:28
  • Yes in another task I need this feature on random collection. – Purushotam Sangroula Jun 5 '17 at 5:28
  • Ok. let me check and reply – Abdul Jun 5 '17 at 5:29

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.