<?php 
namespace Magetop\Advancepreview\Block\Adminhtml;

use Magento\Backend\Block\Template;
use Magento\Directory\Helper\Data as DirectoryHelper;
use Magento\Framework\Json\Helper\Data as JsonHelper;

class CustomOrders extends Template
{
    protected $priceHelper;
    protected $_orderCollectionFactory;
   public function __construct(
       Template\Context $context,
       \Magento\Framework\Pricing\Helper\Data $priceHelper,
       \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
       array $data = [],
       JsonHelper $jsonHelper = null,
       DirectoryHelper $directoryHelper = null
   )
   {
        $this->priceHelper = $priceHelper;
        $this->_orderCollectionFactory = $orderCollectionFactory;
       parent::__construct($context, $data, $jsonHelper, $directoryHelper);
   }
   public function getNewestOrders() {
    $collection = $this->getOrderCollection();
    $ordersData  = [];
    foreach($collection as $order) {
        $customer = $order->getCustomerName();
        $allItems = $order->getAllItems();
        $priceItem = 0;
        $t = 0;
        foreach ($allItems as $key => $item) {
            $infoRequest = $item->getProductOptionByCode('info_buyRequest');
            if (isset($infoRequest['txt_project_final_price'])) {
                $priceItem += $infoRequest['txt_project_final_price'];
            }
            $t++;
        }
       
        $ordersData[] = [
            'order_id'=>$order->getId(),
            'total_item'=>$t,
            'customer_name'=>$customer,
            'total'=>$this->priceHelper->currency($priceItem,true,false),
        ];

    }
    return $ordersData;
   }
   protected function getOrderCollection()
   {
       $collection = $this->_orderCollectionFactory->create()
         ->addAttributeToSelect('*')
         ->setOrder('created_at','DESC')
         ->setPageSize(5);
     
     return $collection;
     
    }

}