How can I display all products bought by a customer in magento?

How can I display all products bought by a customer in magento?

August 1, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How can I display all products bought by a customer in magento?

Display Products Bought by a Customer in Magento

Working with e-commerce platforms like Magento allows for a wide range of customization options to enhance the user experience. One common requirement is to display all products purchased by a specific customer from an external script located in the root folder.

Accessing Magento Models from an External Script

To access Magento models from an external script, you need to include app/mage.php file which provides the necessary Magento bootstrap and autoload functionality. Here’s how you can set up your external PHP script:

<?php
// Include Magento base file
require_once 'app/Mage.php';

// Mage::init() triggers the autoloader
Mage::init();

// Get the current customer
$customer = Mage::getSingleton('customer/session')->getCustomer();

Retrieving All Products Bought by a Customer

Once you have access to the Magento object model, retrieving all products bought by a customer involves querying the order history and then extracting the relevant product information from each order.

Step 1: Get Customer Orders

To retrieve all orders placed by a specific customer, use the Mage::getResourceModel('sales/order_collection') function:

$orders = Mage::getResourceModel('sales/order_collection')
    ->addFieldToFilter('customer_id', $customer->getId())
    ->load();

This code retrieves all orders associated with the current customer’s ID.

Step 2: Extract Product Information from Orders

Each order contains multiple product items. To access these products, iterate through each order and use the getAllItems() method:

foreach ($orders as $order) {
    $items = $order->getAllItems();

    foreach ($items as $item) {
        // Access product attributes
        $productId = $item->getProductId();
        $name = $item->getName();
        $sku = $item->getSku();
        $price = $item->getPrice();
    }
}

Step 3: Displaying Product Information

To display the retrieved products, you can use a simple HTML structure and loop through each product item:

<div id="your-books-section">
    <h2>Your Books</h2>
    <?php foreach ($items as $item): ?>
        <div class="book-item">
            <span class="book-title"><?php echo $item->getName(); ?></span>
            <span class="book-price">$<?php echo $item->getPrice(); ?></span>
        </div>
    <?php endforeach; ?>
</div>

Conclusion

Displaying all products bought by a customer in Magento involves setting up an external script, accessing the necessary models, and querying the order history to extract product information. By following this approach, you can create a custom section on your site that displays items ordered by a specific customer.

Remember to keep your code clean and maintainable by separating logic from presentation using proper PHP and HTML structuring techniques.

For more advanced Magento customization options and best practices, consider exploring the resources available at Laravel Company. They offer expert Magento development services tailored to meet your project’s specific needs.

Catalog Database Orders Customers

Our network