getAllVisibleItems(), getAllItems returns items twice

getAllVisibleItems(), getAllItems returns items twice

August 2, 2026 · Illustrated author avatarBy Stefan Bogdanescu
getAllVisibleItems(), getAllItems returns items twice

Duplicate Order Items in Magento 1.7: Understanding the Behavior

If you’re working with Magento 1.7 and trying to retrieve order items using built-in functions like getAllItems(), getItemsCollection(), or getAllVisibleItems(), you might be facing a peculiar issue - each item is returned twice, leading to data duplication.

Let’s dive into this problem and understand why it occurs even when your database doesn’t contain parent-child product relationships.

The Magento Order Item Retrieval Issue Explained

At the core of this issue lies the way Magento handles order items. By default, Magento 1.7 retrieves order items by joining several tables to gather all necessary information about each item. However, due to how these joins are performed, some rows get duplicated in the result set.

Specifically, when you call getAllItems() or similar functions on an order object, Magento performs a left join between the sales_order_item table and other relevant tables (like catalog_product_entity). If there isn’t a matching row in one of the joined tables for a given item, the item is still included in the result set twice - once with null values from the missing table.

Debugging the Issue: A Closer Look at Your Code

The code you provided clearly illustrates this behavior. You’re loading an order by increment_id and then iterating through its visible items:

$order = Mage::getModel("sales/order")->load($order_id, 'increment_id');
$ordered_items = $order->getAllVisibleItems();

When you print the item details within your loop, you notice that each item is displayed twice. The entity_id (or row ID) in the sales_order_item table is being outputted as 6, indicating that you’re indeed dealing with duplicate rows.

Troubleshooting and Solving the Problem

To resolve this issue without modifying Magento’s core files (which is generally not recommended for updates or security reasons), you can use a workaround by filtering out the duplicates before processing your items. Here’s an approach using PHP:

  1. First, fetch all visible items into an array.
$ordered_items = $order->getAllVisibleItems();
  1. Then, create an array to store item IDs and remove any duplicates based on their IDs.
$item_ids = []; // Initialize empty array

foreach ($ordered_items as $item) {
    if (!in_array($item->getItemId(), $item_ids)) {
        $item_ids[] = $item->getItemId(); // Add item ID to the array
    }
}
  1. Finally, re-fetch the items using only the unique IDs.
$unique_items = Mage::getResourceModel('sales/order_item_collection')
    ->addFieldToFilter('item_id', ['in' => $item_ids]);

Now, $unique_items contains all visible order items without any duplicates. You can proceed with processing these items in your loop:

foreach ($unique_items as $item) {
    // Your item processing code here
}

Conclusion: A Clean and Efficient Approach

In summary, the duplication of order items you’re experiencing is a known limitation in Magento 1.7’s core functions for retrieving order data. To avoid this issue without modifying Magento’s source code:

  • Use the workaround described above to filter out duplicate items based on their IDs.
  • Remember that this approach might introduce performance overhead if dealing with a large number of items, as it requires extra database queries.

For more insights into optimizing your e-commerce platform and staying up-to-date with the latest best practices in Magento development, make sure to explore our other blog posts at Laravel Company.

Magento 1 Catalog Database Orders

Our network