magento orders list query

后端 未结 3 1444
[愿得一人]
[愿得一人] 2020-12-29 17:09

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\

3条回答
  •  暖寄归人
    2020-12-29 17:48

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

    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!

提交回复
热议问题