Model/ResourceModel/Contact.php
public function getSalesDataForCustomersWithOrderStatusesAndBrand($customerIds, $statuses)
    {
        $orderCollection = $this->orderCollectionFactory->create();
        $salesOrder = $orderCollection->getTable('sales_order');
        $salesOrderGrid = $orderCollection->getTable('sales_order_grid');
        $salesOrderItem = $orderCollection->getTable('sales_order_item');

        $orderCollection->addFieldToSelect(['customer_id'])
            ->addExpressionFieldToSelect('total_spend', 'SUM({{grand_total}})', 'grand_total')
            ->addExpressionFieldToSelect('number_of_orders', 'COUNT({{*}})', '*')
            ->addExpressionFieldToSelect('average_order_value', 'AVG({{grand_total}})', 'grand_total')
            ->addFieldToFilter('customer_id', ['in' => $customerIds]);

        $columnData = $this->buildColumnData($salesOrderGrid, $salesOrder, $salesOrderItem);
        $orderCollection->getSelect()
            ->columns($columnData)
            ->group('customer_id');

        if (! empty($statuses)) {
            $orderCollection->getSelect()->where('status in (?)', $statuses);
        }

        $orderArray = [];
        foreach ($orderCollection as $item) {
            $orderArray[$item->getCustomerId()] = $item->toArray(
                [
                    'total_spend',
                    'number_of_orders',
                    'average_order_value',
                    'last_order_date',
                    'first_order_id',
                    'last_order_id',
                    'last_increment_id',
                    'product_id_for_first_brand',
                    'product_id_for_last_brand',
                    'week_day',
                    'month_day',
                    'product_id_for_most_sold_product'
                ]
            );
        }

        return $this->getCollectionWithLastQuoteId($customerIds, $orderArray);
    }
public function getSalesDataForCustomersWithOrderStatusesAndBrand($customerIds, $statuses)
    {
        $orderCollection = $this->orderCollectionFactory->create();
        $salesOrder = $orderCollection->getTable('sales_order');
        $salesOrderGrid = $orderCollection->getTable('sales_order_grid');
        $salesOrderItem = $orderCollection->getTable('sales_order_item');

        $orderCollection->addFieldToSelect(['customer_id'])
            ->addExpressionFieldToSelect('total_spend', 'SUM({{grand_total}})', 'grand_total')
            ->addExpressionFieldToSelect('number_of_orders', 'COUNT({{*}})', '*')
            ->addExpressionFieldToSelect('average_order_value', 'AVG({{grand_total}})', 'grand_total')
            ->addFieldToFilter('customer_id', ['in' => $customerIds]);

        $columnData = $this->buildColumnData($salesOrderGrid, $salesOrder, $salesOrderItem);
        $orderCollection->getSelect()
            ->columns($columnData)
            ->group('customer_id');

        if (! empty($statuses)) {
            $orderCollection->getSelect()->where('status in (?)', $statuses);
        }

        $orderArray = [];
        foreach ($orderCollection as $item) {
            $orderArray[$item->getCustomerId()] = $item->toArray(
                [
                    'total_spend',
                    'number_of_orders',
                    'average_order_value',
                    'last_order_date',
                    'first_order_id',
                    'last_order_id',
                    'last_increment_id',
                    'product_id_for_first_brand',
                    'product_id_for_last_brand',
                    'week_day',
                    'month_day',
                    'product_id_for_most_sold_product'
                ]
            );
        }

        return $this->getCollectionWithLastQuoteId($customerIds, $orderArray);
    }
public function getSalesDataForCustomersWithOrderStatusesAndBrand ( $ customerIds , $ statuses ) { $ orderCollection = $ this -> orderCollectionFactory -> create ( ) ; $ salesOrder = $ orderCollection -> getTable ( 'sales_order' ) ; $ salesOrderGrid = $ orderCollection -> getTable ( 'sales_order_grid' ) ; $ salesOrderItem = $ orderCollection -> getTable ( 'sales_order_item' ) ; $ orderCollection -> addFieldToSelect ( [ 'customer_id' ] ) -> addExpressionFieldToSelect ( 'total_spend' , 'SUM({{grand_total}})' , 'grand_total' ) -> addExpressionFieldToSelect ( 'number_of_orders' , 'COUNT({{*}})' , '*' ) -> addExpressionFieldToSelect ( 'average_order_value' , 'AVG({{grand_total}})' , 'grand_total' ) -> addFieldToFilter ( 'customer_id' , [ 'in' => $ customerIds ] ) ; $ columnData = $ this -> buildColumnData ( $ salesOrderGrid , $ salesOrder , $ salesOrderItem ) ; $ orderCollection -> getSelect ( ) -> columns ( $ columnData ) -> group ( 'customer_id' ) ; if ( ! empty ( $ statuses ) ) { $ orderCollection -> getSelect ( ) -> where ( 'status in (?)' , $ statuses ) ; } $ orderArray = [ ] ; foreach ( $ orderCollection as $ item ) { $ orderArray [ $ item -> getCustomerId ( ) ] = $ item -> toArray ( [ 'total_spend' , 'number_of_orders' , 'average_order_value' , 'last_order_date' , 'first_order_id' , 'last_order_id' , 'last_increment_id' , 'product_id_for_first_brand' , 'product_id_for_last_brand' , 'week_day' , 'month_day' , 'product_id_for_most_sold_product' ] ) ; } return $ this -> getCollectionWithLastQuoteId ( $ customerIds , $ orderArray ) ; }
Customer collection with all data ready for export.

@param  array $customerIds
@param  array $statuses

@return array

@throws \Magento\Framework\Exception\LocalizedException
Customer collection with all data ready for export .