2011-02-27 153 views
2

我想選擇Magento中所有訂單的列表。magento訂單列表查詢

這是我需要在另一個PHP應用程序中顯示magento所有訂單的列表。

也可以使用Magento的版本1.4.2有人寫信給我使用的Magento約定的代碼,如Mage::

林。

謝謝,
馬克

+0

你到目前爲止嘗試過什麼?另外,如果您接受了一些您提出的問題的答案,那麼您可能會有更多的運氣獲得答案。 – 2011-02-27 20:08:53

回答

10

該代碼使用「Magento的方式」,並訪問通過隔離您從表結構變化的模型層中的數據的主(例如平坦vs EAV)。在Magento安裝的根目錄下創建一個包含此框架代碼的新PHP文件(如果其他地方更新了第一個require語句的路徑)。

這給你一些如何添加屬性到集合的例子,你應該能夠按照例子添加更多的需要。它向您顯示如何按屬性過濾和按屬性排序。用於回顯您需要的字段的示例。

HTH,
JD

require_once 'app/Mage.php'; 
umask(0); 
Mage::app('default'); 
    $orders = Mage::getResourceModel('sales/order_collection') 
     ->addAttributeToSelect('*') 
     ->joinAttribute('billing_firstname', 'order_address/firstname', 'billing_address_id', null, 'left') 
     ->joinAttribute('billing_lastname', 'order_address/lastname', 'billing_address_id', null, 'left') 
     ->joinAttribute('billing_street', 'order_address/street', 'billing_address_id', null, 'left') 
     ->joinAttribute('billing_company', 'order_address/company', 'billing_address_id', null, 'left') 
     ->joinAttribute('billing_city', 'order_address/city', 'billing_address_id', null, 'left') 
     ->joinAttribute('billing_region', 'order_address/region', 'billing_address_id', null, 'left') 
     ->joinAttribute('billing_country', 'order_address/country_id', 'billing_address_id', null, 'left') 
     ->joinAttribute('billing_postcode', 'order_address/postcode', 'billing_address_id', null, 'left') 
     ->joinAttribute('billing_telephone', 'order_address/telephone', 'billing_address_id', null, 'left') 
     ->joinAttribute('billing_fax', 'order_address/fax', 'billing_address_id', null, 'left') 
     ->joinAttribute('shipping_firstname', 'order_address/firstname', 'shipping_address_id', null, 'left') 
     ->joinAttribute('shipping_lastname', 'order_address/lastname', 'shipping_address_id', null, 'left') 
     ->joinAttribute('shipping_street', 'order_address/street', 'shipping_address_id', null, 'left') 
     ->joinAttribute('shipping_company', 'order_address/company', 'shipping_address_id', null, 'left') 
     ->joinAttribute('shipping_city', 'order_address/city', 'shipping_address_id', null, 'left') 
     ->joinAttribute('shipping_region', 'order_address/region', 'shipping_address_id', null, 'left') 
     ->joinAttribute('shipping_country', 'order_address/country_id', 'shipping_address_id', null, 'left') 
     ->joinAttribute('shipping_postcode', 'order_address/postcode', 'shipping_address_id', null, 'left') 
     ->joinAttribute('shipping_telephone', 'order_address/telephone', 'shipping_address_id', null, 'left') 
     ->joinAttribute('shipping_fax', 'order_address/fax', 'shipping_address_id', null, 'left') 

     ->addFieldToFilter('status', array("in" => array(
      'complete', 
      'closed') 
      )) 

     ->addAttributeToFilter('store_id', Mage::app()->getStore()->getId()) 
     ->addAttributeToSort('created_at', 'asc') 
     ->load(); 
    foreach($orders as $order): 
    echo $order->getIncrementId().'<br/>'; 
    echo $order->getShippingTelephone().'<br/>'; 
    endforeach; 
4

試試這個:

select * from sales_flat_order; 

這頭級別的信息。如果您需要線項目信息/更深層次的信息,您可以使用類似下面的連接:

select e.*,sfoi.* from sales_flat_order e 
left join sales_flat_order_item sfoi on (e.entity_id=sfoi.order_id) 

現在,這是要複製(笛卡爾積)所有的頭信息與線項目信息一起。如果進一步要求的東西,讓我知道,我在Magento的EAV SQL :)

+0

我使用magento 1.4.2.wehn我執行了前2個quesries它說「表'pigi_magento.sales_order'不存在」 – 2011-02-28 05:26:38

+0

看到更新 - 你正在使用新的平板模型 – philwinkle 2011-02-28 05:30:39

+0

謝謝winkle,我絕對需要向您學習註冊Magento的數據庫結構。您能否提供一些見解如何在Magento中構建表格? – 2011-02-28 05:44:20

0

晚了一點,但是這可能是有用的(在Magento CE 1.7.0.2測試)。該代碼在指導方面受到嚴重評論。

<?php 
// include the core code we are going to use 
require_once('app/Mage.php'); 
umask (0); 
Mage::app('default'); 

// resources 
$resource = Mage::getSingleton('core/resource'); 

// db access 
$db_read = $resource->getConnection('core_read'); 
$db_write = $resource->getConnection('core_write'); 

// support table prefix if one is being used 
$table_prefix = Mage::getConfig()->getTablePrefix(); 

// count the orders 
$order_num = $db_read->fetchOne("SELECT COUNT(*) AS num FROM {$table_prefix}sales_flat_order WHERE status = 'pending'"); 

// get an array of the orders 
$orders = $db_read->fetchAll("SELECT sales.* FROM {$table_prefix}sales_flat_order AS sales WHERE sales.status = 'pending'"); 

// start iterating through the orders 
for($i=0; $i < intval($order_num); $i++) { 

    // order id 
    $orderid = $orders[$i]['entity_id']; 

    // shipping address 
    $order_details = Mage::getModel('sales/order')->load($orderid); 
    $shippingAddress = $order_details->getShippingAddress(); 

    // use like so 
    $shippingAddress->getPrefix()); 
    $shippingAddress->getFirstname(); 
    $shippingAddress->getLastname(); 
    $shippingAddress->getCompany(); 
    $shippingAddress->getEmail(); 
    $shippingAddress->getTelephone(); 
    $shippingAddress->getStreetFull(); 

    // billing address 
    $order_details = Mage::getModel('sales/order')->load($orderid); 
    $billingAddress = $order_details->getBillingAddress(); 

    // use like so 
    $billingAddress->getPrefix()); 
    $billingAddress->getFirstname(); 
    $billingAddress->getLastname(); 
    $billingAddress->getCompany(); 
    $billingAddress->getEmail(); 
    $billingAddress->getTelephone(); 
    $billingAddress->getStreetFull(); 

    // and if you want order items, do the following 
    $items = $db_read->fetchAll("SELECT 
     items.order_id AS orderid, 
     items.item_id AS itemid, 
     orders.total_item_count AS total_items_in_order, 
     items.quote_item_id AS quoteid, 
     items.created_at AS orderdate, 
     items.product_type, 
     items.sku AS itemcode, 
     items.name AS itemname, 
     items.price_incl_tax AS itemprice, 
     items.tax_amount AS itemtax, 
     items.discount_amount AS discount, 
     items.qty_ordered AS qty_ordered, 
     items.qty_shipped AS qty_shipped, 
     address.email AS email, 
     address.prefix AS title, 
     address.firstname AS firstname, 
     address.lastname AS lastname, 
     address.street AS address, 
     address.city AS city, 
     address.region AS region, 
     address.country_id AS country, 
     address.postcode AS postcode, 
     address.telephone AS telephone 
     FROM {$table_prefix}sales_flat_order AS orders 
     JOIN {$table_prefix}sales_flat_order_item AS items 
      ON items.order_id = orders.entity_id 
     LEFT JOIN {$table_prefix}sales_flat_order_address AS address 
      ON orders.entity_id = address.parent_id 
     WHERE 
     items.order_id = $orderid 
     AND address.address_type = 'shipping' 
     AND orders.status = 'pending' 
    "); 

    foreach ($items AS $item) { 
     echo $item['itemid']; 
     // blah blah blah 
    } 
} 
?> 

希望能幫助別人!