skip to Main Content

I want to add Order search functionality in customer account My Orders section. As if customer have many orders so they dont need to navigate instead of they can search by orders like this –

http://demo.dckap.com/m2/sales/order/history/

I tried to get customer session in Magento_Sales/templates/order/history.phtml page but its not working.

Is there any way we can pass input search box value to order object ?

Or Load customer orders collection ?

2

Answers


  1. Chosen as BEST ANSWER

    This is my solution to load logged In customer orders collection -

    $objectManager = MagentoFrameworkAppObjectManager::getInstance();
    $resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
    $connection = $resource->getConnection();
    $customerSession = $objectManager->create('MagentoCustomerModelSession');
    if ($customerSession->isLoggedIn()) {
        $customer_id = $customerSession->getCustomer()->getId();
        $order_collection = $objectManager->create('MagentoSalesModelOrder')->getCollection()->addAttributeToFilter('customer_id', $customer_id)->addAttributeToFilter('increment_id', array('eq' => $orderId));
    }
    

    Then after that I have replace order collection with my order collection $order_collection to get desired search order value from text box.


  2. you need to override few files. you can try with below code, it should work.
    for view access, override canView() function defined in MagentoSalesControllerAbstractControllerOrderViewAuthorization.php

    //overide getOrders() method in history.php block file as below
    // to get customer id from customer session
    if (!($customerId = $this->_customerSession->getCustomerId())) {
                return false;
            }
    
    // to load orders for customer
     $this->orders = $this->_orderCollectionFactory->create()->addFieldToFilter('customer_id', array('eq' => $customerId));
    
    //to add filter for search
    if (!empty(<yoursearchterm>)) {
                    $this->orders->addFieldToFilter(
                            'entity_id', ['like' => '%' . <yoursearchterm> . '%']
                    );
                }
    

    and finally add below line

    return $this->orders;
    

    Next you will need to override history.phtml and add your search boxes inside form. you can set action like below

     action="<?php echo $block->getUrl('sales/order/history'); ?>"
    

    Hope this helps!!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search