【问题标题】:How to get all order collection in Customer account My Orders如何在客户帐户我的订单中获取所有订单集合
【发布时间】:2018-07-16 01:13:47
【问题描述】:

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

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

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

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

或加载客户订单集合?

【问题讨论】:

  • 您的搜索正在工作您正在尝试做什么

标签: 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'); ?>"

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 2011-04-21
    • 1970-01-01
    • 2018-06-07
    • 2019-10-09
    相关资源
    最近更新 更多