如何获取客户账户我的订单中的所有订单集合

How to get all order collection in Customer account My Orders

我想在客户帐户“我的订单”部分添加订单搜索功能。好像客户有很多订单,所以他们不需要导航,而是可以像这样按订单搜索 -

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

我试图在 Magento_Sales/templates/order/history.phtml 页面中获取客户会话,但它不起作用。

有什么方法可以将输入的搜索框值传递给订单对象吗?

或加载客户订单集合?

您需要覆盖几个文件。您可以尝试使用以下代码,它应该可以工作。 对于查看访问,覆盖 Magento\Sales\Controller\AbstractController\OrderViewAuthorization.php

中定义的 canView() 函数
//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> . '%']
                );
            }

最后添加下面一行

return $this->orders;

接下来您需要覆盖 history.phtml 并在表单内添加您的搜索框。您可以像下面这样设置操作

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

希望对您有所帮助!!

这是我加载登录客户订单集合的解决方案 -

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
if ($customerSession->isLoggedIn()) {
    $customer_id = $customerSession->getCustomer()->getId();
    $order_collection = $objectManager->create('Magento\Sales\Model\Order')->getCollection()->addAttributeToFilter('customer_id', $customer_id)->addAttributeToFilter('increment_id', array('eq' => $orderId));
}

然后我将订单集合替换为我的订单集合 $order_collection 以从文本框中获取所需的搜索订单值。