【问题标题】:How to get all order collection in Customer account My Orders如何在客户帐户我的订单中获取所有订单集合
【发布时间】:2018-07-16 01:13:47
【问题描述】:
【问题讨论】:
标签:
php
ajax
magento
magento2
【解决方案1】:
这是我加载已登录客户订单集合的解决方案 -
$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,以从文本框中获取所需的搜索订单值。
【解决方案2】:
您需要覆盖几个文件。您可以尝试使用以下代码,它应该可以工作。
对于视图访问,覆盖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'); ?>"
希望这会有所帮助!