magento中根据用户的id获取用户的所有订单以及每个订单中的物品 以及物品的相关属性

时间:2024-10-24 20:34:08

本篇文章是对于已经有了magento基础的人而言,在某个模块的额controller中写任意一个函数。

 public function goodbyeAction() {
for ($customer_id=1; $customer_id<10; $customer_id++) {
// $customer_id=5;//用户的id
$orders = Mage::getResourceModel('sales/order_collection')
->addFieldToSelect('*')
->addFieldToFilter('customer_id', $customer_id)
->addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()))
->setOrder('created_at', 'desc'); //获得当前用户所有的订单
$alldata = $orders->getData();
$ddl = sizeof($alldata);//获取到该用户的所有下单数量 for ($x = 0; $x < $ddl; $x++) { //可以考虑把取消的订单不要了
$sales_order = Mage::getModel('sales/order')->load($alldata[$x]['entity_id']); //$sales_order是某一个订单
foreach ($sales_order->getAllItems() as $item) {//item是某一件物品 $name = $item->getName(); //获取订单产品名
// $xsl = $item->getOrderedQty();//销售量这样获取不到,网上这种方式居多
$sku = $item->getSku();//获取sku
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addAttributeToFilter('sku', $sku)
->setOrder('ordered_qty', 'desc')
->getFirstItem();
$product = $_productCollection;
echo '销售量: '.(int)$product->ordered_qty;//这种方式获取销售量可以
echo '产品名: '.$name;
//echo $xsl;
echo 'sku: '. $sku;
}
}
}
}

  第一层for循环$customer_id从1到10,仅仅是对magento后台是1-10的用户id的用户进行遍历。

基于物品的属性,可能会用到的一些函数

echo $name = $item->getName(); //获取订单产品名
echo $price = $item->getPrice();//获取价格
echo $address= $item->getShippingAddress();//获取地址
echo $sku= $item->getSku();//获取sku
echo $FirstName=$billingAddress->getFirstname();
echo $LastName=$billingAddress->getLastname();
echo $Email=$sales_order->getData('customer_email');
echo $Phone=$billingAddress->getTelephone();
echo $ZipCode=$billingAddress->getPostcode();
echo $company=$billingAddress->getCompany();
echo $Address=$billingAddress->getStreetFull();
echo $City=$billingAddress->getCity();
echo $State=$billingAddress->getRegion();
echo $Country=$billingAddress->getCountry();
echo $option = $item->getProductOptions(); //获取option属性
echo $qty = $item->getQtyOrdered(); //获取订单产品数量
echo $item->getRowTotal();//获取total 针对用户的某一个订单,可以获得的属性:
  $shipping=$sales_order->getShippingDescription();//运送方式
$order = Mage::getModel ( 'sales/order' )->loadByIncrementId ($incrementID);//$incrementID是订单号
  $pay=Mage::helper('payment')->getInfoBlock($order->getPayment())->toHtml();//支付方式
  $status = $sales_order->getStatus();//订单状态
$state = $sales_order->getState();
$Email=$sales_order->getData('customer_email'); //客户的邮件