2

I'm trying to display a history of customer's orders by products. I succeeded to display every product ordered with its date, name, price, sku and quantity. But now I'd like to display it by at least one criteria: the date.

The purpose is to offer the customer a list of his purchases per month. How could I do it? Right now, I have done this in history.phtml file:

<?php 
    if (Mage::getSingleton('customer/session')->isLoggedIn()) {

        $orders= Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('customer_id',Mage::getSingleton('customer/session')->getCustomer()->getId());

            ?>

        <table class="data-table" id="my-orders-table">
            <thead>
            <tr class="first last">
                <th>Produit</th>
                <th>Référence</th>
                <th>Date</th>
                <th><span class="nobr">Prix</span></th>
                <th><span class="nobr">Quantités</span></th>
            </tr>
            </thead>
            <tbody>

            <?php
            foreach($orders as $eachOrder) {
                $order = Mage::getModel("sales/order")->load($eachOrder->getId()); 

                $items = $order->getAllVisibleItems();
                $i = 0;

                foreach($items as $item): ?>
                    <tr class="first last odd">

                    <?php 
                        echo "<td>" . $item->getData()['name'] . "</td>";
                        echo "<td>" . $item->getData()['sku'] . "</td>";
                        echo "<td>" . $this->formatDate($item->getData()['created_at']) . "</td>";
                        echo "<td>" . $item->getData()['price'] . "</td>";
                        echo "<td>" . intval($item->getData()['qty_ordered']) . "</td>";
                    ?>
                    </tr>  
                <?php endforeach;
            }
    }

             ?>

    </tbody>
    </table>

EDIT I edited my code and now I can sort my orders by month by adding a new attribute: addAttributeToFilter('created_at', array('like' => date('Y') . '-10-%');

But I still don't know how to create a dropdown list to select the month and then refresh to sort products with the chosen month. Thank you!

1
  • Just to say that I edited my code and now I can sort my orders by month by adding a new attribute: addAttributeToFilter('created_at', array('like' => date('Y') . '-10-%'); But I still don't know how to create a dropdown list to select the month and then refresh to sort products with the chosen month.
    – Kiritsu
    Commented Nov 2, 2015 at 7:16

1 Answer 1

0

I found a way to do what I wanted. Here's my solution:

<div class="page-title"><h1>Mon historique par produit</h1></div>
<?php

        if ($this->getRequest()->getParam('date') && $this->getRequest()->getParam('date') != '11') {
            $orders = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('customer_id', Mage::getSingleton('customer/session')->getCustomer()->getId())->addAttributeToFilter('created_at', array('like' => $this->getRequest()->getParam('date')."-%"));
        } else if ($this->getRequest()->getParam('date') && $this->getRequest()->getParam('date') == '11') {
            $orders = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('customer_id', Mage::getSingleton('customer/session')->getCustomer()->getId());
        } else {
            $orders = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('customer_id', Mage::getSingleton('customer/session')->getCustomer()->getId())->addAttributeToFilter('created_at', array('like' => date('Y-m')."-%"));
        }

?>

<form method="GET" action='.'>
    <p style="text-align: right;">Classer au 
    <select name="date">
        <?php 
            for ($i=11; $i > 0; $i--) { 
                echo "<option value='" . date('Y-m', strtotime(date('Y-m')." -" . (11 - $i) . " month")) . "'>" . date('Y-m', strtotime(date('Y-m')." -" . (11 - $i) . " month")) . "</option>";
            }
        ?>
        <option value="11">Depuis le début</option>
    </select>
    <input type="submit" />
    </p>
</form>

<table class="data-table" id="my-orders-table">
    <thead>
        <tr class="first last">
            <th>Produit</th>
            <th>Référence</th>
            <th>Date</th>
            <th><span class="nobr">Prix</span></th>
            <th><span class="nobr">Quantités</span></th>
        </tr>
    </thead>
    <tbody>

    <?php
        $i = 1;
        $count = count($orders);
        echo "<h3>" . $count . " articles(s)</h3>";
        foreach($orders as $eachOrder) {
            $order = Mage::getModel("sales/order")->load($eachOrder->getId()); 

            $items = $order->getAllVisibleItems();

            foreach($items as $item): ?>
                <?php if (($i++ % 2) == 0) {?>
                    <tr class="even">
                <?php } else {?>
                    <tr>

                    <?php 
                        }
                        $total += number_format($item->getData()['price'], 2);
                            echo "<td>" . $item->getData()['name'] . "</td>";
                            echo "<td>" . $item->getData()['sku'] . "</td>";
                            echo "<td>" . $this->formatDate($item->getData()['created_at']) . "</td>";
                            echo "<td>" . number_format($item->getData()['price'], 2, ',', '') . " €</td>";
                            echo "<td>" . intval($item->getData()['qty_ordered']) . "</td>";
                    ?>

                    </tr>
                <?php endforeach;
            }

            if ($this->getRequest()->getParam('date') && $this->getRequest()->getParam('date') == '11') {
                echo "<tr><td><strong>Total : </strong>" .$total. "€</td></tr>";
            } else {
                echo "<tr><td><strong>Total sur le mois : </strong>" .$total. "€</td></tr>";
            }

    ?>
</tbody>
</table>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.