1

Is their any way we can get total amount that has been spend by customer in magento 2 ?

9
  • Total amount for all orders that have been placed by customer? – ZealousWeb Mar 5 at 11:14
  • yes, Total Spent on order by customer. – devhs Mar 5 at 11:15
  • 1
    I meant to ask that for particular order or all orders which have been placed by customer? – ZealousWeb Mar 5 at 11:17
  • Thanks for the response. All orders total placed by customer. – devhs Mar 5 at 11:18
  • Thanks. Do you need it for particular customer or for all customers? – ZealousWeb Mar 5 at 11:19
2

You can load the order collection for example by customer email, then loop through the orders totalling the value:

use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;

    public function __construct(
        CollectionFactory $orderCollectionFactory
    ) {
       $this->orderCollectionFactory = $orderCollectionFactory;
    }

    public function test()
    {
        
        $customerEmail="testcustomer@mycoolshop.com";
        $total=0;
        $orders = $this->orderCollectionFactory->create()
            ->addAttributeToFilter('customer_email', $customerEmail);
        foreach ($orders as $order) {
            echo "Order# {$order->getIncrementId()} - Creation Date: {$order->getCreatedAt()} - Value: {$order->getGrandTotal()}". PHP_EOL;
            $total=$total+$order->getGrandTotal();
        }
        echo "Total for customer:".$total.PHP_EOL;


    }
9
  • Thanks for the answer, let me check – devhs Mar 5 at 11:23
  • I am getting 0 only. – devhs Mar 5 at 11:25
  • are you using a valid email address for a customer that has placed orders? – paj Mar 5 at 11:27
  • Yes valid email address, i have echo count($orders); that returns 4, it seems like issue is with $order->getGrandTotal() – devhs Mar 5 at 11:28
  • 1
    changing getGrandTotal to getTotal works. – devhs Mar 5 at 11:30
1

Please add below code in any root file. Please create custom.php file in root of magento folder and check your output.

<?php
ini_set('display_errors', 1);
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$OrderFactory = $objectManager->create('Magento\Sales\Model\ResourceModel\Order\CollectionFactory');
$orderCollection = $OrderFactory->create()->addFieldToSelect(array('*'));
$orderCollection->addAttributeToFilter('customer_email', 'test@zealousweb.com');

$total = 0;
foreach($orderCollection as $orderData)
{
     $total = $total + $orderData->getGrandTotal();
}   

echo $total;
die();
1
  • If this script is helpful for you then please approve and upvote this. Many thanks! – ZealousWeb Mar 5 at 11:40

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.