问题
I need to retrieve an order from Magento by its id. How do I load a specific order by id?
so far i have tried this:
<?php
$orderNumber = 145000013;
$order = Mage::getModel('sales/order')->loadByIncrementId($orderNumber);
// get order item collection
$orderItems = $order->getItemsCollection();
foreach ($orderItems as $item){
$product_id = $item->product_id;
$product_sku = $item->sku;
$product_name = $item->getName();
$_product = Mage::getModel('catalog/product')->load($product_id);
$cats = $_product->getCategoryIds();
$category_id = $cats[0];
$category = Mage::getModel('catalog/category')->load($category_id);
$category_name = $category->getName();
echo "orderNumber=".$orderNumber."<br/>";
echo "orderValue=".$orderValue."<br/>";
echo "product_name=".$product_name."<br/>";
echo "product_id=".$product_id."<br/>";
echo "product_sku=".$product_sku."<br/>";
echo "category_id=".$category_id."<br/>";
echo "category_name=".$category_name."<br/><br/>";
}
?>
it works fine for static order...but i want to get dynamically.
回答1:
For this you need to follow the following steps:
- Create a form to search for order details
- You can add your custom code on your desired location.
Then you need to submit your search form on the URL where you want to show the order details. there must be one template file associated with the form submitted URL.
In the Template file you can use your above specified code. but as you want to make it dynamic need to fetch submitted order number using the following way:
$orderId = $this->getRequest()->getParam('search_orderid'); //search_orderid is name of search box
Search Form:
<form id="orderSerchForm" method="post" action="<?php echo $this->getUrl('sales/order/history') ?>">
<input type="text" name="search_order" id="search_order" placeholder="Enter Order ID" />
<input type="submit" />
</form>
回答2:
First you need to create a form that post to a custom controller that include your code as above
Then update your code with
$orderNumber = $this->getRequest()->getParams('field_name');;
$order = Mage::getModel('sales/order')->loadByIncrementId($orderNumber);
See this How to get post data variables after submition a form in magento
来源:https://stackoverflow.com/questions/30994625/how-to-get-order-details-dynamically