0

I am retrieving data from third party as an object, I am forcefully converting an object into array and collection.

I want to apply pagination with the data, I tried using core pagination logic and setting it into magento 2 but sometimes count doesn't work properly like if no data exists still pagination shows page number dropdown.

Here is the code I have done:

protected function _prepareLayout()
    {
        parent::_prepareLayout();
        $this->pageConfig->getTitle()->set(__('Coupons'));


        if ($this->getCouponBookOrderHistory()) {
            $pager = $this->getLayout()->createBlock(
                'Magento\Theme\Block\Html\Pager',
                'test.news.pager'
            )->setAvailableLimit(array(10=>10,15=>15))->setShowPerPage(true)->setCollection(
                $this->getCouponBookOrderHistoryTotal()
            );
            $this->setChild('pager', $pager);
            $this->getCouponBookOrderHistory()->load();
        }
        return $this;
    }

    public function getPagerHtml()
    {
        return $this->getChildHtml('pager');
    }
    public function getCouponBookOrderHistory()
    {
        $customerId = $this->customer->getCustomer()->getAcwCustomerId(); 
        /*echo '<pre>';
        print_r($customerId);*/
        $result = array();

        $result = $this->coupons->getCouponBooks($customerId);

        //63498
        $result = (array)$result;
        //get values of current page
        $page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
        //get values of current limit
        $pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 10;

        $total = count( $result ); //total items in array    
        $limit = $pageSize; //per page    

        $totalPages = ceil( $total/ $limit ); //calculate total pages
        $page = max($page, 1); //get 1 page when $_GET['page'] <= 0
        $page = min($page, $totalPages); //get last page when $_GET['page'] > $totalPages
        $offset = ($page - 1) * $limit;
        if( $offset < 0 ) $offset = 0;
        $result = array_slice( $result, $offset, $limit );

        $master = array();
        array_push($master, $result);
        $collection=$this->_collectionFactory->create();
        // print_r($master); die;

        foreach($master as $row){
            $varienObject = new \Magento\Framework\DataObject(); 
            $varienObject->setData((array)$row);
            $collection->addItem($varienObject);
        }
        return $collection;
    }
public function getCouponBookOrderHistoryTotal()
    {
        $customerId = $this->customer->getCustomer()->getAcwCustomerId(); 
        /*echo '<pre>';
        print_r($customerId);*/
        $result = array();
        $result = $this->coupons->getCouponBooks($customerId);
        //63498


        $result = (array)$result;
        $master = array();
        array_push($master, $result);
        $collection=$this->_collectionFactory->create();
        // print_r($master); die;

        foreach($master as $row){
            $varienObject = new \Magento\Framework\DataObject(); 
            $varienObject->setData((array)$row);
            $collection->addItem($varienObject);
        }
        return $collection;
    }

In my phtml file:

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

I have created 2 functions getCouponBookOrderHistoryTotal() for fetching total proper count so pagination works and other to fetch data from array using array_slice but I am missing something.

Can anyone help me out how can I directly convert this to apply filters to collection, I know I tried like this:

// $collection->setPageSize($totalPages);
// $collection->setCurPage($page);

but still It didnt applied to filters as well.

1 Answer 1

0

Can you change your below function with below code ?

public function getCouponBookOrderHistory()
{
    $customerId = $this->customer->getCustomer()->getAcwCustomerId(); 
    /*echo '<pre>';
    print_r($customerId);*/
    $result = array();

    $result = $this->coupons->getCouponBooks($customerId);

    //63498
    $result = (array)$result;
    //get values of current page
    $page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
    //get values of current limit
    $pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 10;

    $collection=$this->_collectionFactory->create();
    $collection->setPageSize($pageSize);
    $collection->setCurPage($page);
    return $collection;
}
3
  • Hey I am getting data as an object dont I have to convert to collection?
    – Jaymin
    Jun 11, 2020 at 11:08
  • collection should be in object form always Jun 11, 2020 at 11:12
  • didnt worked but!
    – Jaymin
    Jun 11, 2020 at 14:49

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.