0

I'd like to show the "date of the last order" on the view.phtml product page.

So far I've discovered "getCreatedAtStoreDate" from "design/template/sales/order/history.phtml.

But when I include these lines in view.phtml it results in the following error: Call to a member function getCreatedAtStoreDate() on null in /.../view.phtml

Is there any simple way to show the date of the last purchase? Maybe someone had this issue before. Thanks

1
  • you want to only for customer last order or common last order?
    – Ajay Patel
    Apr 15, 2016 at 15:48

6 Answers 6

1

When a product is purchased, an Order Item is created. The way to go imho is to get the Order-Item-Collection, filter by product_id and sort by created_at. This should work (untested, though):

// only if product is not yet available
$product = Mage::registry('current_product');

// get newest order item of this product
$collection = Mage::getModel('sales/order_item')->getCollection()->addFieldToFilter('product_id', $product->getId())->addFieldToSort('created_at', 'DESC')->addPageSize(1)->addCurPage(1);

if($collection->count()) {
    $item = $collection->getFirstItem();
    echo $item->getCreatedAt();
}

You should not use this kind of code in a template file, though. Make an own extension with an own Block-class and include this block in your layout.

4
  • this looks promising - gonna try.
    – chris
    Apr 17, 2016 at 10:02
  • Thanks! This worked; but I had to remove the following: ->addFieldToSort('created_at', 'DESC')->addPageSize(1)->addCurPage(1)
    – chris
    Apr 17, 2016 at 10:21
  • I've tried to add some information about the ordered customoptions. So far the following does work; but it uses "Order ID" to sort out the result. Is there any way of using something like "Product ID->getLastOrder" instead?
    – chris
    Apr 17, 2016 at 10:36
  • If you remove "->addFieldToSort('created_at', 'DESC')", I am not sure it will work, because this filter sorts newest items first (I think, default is always by ID and ASC) Apr 18, 2016 at 7:41
0

To get last order,

$orders = Mage::getModel('sales/order')->getCollection()
       ->setOrder('increment_id','DESC')
       ->setPageSize(1);
       ->getFirstItem();

To get the last order by customer.

Mage::getSingleton('checkout/session')->getLastOrderId();

Happy Coding. :)

0

@Ajay & @simonthesorcerer - thanks.

I forgot to mention that it should be the "Last Orderdate for each product"

This date should be visible on each "Product Detail" page of the individual selected product.

Example:

Product-Id: 1; Last Date of Order: 04.04.2016

Product-Id: 2; Last Date of Order: 01.04.2016

Thanks ahead.

0

I've tried to add some information about the ordered customoptions. So far the following does work; but it uses "Order ID" to sort out the result. Is there any way of using something like "Product ID->getLastOrder" instead?

$incrementId = '100000108';
$order = Mage::getModel('sales/order')->loadByIncrementId($incrementId);
foreach ($order->getAllItems() as $item) {
    $options = $item->getProductOptions(); 
    $customOptions = $options['options'];   
    if(!empty($customOptions))
    {
    foreach ($customOptions as $option)
    {       
        $optionTitle = $option['label'];
        $optionId = $option['option_id'];
        $optionType = $option['type'];
        $optionValue = $option['value'];
    }
    echo $optionValue;


    }
}

Thanks @Mukesh

0

For those who want to make a column for the last ordered date of a product in Product grid, you can simply join sales_flat_order_item and use the code below in your select query

new Zend_Db_Expr("max(sales_flat_order_item.created_at)")
0

This should work on view.phtml

$order_items = Mage::getResourceModel('sales/order_item_collection')
    ->addAttributeToSelect('created_at')
    ->addAttributeToFilter('product_id', array('eq' => $_product->getId()))
    ->load();
    $order_items->getSelect()->order('created_at DESC');
    $order_items->getSelect()->limit(1);
    foreach ($order_items as $value) {
        echo $value->getCreatedAt();
    }

Your Answer

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

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