magento订单列表查询

发布于 2024-10-19 11:18:14 字数 191 浏览 3 评论 0原文

我想选择 Magento 中所有订单的列表。

这是我目前正在处理的另一个 PHP 应用程序中显示来自 magento 的所有订单列表所必需的。

也有人可以使用 Magento 约定为我编写代码,例如 Mage::

我使用 Magento 1.4.2 版本。

谢谢,
标记

I want to select the list of all orders in Magento.

This is required for me to show the list of all the orders from magento in another PHP application presently I'm working on.

Also can some one write me the code using the Magento conventions such as Mage::

Im using Magento 1.4.2 version.

Thanks,
Mark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜你已久 2024-10-26 11:18:14

此代码使用“Magento 方式”并通过模型层访问数据,这使您免受表结构更改(例如,扁平与 EAV)的影响。在 Magento 安装的根目录中创建一个包含此框架代码的新 PHP 文件(如果在其他位置,请更新第一个 require 语句的路径)。

这为您提供了一些有关如何向集合添加属性的示例,您应该能够按照这些示例添加更多属性(如果需要)。它向您展示了如何按属性过滤以及按属性排序。还可以显示您需要的字段的示例。

HTH,
京东

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;

This code uses the "Magento way" and accesses the data through the Model layer which insulates you from changes in the table structure (e.g. flat vs EAV). Create a new PHP file containing this skeleton code in the root of your Magento install (if elsewhere update the path for the first require statement).

This gives you some examples of how to add attributes to the collection, you should be able to follow the examples to add more if required. It shows you how to filter by attributes, and sort by attributes. Examples also for echo'ing out the fields that you need.

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;
能怎样 2024-10-26 11:18:14

试试这个:

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 的大师:)

Try this:

select * from sales_flat_order;

That's header-level information. If you require line-item information / deeper information, you might use something like the following join:

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

Now, this is going to duplicate (cartesian product) all of the header information along with the line-item information. If you require something further, let me know, I'm a master at Magento EAV SQL :)

稀香 2024-10-26 11:18:14

有点晚了,但这可能有用(在 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
    }
}
?>

希望对某人有帮助!

A bit late, but this might be useful (tested in Magento CE 1.7.0.2). The code is heavily commented for guidance.

<?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
    }
}
?>

Hope that helps someone!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文