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?

2
  • have you found any solutions yet? Commented Jun 5, 2017 at 11:46
  • Exactly not. abdul gave me a solution which I was not looking for. Still looking for the best.
    – P S
    Commented Jun 5, 2017 at 11:50

2 Answers 2

0

Just Use below syntax,

<?php
$items = $block->getCollection();  
 foreach ($items as $value) {
    var_dump($value->getData());
 }
?>
4
  • Please let me know if you have still issue. Commented Jun 5, 2017 at 5:13
  • No mate , the issue persists.
    – P S
    Commented Jun 5, 2017 at 5:17
  • @Purushotam Actually what do you want to do? Commented Jun 5, 2017 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;
    – P S
    Commented Jun 5, 2017 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 ?>
7
  • 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.
    – P S
    Commented Jun 5, 2017 at 5:23
  • are you need product collection in randomly right?
    – Abdul
    Commented Jun 5, 2017 at 5:27
  • But above code working fine?
    – Abdul
    Commented Jun 5, 2017 at 5:28
  • Yes in another task I need this feature on random collection.
    – P S
    Commented Jun 5, 2017 at 5:28
  • Ok. let me check and reply
    – Abdul
    Commented Jun 5, 2017 at 5:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.