1

I need order collection filter from order id.

/**
 * @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
 */
protected $_orderCollectionFactory; 


public function __construct(Template\Context $context,
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
    ) 
{
    $this->_orderCollectionFactory = $orderCollectionFactory;
    parent::__construct($context);
}

public function getMyOrderCollection(){
   $orderIds = array();
    $i = 0;
   $orderCollection = $this->_orderCollectionFactory->create();
   foreach($orderCollection as $order){         
        if($i < 4){
            $orderIds[] = $order->getId();
        }
        $i++;
    }
   return $orderCollection->addAttributeToFilter('entity_id', array('in' => $orderIds ));
}

Phtml file code:

 <div class="block block-dashboard-orders">
    <?php $myOrders = $block->getMyOrderCollection();?>
        <div class="block-title order">
            <strong><?php /* @escapeNotVerified */ echo __('My Orders') ?></strong>
        </div>
        <div class="block-content">
        <?php if (sizeof($myOrders->getItems()) > 0): ?>
            <div class="table-wrapper orders-recent">
                <table class="data table table-order-items recent" id="my-orders-table">
                    <caption class="table-caption"><?php /* @escapeNotVerified */ echo __('Recent Orders') ?></caption>
                    <thead>
                        <tr>
                            <th scope="col" class="col id"><?php /* @escapeNotVerified */ echo __('Order #') ?></th>
                            <th scope="col" class="col date"><?php /* @escapeNotVerified */ echo __('Date') ?></th>
                            <th scope="col" class="col shipping"><?php /* @escapeNotVerified */ echo __('Ship To') ?></th>
                            <th scope="col" class="col total"><?php /* @escapeNotVerified */ echo __('Order Total') ?></th>
                            <th scope="col" class="col status"><?php /* @escapeNotVerified */ echo __('Status') ?></th>
                            <th scope="col" class="col actions"><?php /* @escapeNotVerified */ echo __('Action') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($myOrders as $order): ?>
                            <tr>
                                <td data-th="<?php echo $block->escapeHtml(__('Order #')) ?>" class="col id"><?php /* @escapeNotVerified */ echo $order->getRealOrderId() ?></td>
                                <td data-th="<?php echo $block->escapeHtml(__('Date')) ?>" class="col date"><?php /* @escapeNotVerified */ echo $block->formatDate($order->getCreatedAt()) ?></td>
                                <td data-th="<?php echo $block->escapeHtml(__('Ship To')) ?>" class="col shipping"><?php echo $order->getShippingAddress() ? $block->escapeHtml($order->getShippingAddress()->getName()) : '&nbsp;' ?></td>
                                <td data-th="<?php echo $block->escapeHtml(__('Order Total')) ?>" class="col total"><?php /* @escapeNotVerified */ echo $order->formatPrice($order->getGrandTotal()) ?></td>
                                <td data-th="<?php echo $block->escapeHtml(__('Status')) ?>" class="col status"><?php /* @escapeNotVerified */ echo $order->getStatusLabel() ?></td>
                                <td data-th="<?php echo $block->escapeHtml(__('Actions')) ?>" class="col actions">
                                    <a href="<?php /* @escapeNotVerified */ echo $block->getViewUrl($order) ?>" class="action view">
                                        <span><?php /* @escapeNotVerified */ echo __('View Order') ?></span>
                                    </a>                                
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php else: ?>
            <div class="message info empty"><span><?php /* @escapeNotVerified */ echo __('You have no orders.'); ?></span></div>
        <?php endif; ?>
        </div>
    </div>

In above code $orderIds is array with order id. When I fetch this function in phtml file it gives all order instead of filter collection. When I var_dump($block->getMyOrderCollection()->getData()) it gives filtered data. What is issue? Can anyone help me?

EDIT

I got solution as below

$orderCollection = $this->_orderCollectionFactory->create();
        $orderIds = array();
        foreach($orderCollection as $order){
                $itemCollection = $order->getItemsCollection();
                foreach($itemCollection as $item){
                    if($item->getData('myorder_id') == 1){
                        $orderIds[] = $order->getId();                  
                    }
                }
        }
        $sellerOrders = $this->_orderCollectionFactory->create()->addAttributeToFilter('entity_id', array('in' => $orderIds ));

In above code I have to reuse $this->_orderCollectionFactory->create() but why I need this second time don't know

  • can you add the code from your phtml file and the full code of the block? – Marius Dec 16 '16 at 8:49
  • Added block full code and phtml file code – Prashant Valanda Dec 16 '16 at 8:53
  • 1
    try with <?php foreach ($myOrders as $order): ?> instead of <?php foreach ($myOrders->getData() as $order): ?> – Marius Dec 16 '16 at 8:56
  • Sorry it was my mistake i am checking another way as well so but it not working with $myOrdes It gives all order rather than filter collection – Prashant Valanda Dec 16 '16 at 8:58
  • @Marius If i do var_dump($myOrdes->getData()) it gives me filtered record – Prashant Valanda Dec 16 '16 at 8:59
2

Not sure what problem but solved it by $orderCollection->getAllIds()

 public function getMyOrderCollection(){
   $orderCollection = $this->_orderCollectionFactory->create();
   $i=1;
   foreach($orderCollection->getAllIds() as $order){         
        if($i < 2){
            $orderIds[] = $order;
        }
        $i++;
    }
   return $orderCollection->addAttributeToFilter('entity_id', array('in' => $orderIds ));
    }

or you can try simply

 $orderCollection = $this->_orderCollectionFactory->create()->setPageSize(4);
  • @PrashantValanda why you are not using setPageSize(4). i don't get why you are trying here? – Qaisar Satti Dec 16 '16 at 10:06

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.