Magento Forum

   
Determine if customer already purchased an item
 
celerystick
Jr. Member
 
Total Posts:  4
Joined:  2011-08-10
 

I sell virtual goods in my store, and it makes no sense for someone to ever purchase more than 1 instance of each product. Ideally, I would like a logged in customer to see a list of products similar to how say, Apple App Store shows you - a button “Buy now” next to items they have not purchased, and a label “Already purchased” next to those they did. How can I achieve this?

I was thinking of modifying the product display template - which I have already modified for stylistic purposes - but don’t know how to check each product for has-this-item-been-purchased-by-this-user.

Any help would be greatly appreciated.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brent W Peterson
Moderator
 
Avatar
Total Posts:  3215
Joined:  2009-02-26
Minneapolis MN
 

You can first load the customers order

$orderCollection Mage::getModel(’sales/order’)->getCollection(); 
$orderCollection->getSelect()->where(’e.customer_id =.$customerId);

The loop through your order collection to see if the product is there and if it is you can display to the customer they have already ordered.

 
Magento Community Magento Community
Magento Community
Magento Community
 
celerystick
Jr. Member
 
Total Posts:  4
Joined:  2011-08-10
 

Thank you for the tip, need some more help smile.

I now run the following in the beginning of list.phtml:

$orderCollection Mage::getModel('sales/order')->getCollection(); 
$orderCollection->getSelect()->where('e.customer_id ='Mage::getSingleton('customer/session')->getCustomerId()); 
$arr $orderCollection->toArray();

At the end, $arr stays blank. Order history page for the logged in user shows 4 orders - 2 pending and 2 complete. What am I doing wrong?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brent W Peterson
Moderator
 
Avatar
Total Posts:  3215
Joined:  2009-02-26
Minneapolis MN
 

ok so you don\’t want to show products for customers that are logged in correct?

You want to start out like this

<?php if(Mage::getSingleton(\'customer/session\')->isLoggedIn()): ?>
Then if you don\’t want to override you can put all the products for the customer in an array and check to see if they are in the existing collection.

You can do something like this

$productIds = array()

$collection Mage::getModel(’sales/order’)->getCollection(); 
        
$collection->addAttributeToSelect(\'product_id\');
        
$collection->addAttributeToFilter(\'customer_id\', array(
            
\'eq\' => Mage::getSingleton(\'customer/session\')->getId(),
            ));
        foreach (
$collection as $order)
        
{
            $productIds[] 
$order->getProductId();
        
}


$newCollection 
$this->$_productCollection

$newcollection
->addAttributeToFilter(\'product_id\', array(
    
\'nin\' => $productIds[] ,
    ));

Ok so the bad news is that I have not tested any of it!!

 
Magento Community Magento Community
Magento Community
Magento Community
 
celerystick
Jr. Member
 
Total Posts:  4
Joined:  2011-08-10
 

Thank you for help!

I eventually cooked up something that worked out of all these ingredients:

$orderCollection Mage::getModel('sales/order')->getCollection(); 
$orders $orderCollection->addAttributeToFilter("customer_id"Mage::getSingleton('customer/session')->getCustomerId())->addAttributeToFilter('state''complete');

$purchased = array(); // will contain IDs of purchased items
foreach ($orders as $order){
    $items 
$order->getAllItems();
    foreach (
$items as $item$purchased[] $item->getProductId();        
}

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top