1

I want to check if the customer logged in has had at least 1 order over €30 euro. What would be the best way to do this?

10

Use functions below

/**
 * @param int $customerId
 * @param null|float $grandTotal
 *
 * @return bool
 */
public function isCustomerHasOrders($customerId, $grandTotal = null)
{
    $orderCollection = $this->getCustomerOrderCollection($customerId, $grandTotal);
    return (bool)$orderCollection->getSize();
}

/**
 * @param int  $customerId
 * @param null $grandTotal
 *
 * @return Mage_Sales_Model_Resource_Order_Collection
 */
public function getCustomerOrderCollection($customerId, $grandTotal = null)
{    
    $orderCollection = Mage::getResourceModel('sales/order_collection')
        ->addFieldToSelect('*')
        ->addFieldToFilter('customer_id', $customerId)
        ->addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()))
        ->setOrder('created_at', 'desc')
    ;
    if ($grandTotal && $grandTotal > 0) {
        $orderCollection->addFieldToFilter('base_grand_total', array ('gteq' => $grandTotal));
    }
    return $orderCollection;
}

You can specify order statuses

->addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()))

Usage

$customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
$grandTotal = 30;
if ($this->isCustomerHasOrders($customerId, $grandTotal)) {
    // TODO:
}
  • can i get the order which has specific product? – Hunter May 19 '17 at 4:15
  • @Hunter It is possible, but you would need to sub-query on the sales_flat_order_item table, or JOIN that table to add that filter. – Eric Seastrand May 22 '18 at 17:17
7

This should do it:

<?php //filtering the customers by email address
$customer = Mage::getSingleton('customer/session')->getCustomer();
$email = $customer->getEmail(); 

$orderCollection = Mage::getModel('sales/order')->getCollection();
$orderCollection->addFieldToFilter('customer_email', $email);

//loop through his orders
foreach ($orderCollection as $_order): ?>

    <?php $order_total = $_order->getGrandTotal(); //get order total ?>
    <?php if($order_total > 30): //check total, including shipping ?>
        <p>Hey, this customer has at least one order over 30 euros</p>
        <?php break; //break the loop at first order over 30 ?>
    <?php endif; ?>

<?php endforeach; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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