Skip to content

Pager not work in my custom collection?

I have a custom collection for ordered products for a customer.

$connection = $this->resourceConnection->getConnection(MagentoFrameworkAppResourceConnection::DEFAULT_CONNECTION);
    $customerSession = $this->customerSession->create();

    if ($customerSession->isLoggedIn()) {
        $customerId = $customerSession->getId();
        $result1 = $connection->fetchAll('SELECT distinct(product_id) FROM `sales_order_item` as soi INNER join sales_order as so on so.entity_id=soi.order_id WHERE so.customer_id = ' . $customerId);
        if (empty($result1)) {
            $this->messageManager->addWarningMessage(__("You don't have any product purchase yet."));

            $this->response->setRedirect('/customer/account/');
        }
    } else {
        $this->messageManager->addWarningMessage(__("Please login to see collection."));
        $this->response->setRedirect('/customer/account/login');
    }

    if (!empty($result1)) {
        $data = [];
        foreach ($result1 as $result) {
            $data[] = $result['product_id'];
        }
    }

    $collection1 = $this->productCollection->create();
    if (!empty($data)) {
        $collection1->addFieldToFilter('entity_id', ['in' => $data]);
        $collection1->addAttributeToSelect('*');
    }

    return $collection1;

I am calling this collection in my block class like below.

protected function _prepareLayout()
{
    $this->pageConfig->getTitle()->set(__(' Previously Bought Items'));
    parent::_prepareLayout();
    $page_size = $this->getPagerCount();
    $limit = ($this->getRequest()->getParam('limit')) ? $this->getRequest()->getParam('limit') : $this->getMinumRecord();
    $page_data = $this->getCustomData();
    if ($this->getCustomData()) {
        $pager = $this->getLayout()->createBlock(
            MagentoThemeBlockHtmlPager::class,
            'custom.custompager.name'
        )
            ->setAvailableLimit($page_size)
            ->setLimit($limit)
            ->setShowPerPage(true)
            ->setCollection($page_data);
        $this->setChild('pager', $pager);
        $this->getCustomData()->load();
    }
    return $this;
}

public function getCustomData()
{
    // get param values
    $page = ($this->getRequest()->getParam('p')) ? $this->getRequest()->getParam('p') : 1;
    $pageSize = ($this->getRequest()->getParam('limit')) ? $this->getRequest()->getParam('limit') : 1; // set minimum records
    // get custom collection
    $collection = $this->CustomCollection->getProductCollection();
    $collection->setPageSize($pageSize);
    $collection->setCurPage($page);
    return $collection;
}

public function getPagerCount()
{
    // get collection
    $minimum_show = 10; // set minimum records

    $page_array = [];
    $list_data = $this->bsLayer->getProductCollection();
    $list_count = ceil(count($list_data->getData()));
    $show_count = $minimum_show + 1;
    if (count($list_data->getData()) >= $show_count) {
        $list_count = $list_count / $minimum_show;
        $page_nu = $total = $minimum_show;
        $page_array[$minimum_show] = $minimum_show;
        for ($x = 0; $x <= $list_count; $x++) {
            $total = $total + $page_nu;
            $page_array[$total] = $total;
        }
    } else {
        $page_array[$minimum_show] = $minimum_show;
        $minimum_show = $minimum_show + $minimum_show;
        $page_array[$minimum_show] = $minimum_show;
    }
    return $page_array;
}

Here my page pagination and everything working fine except show per page filtering.
Do i miss something here?