Magento通过邮箱获取订单信息

Magento通过邮箱获取订单信息
Magento通过邮箱获取订单信息

magento的账户是基于邮箱的,通过邮箱基本可以获取账户的所有信息,当然也包含订单信息。

本例为使用email邮箱获取此账户的订单信息,并且返回Json串。适合使用URL或者post请求来获取某个用户的订单信息。代码如下

<?php
$customer_email = Mage::app()->getRequest()->getParam('email');
?>
<?php
$orders = Mage::getResourceModel('sales/order_collection')
            ->addFieldToSelect('*')
            ->addFieldToFilter('customer_email', $customer_email)
            ->addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()))
            ->setOrder('created_at', '')
        ;
?>
<?php
$i = 0;
 ?>
<?php foreach ($orders as $_order): ?>
<?php

$order_list[$i][OrderId] = $_order->getRealOrderId();
// echo $_order->getRealOrderId();

$order_list[$i][GrandTotal] = $_order->getGrandTotal();
// echo $_order->formatPrice($_order->getGrandTotal());

$order_list[$i][CreatedAt] = $_order->getCreatedAt();
//2011-09-04 14:46:05

$order_list[$i][UpdatedAt] = $_order->getUpdatedAt();

$order_list[$i][status] = $_order->getStatus();
// echo $_order->getStatusLabel();

//$order_list[$i][Name] = $_order->getShippingAddress() ? $this->htmlEscape($_order->getShippingAddress()->getName()) : NULL ;
 $i++;
// echo $_order->getShippingAddress() ? $this->htmlEscape($_order->getShippingAddress()->getName()) : '&nbsp;' ?>

<?php endforeach; ?>

<?php //print_r($orders); ?>
<?php //print_r($order_list); ?>
<?php echo json_encode($order_list); ?>

此为测试代码,请自己精简。
此代码开始为通过url获取email地址。
magento获取get,post值得方法见:http://magento.myurbandata.com/2011/01/28/get-and-post-method-in-magento/

//How to get and post values in magento

//Get Method:

Mage::app()->getRequest()->getParam();

Mage::app()->getRequest()->getParam(‘id’);

//Post Method:

Mage::app()->getRequest()->getPost();

Mage::app()->getRequest()->getPost(‘name’);

然后通过此邮件获取订单,关键代码为:

addFieldToFilter('customer_email', $customer_email)

告诉magento要在众多的订单中筛选此邮箱的订单。

 

转载表明出处:www.hellokeykey.com