代码之家  ›  专栏  ›  技术社区  ›  Rahul Harshad

如何在客户帐户My Orders中获取所有订单收款

  •  0
  • Rahul Harshad  · 技术社区  · 7 年前

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

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

    我试着让客户参加会议 Magento_Sales/templates/order/history.phtml 页面,但它不工作。

    是否有任何方法可以将输入的搜索框值传递给order对象?

    或加载客户订单集合?

    2 回复  |  直到 7 年前
        1
  •  0
  •   Pallavi    7 年前

    您需要覆盖几个文件。您可以尝试以下代码,它应该可以工作。 对于视图访问,替代 canView() 中定义的函数 Magento\Sales\Controller\AbstractController\OrderViewAuthorization.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> . '%']
                    );
                }
    

    最后添加以下行

    return $this->orders;
    

    接下来,您需要覆盖历史记录。phtml并在表单中添加搜索框。您可以设置如下操作

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

    希望这有帮助!!

        2
  •  0
  •   Rahul Harshad    7 年前

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

    $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 从文本框中获取所需的搜索顺序值。