2

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 ?

Amit Bera
77.8k21 gold badges127 silver badges240 bronze badges
asked Sep 29, 2017 at 9:45

1 Answer 1

3

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()
 ->get(\Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory::class);
 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();
 $this->itemCollection->setOrderFilter($this->getOrder());
 $this->itemCollection->filterByParent(null);
 /** @var \Magento\Theme\Block\Html\Pager $pagerBlock */
 $pagerBlock = $this->getChildBlock('sales_order_item_pager');
 if ($pagerBlock) {
 $pagerBlock->setLimit($this->itemsPerPage);
 //here pager updates collection parameters
 $pagerBlock->setCollection($this->itemCollection);
 $pagerBlock->setAvailableLimit([$this->itemsPerPage]);
 $pagerBlock->setShowAmounts($this->isPagerDisplayed());
 }
 return parent::_prepareLayout();
 }
Amit Bera
77.8k21 gold badges127 silver badges240 bronze badges
answered Sep 29, 2017 at 10:14
3
  • This answer’s OP underlying issue, but it doesn’t answer the original question: how to get a collection when one has an array. Commented Apr 1, 2021 at 9:28
  • 1
    @bfontaine check \Magento\Framework\Data\Collection::addItem() method. Just create collection factory, convert array item into the \Magento\Framework\DataObject and add items to collection Commented Apr 2, 2021 at 6:04
  • _____ Thank you! Commented Apr 2, 2021 at 7:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.