Exporting Magento Orders - Getting Discount Amount

Exporting Magento Orders - Getting Discount Amount

October 2, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Exporting Magento Orders - Getting Discount Amount

Exporting Magento Orders: Getting Discount Amount

Exporting order data from Magento is a common requirement for e-commerce businesses. It allows for better analysis of sales performance, customer behavior, and inventory management. In your question, you’ve shared some PHP code that exports orders based on certain filters like status and total paid amount.

Current State & Gaps in Your Code

The current implementation retrieves the following order details:

  • Billing address last name
  • Total paid amount
  • Shipping address telephone number
  • Payment method type (credit card)
  • Order status
  • Newline character for formatting

However, you’ve mentioned that you need to get both the total discount and item quantity from each order. The existing code doesn’t fetch these values.

Getting the Total Discount Amount

To retrieve the total discount amount of an order in Magento, you can use the getDiscountAmount method provided by the Mage_Sales_Model_Order model:

$discountAmount = $myOrder->getDiscountAmount();

The getDiscountAmount method returns the total discount applied to the order. This includes discounts from coupon codes, tier prices, special prices, and any other applied discounts.

Getting the Number of Order Items

To get the quantity of items in an order, you need to traverse through the order’s item collection:

$itemCollection = $myOrder->getItemsCollection();
$totalItemQuantity = 0;

foreach ($itemCollection as $item) {
    $totalItemQuantity += $item->getQtyOrdered();
}

The getItemsCollection method returns an object of type Mage_Sales_Model_Mysql4_Order_Item_Collection. You can loop through this collection to get the quantity ordered (getQtyOrdered) for each item in the order.

Enhanced Code with Discount Amount & Item Quantity

Here’s how you can update your existing code to include both the total discount amount and the number of items:

// !    ----- GET NEW ORDERS -----
$myOrder=Mage::getModel('sales/order');
$orders=Mage::getModel('sales/mysql4_order_collection');

//Optional filters you might want to use - more available operations in method _getConditionSql      in Varien_Data_Collection_Db.
$orders->addFieldToFilter('total_paid',Array('gt' => 0)); //Amount paid larger than 0
$orders->addFieldToFilter('status',Array('eq' => "processing"));  //Status is "processing"

$allIds=$orders->getAllIds();
foreach($allIds as $thisId) {
    $myOrder->reset()->load($thisId);

    // Getting Order Fields
    echo "'" . $myOrder->getBillingAddress()->getLastname() . "',";
    echo "'" . $myOrder->getTotal_paid() . "',";
    echo "'" . $myOrder->getDiscountAmount() . "',";  // Discount Amount Added Here
    echo "'" . $myOrder->getShippingAddress()->getTelephone() . "',";
    echo "'" . $myOrder->getPayment()->getCc_type() . "',";

    $itemCollection = $myOrder->getItemsCollection();
    $totalItemQuantity = 0;
    foreach ($itemCollection as $item) {
        $totalItemQuantity += $item->getQtyOrdered();
    }

    echo "'" . $totalItemQuantity . "',";  // Item Quantity Added Here
    echo "'" . $myOrder->getStatus() . "',";
    echo "\r\n";
}

Conclusion

In this blog post, we’ve reviewed your existing PHP code for exporting Magento orders and enhanced it to include the total discount amount and the number of items in each order.

Remember that Magento provides a rich API for interacting with order data. By using the appropriate methods and collections provided by the Mage_Sales_Model_Order model, you can efficiently retrieve all necessary information about your orders without having to rely on complex queries or unnecessary computations.

If you need further assistance with Magento development or e-commerce solutions, feel free to explore our services at Laravel Company. Our team of experienced developers specializes in building custom, scalable, and secure e-commerce platforms using the latest technologies.

Performance Orders Customers Pricing

Our network