I am a new developer of Magento 2.

I got an array of product items from order.

$products = $order->getAllVisibleItems();

However, getAllVisibleItems() get the array of products. I need to convert the array to collection so that I can render it by pager. what should I do ?


Check how it's done in the \Magento\Sales\Block\Order\Items::_prepareLayout() method.

You create item collection factory and use it as collection for building pager

     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param array $data
     * @param \Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory|null $itemCollectionFactory
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Registry $registry,
        array $data = [],
        \Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory $itemCollectionFactory = null
    ) {
        $this->_coreRegistry = $registry;
        $this->itemCollectionFactory = $itemCollectionFactory ?: \Magento\Framework\App\ObjectManager::getInstance()
        parent::__construct($context, $data);

     * Init pager block and item collection with page size and current page number.
     * @return $this
    protected function _prepareLayout()
        $this->itemsPerPage = $this->_scopeConfig->getValue('sales/orders/items_per_page');

        $this->itemCollection = $this->itemCollectionFactory->create();

        /** @var \Magento\Theme\Block\Html\Pager $pagerBlock */
        $pagerBlock = $this->getChildBlock('sales_order_item_pager');
        if ($pagerBlock) {
            //here pager updates collection parameters

        return parent::_prepareLayout();

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.