本文介绍如何通过按订单ID或订单增量ID加载订单来获取订单信息。
我们将按订单ID以及订单增量ID加载订单。
然后,我们获取以下信息:
-订单细节
-订单项目的信息
-订单的支付信息
订单的帐单地址-
-订单的送货地址
我将展示如何通过两种方式获取订单信息,即
–使用依赖注入(DI)
–使用对象管理器
使用依赖注入(DI)
在此示例中,我使用了一个名为Chapagain_HelloWorld的自定义模块。
这是Block类:
<?php
namespace Chapagain\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
/**
* @var \Magento\Sales\Model\OrderRepository
*/
protected $orderRepository;
/**
* @var \Magento\Framework\Api\SearchCriteriaBuilder
*/
protected $searchCriteriaBuilder;
public function __construct(
\Magento\Sales\Model\OrderRepository $orderRepository,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
\Magento\Backend\Block\Template\Context $context,
array $data = []
)
{
$this->orderRepository = $orderRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
parent::__construct($context, $data);
}
public function getOrderById($id) {
return $this->orderRepository->get($id);
}
public function getOrderByIncrementId($incrementId) {
$this->searchCriteriaBuilder->addFilter('increment_id', $incrementId);
$order = $this->orderRepository
->getList($this->searchCriteriaBuilder->create())
->getItems();
return $order;
}
}
?>
这是模板文件代码:
<?php
$incrementId = '000000051';
$order = $block->getOrderByIncrementId($incrementId);
//var_dump($order);
foreach ($order as $key => $value) {
var_dump($value->getData());
}
$orderId = '47';
$order = $block->getOrderById($orderId);
//var_dump($order->getData());
// Get Order Information
echo $order->getEntityId() . '<br>';
echo $order->getIncrementId() . '<br>';
echo $order->getState() . '<br>';
echo $order->getStatus() . '<br>';
echo $order->getStoreId() . '<br>';
echo $order->getGrandTotal() . '<br>';
echo $order->getSubtotal() . '<br>';
echo $order->getTotalQtyOrdered() . '<br>';
echo $order->getOrderCurrencyCode() . '<br>';
// Get Customer Information from the Order
echo $order->getCustomerId() . '<br>';
echo $order->getCustomerEmail() . '<br>';
echo $order->getCustomerFirstname() . '<br>';
echo $order->getCustomerLastname() . '<br>';
// Get Order Items
$orderItems = $order->getAllItems();
foreach ($orderItems as $item) {
//var_dump($item->getData());
echo $item->getItemId() . '<br>';
echo $item->getOrderId() . '<br>';
echo $item->getStoreId() . '<br>';
echo $item->getProductId() . '<br>';
print_r($item->getProductOptions()) . '<br>';
echo $item->getSku() . '<br>';
echo $item->getName() . '<br>';
echo $item->getQtyOrdered() . '<br>';
echo $item->getPrice() . '<br>';
}
// Get Order Payment
$payment = $order->getPayment();
//var_dump($payment->getData());
echo $payment->getMethod() . '<br>'; // payment method code
echo $payment->getAmountPaid() . '<br>';
echo $payment->getAmountOrdered() . '<br>';
echo $payment->getAdditionalInformation()['method_title'] . '<br>';
// Get Billing Information
$billingAddress = $order->getBillingAddress();
//var_dump($billingAddress->getData());
echo $billingAddress->getFirstname() . '<br>';
echo $billingAddress->getLastname() . '<br>';
echo $billingAddress->getEmail() . '<br>';
echo $billingAddress->getTelephone() . '<br>';
print_r($billingAddress->getStreet()) . '<br>';
echo $billingAddress->getCity() . '<br>';
echo $billingAddress->getRegion() . '<br>';
echo $billingAddress->getCountryId() . '<br>';
// Get Shipping Information
$shippingAddress = $order->getShippingAddress();
//var_dump($shippingAddress->getData());
echo $shippingAddress->getFirstname() . '<br>';
echo $shippingAddress->getLastname() . '<br>';
echo $shippingAddress->getEmail() . '<br>';
echo $shippingAddress->getTelephone() . '<br>';
print_r($shippingAddress->getStreet()) . '<br>';
echo $shippingAddress->getCity() . '<br>';
echo $shippingAddress->getRegion() . '<br>';
echo $shippingAddress->getCountryId() . '<br>';
?>
使用对象管理器
推荐使用上面的代码使用依赖注入(DI)。如果您曾经需要使用对象管理器,则可以尝试以下代码通过按订单ID或增量ID加载订单来获取订单信息。
<?php
$incrementId = '000000051';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderRepository = $objectManager->create('\Magento\Sales\Model\OrderRepository');
$searchCriteriaBuilder = $objectManager->create('\Magento\Framework\Api\SearchCriteriaBuilder');
$searchCriteriaBuilder->addFilter('increment_id', $incrementId);
$order = $orderRepository->getList($searchCriteriaBuilder->create())->getItems();
//var_dump($order);
foreach ($order as $key => $value) {
var_dump($value->getData());
}
$orderId = '47';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId);
//var_dump($order->getData());
// Get Order Information
echo $order->getEntityId() . '<br>';
echo $order->getIncrementId() . '<br>';
echo $order->getState() . '<br>';
echo $order->getStatus() . '<br>';
echo $order->getStoreId() . '<br>';
echo $order->getGrandTotal() . '<br>';
echo $order->getSubtotal() . '<br>';
echo $order->getTotalQtyOrdered() . '<br>';
echo $order->getOrderCurrencyCode() . '<br>';
// Get Customer Information from the Order
echo $order->getCustomerId() . '<br>';
echo $order->getCustomerEmail() . '<br>';
echo $order->getCustomerFirstname() . '<br>';
echo $order->getCustomerLastname() . '<br>';
// Get Order Items
$orderItems = $order->getAllItems();
foreach ($orderItems as $item) {
//var_dump($item->getData());
echo $item->getItemId() . '<br>';
echo $item->getOrderId() . '<br>';
echo $item->getStoreId() . '<br>';
echo $item->getProductId() . '<br>';
print_r($item->getProductOptions()) . '<br>';
echo $item->getSku() . '<br>';
echo $item->getName() . '<br>';
echo $item->getQtyOrdered() . '<br>';
echo $item->getPrice() . '<br>';
}
// Get Order Payment
$payment = $order->getPayment();
//var_dump($payment->getData());
echo $payment->getMethod() . '<br>'; // payment method code
echo $payment->getAmountPaid() . '<br>';
echo $payment->getAmountOrdered() . '<br>';
echo $payment->getAdditionalInformation()['method_title'] . '<br>';
// Get Billing Information
$billingAddress = $order->getBillingAddress();
//var_dump($billingAddress->getData());
echo $billingAddress->getFirstname() . '<br>';
echo $billingAddress->getLastname() . '<br>';
echo $billingAddress->getEmail() . '<br>';
echo $billingAddress->getTelephone() . '<br>';
print_r($billingAddress->getStreet()) . '<br>';
echo $billingAddress->getCity() . '<br>';
echo $billingAddress->getRegion() . '<br>';
echo $billingAddress->getCountryId() . '<br>';
// Get Shipping Information
$shippingAddress = $order->getShippingAddress();
//var_dump($shippingAddress->getData());
echo $shippingAddress->getFirstname() . '<br>';
echo $shippingAddress->getLastname() . '<br>';
echo $shippingAddress->getEmail() . '<br>';
echo $shippingAddress->getTelephone() . '<br>';
print_r($shippingAddress->getStreet()) . '<br>';
echo $shippingAddress->getCity() . '<br>';
echo $shippingAddress->getRegion() . '<br>';
echo $shippingAddress->getCountryId() . '<br>';
?>
希望这可以帮助。谢谢。