2

I'm trying to get some of the values from the collection, below is my code.

<?php 
public function RentalPartnerRevenue()
  { 
    $filter = $this->request->getParam('filter'); 
    $operator_id = $this->request->getParam('operator_id');   
    try{ 

        $completecollection1 = $this->_orderCollectionFactory->create()
                                             ->addAttributeToSelect('*');  
            
        if(isset($filter) && $filter  == 'totalrevenue'){
            //$completecollection1->addAttributeToSelect('grand_total')->getColumnValues('grand_total');       
            $completecollection1->getSelect()->joinLeft(
                'rider_detail as wrd',
                'wrd.booking_id = main_table.entity_id',
                ['entity_id','order_total', 'operator_id']
            )->order('wrd.created_at DESC')
             ->where('booking_status ="' . self::COMPLETED . '"')
             ->where('operator_id ="' . $operator_id . '"');

            echo $completecollection1->getSelect()->__toString();
            //print_r($completecollection1->getData());

        }elseif(isset($filter) && $filter  == 'todaysrevenue'){ 

        }elseif(isset($filter) && $filter  == 'lastmonthrevenue'){ 

        }elseif(isset($filter) && $filter  == 'lastthreemonthrevenue'){ 

        }elseif(isset($filter) && $filter  == 'selectcustomdate'){

        }
         
        if(count($completecollection1->getData())){   
           $PartnerArray = [];
            foreach ($completecollection1 as $Partnerrevenue) {  
             if(isset($PartnerArray[$Partnerrevenue->getData('operator_id')])){ 
                 $PartnerArray[$Partnerrevenue->getData('operator_id')] = $PartnerArray[$Partnerrevenue->getData('operator_id')] + $Partnerrevenue->getData('grand_total');                 
               }else{
                 $PartnerArray[$Partnerrevenue->getData('operator_id')] = $Partnerrevenue->getData('grand_total');
               } print_r($PartnerArray); // right data
            } 
            
        }
    }catch (Exception $e) {
        return $this->setResponse(500, $e->getMessage(), null);
    }
  }

OUTPUT

Array
(
[2707173] => 700.0000
)
Array
(
[2707173] => 7500
)
Array
(
[2707173] => 12900
)
Array
(
[2707173] => 18300
)
Array
(
[2707173] => 23700
)
Array
(
[2707173] => 25500
)
Array
(
[2707173] => 35500
)
Array
(
[2707173] => 45500
)
Array
(
[2707173] => 79500
)
Array
(
[2707173] => 113500
)

here 2707173 is operator_id and => 700.000 to 113500 is order_total

now I want to get sum of order_total, but I don't know how can I sum these values. please help me with this.

thanks.

2
  • You mean you want to sum of array value? inside foreach? Commented Oct 12, 2022 at 13:12
  • @RohanHapani Yes Commented Oct 12, 2022 at 13:30

2 Answers 2

1

Based on my understanding of your requirement I am sharing the below code check if this helps, This will sum up all the values based on the operator id.

if(count($completecollection1->getData())){
    $PartnerArray = [];
    foreach ($completecollection1 as $Partnerrevenue) {
        $PartnerArray[$Partnerrevenue->getData('operator_id')][] = $Partnerrevenue->getData('grand_total');
    }
    $orderTotal = [];
    foreach($PartnerArray as $operatorId => $data) {
        $orderTotal[$operatorId] = array_sum($data);
    }
   return $orderTotal;
}
0

Magento allow us to retrieve all value of a specific column via $collection->getColumnValues('column');, in your case we can get the sum of grand total as per following:

$sumOfTotal = array_sum($completecollection1->getColumnValues('grand_total'));

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.