2016-07-05 59 views
2

我是magento2的初學者,我會嘗試檢索所有訂單相關的信息 信息和該信息只是想顯示在管理端 像訂單的ID,訂單狀態,商店名稱,訂單日期, 支付方式等 在這裏,我嘗試獲取訂單信息,如訂單ID,訂單狀態,但 頁面將重定向到主頁/儀表板。如何獲得訂單收集對象在magento 2

我使用product Collection對象獲取產品相關信息,但我如何獲取所有訂單信息。

我不知道如何使用訂單集合 對象或訂單儲存庫對象以及訂單信息顯示 在管理端在magento2中。

這是我的觀點或info.phtml文件是:

<?php  $_order = $block->getOrder(3); 
$orderAdminDate = $block->formatDate(
$block->getOrderAdminDate($_order->getCreatedAt()), 
\IntlDateFormatter::MEDIUM, true); 
echo 'Order Status = '.$_order->getStatusLabel(); 
echo 'Order Id = '. $_order->getRealOrderId(); ?> 

這裏是我的Info.php塊文件是:

class Info extends \Magento\Sales\Block\Adminhtml\Order\AbstractOrder 
{ 
public function __construct(
\Magento\Backend\Block\Template\Context $context, 
\Magento\Sales\Model\Order\Address\Renderer $addressRenderer, 
array $data = [] 
) { $this->addressRenderer = $addressRenderer; 
parent::__construct($context, $adminHelper, $data); 
} 
public function getOrder() 
{ return $this->_coreRegistry->registry('current_order'); } } 

回答

2

In your Block file

namespace "Your Module namespace"; 
class modelclass extends \Magento\Framework\View\Element\Template { 
    /** @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory */ 
    protected $_orderCollectionFactory; 
    /** @var \Magento\Sales\Model\ResourceModel\Order\Collection */ 
    protected $orders; 
    public function __construct(
\Magento\Framework\View\Element\Template\Context $context, \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory, array $data = [] 
) { 
    $this->_orderCollectionFactory = $orderCollectionFactory;  
    parent::__construct($context, $data);   
} 

public function getOrders() {   
     if (!$this->orders) 
     { 
      $this->orders = $this->_orderCollectionFactory->create()->addFieldToSelect('*'); 
     } 
     return $this->orders; 
} 

In your phtml file

$_orders = $block->getOrders(); 
if ($_orders && count($_orders)) { 
$complete = $pending = $closed = $canceled = $processing = $onHold = 0; 
foreach ($_orders as $_order) { 
$label = $_order->getStatusLabel(); 
switch ($label) { 
     case 'Complete' : $complete++; 
      break; 
     case 'Pending' : $pending++; 
      break; 
     case 'Processing' : $processing++; 
      break; 
     case 'Canceled' : $canceled++; 
      break; 
     case 'Closed' : $closed++; 
      break; 
    } 
} 
    echo "Order Status <br>"; 
    echo "Completed Order " . $complete . "<br>"; 
    echo "Pending Order " . $pending . "<br>"; 
    echo "Closed Order " . $closed . "<br>"; 
    echo "Canceled Order " . $canceled . "<br>"; 
    echo "Processing Order" . $processing . "<br>"; 
} 
else{ 
echo "You have no Orders"; 
} 
+0

這不再適用於M2.2。它給出錯誤「未捕獲的錯誤:調用成員函數create()null」 –

4

您可以在下面的代碼

<?php 
namespace 'moduleNameSpace'; 
class ModelClass extends \Magento\Framework\View\Element\Template 
{ 

protected $_orderCollectionFactory; 

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context, 
    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory, 
    array $data = [] 
) { 
    $this->_orderCollectionFactory = $orderCollectionFactory; 

    parent::__construct($context, $data); 
    $this->_isScopePrivate = true; 
} 

protected function _construct() 
{ 
    parent::_construct(); 
    $this->_orderCollectionFactory->create()->addAttributeToSelect('*') 

} 
    public function getSalesOrderCollection(array $filters = []){ 

    return $this->orderCollectionFactory; 
}  
} 
+0

謝謝您的回答。這是工作,但我發現早期的解決方案,這是低於 –

+0

你的getSalesOrderCollection函數中缺少一個foreach。 –

2

GET經歷您的所有訂單信息

public function __construct(
    \Psr\Log\LoggerInterface $_logger, 
    \Magento\Sales\Model\Order $_orderModel 

    ) { 
    $this->_logger = $_logger; 
    $this->_orderModel = $_orderModel; 

    } 
    public function getOrders($store){ 
    $orders = $this->_orderModel->getCollection(); 
    $orders->join(array('item' => 'sales_order_item'), 'main_table.entity_id = item.order_id AND main_table.store_id='.$store.' '); 
    $orders->getSelect()->group('main_table.entity_id'); 
    $orders->getSelect()->order('main_table.created_at DESC'); 
    $this->_logger->info($orders); //find your query in system.log 
    $order_array = array(); 
     foreach($orders as $k=>$order) { 
        $order_array[$k] = array(
         'order_id' => $order->getId(), 
         'order_incremental_id' => $order->getIncrementId(), 
         'order_status' => $order->getStatusLabel(), 
         'order_date' => $order->getCreatedAt(), 
         'customer_name' => $order->getCustomerName() 
         //as your need 
        ); 
     } 
    return json_encode($order_array); 
} 
+0

嗨,** @水龍頭** Thnks爲您的答覆,但它不適合我。給出錯誤。 –

0
<?php 
namespace namespace\modulename\Model\Config\Source; 
use Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory; 
class OrderStatus implements \Magento\Framework\Option\ArrayInterface 
{ 
    protected $options = null; 
    protected $collectionFactory; 

    public function __construct(
      CollectionFactory $collectionFactory 
    ) { 
     $this->collectionFactory = $collectionFactory; 
    } 
    public function toOptionArray() 
    { 
     if ($this->options === null) { 
      $status = $this->collectionFactory->create(); 
      foreach ($status as $stat) { 
       $this->options[] = [ 
        'value' => $stat->getStatus(), 
        'label' => $stat->getLabel(), 
       ]; 
      } 
     } 
     return $this->options; 
    } 
} 
+0

使用此代碼在您的模塊中加載銷售訂單集合。結果將顯示在store-> config中magento2 – chris

0
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$orderDatamodel = $objectManager->get('Magento\Sales\Model\Order')->getCollection(); 
foreach($orderDatamodel as $orderDatamodel1){ 
print_r($orderDatamodel1->getData()); 

} 
+1

最好添加一些澄清,不僅代碼片段,所以其他人可以理解。 – AaoIi

相關問題