1

how can we get the Revenue using API or Without using API programming in Magento 2.4, like below image,

enter image description here

I have tried using API, and below is my code. webapi.xml

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">
    <route url="/V1/custom/custom-api/post/" method="GET">
        <service class="Rental\Totalrevenue\Api\CustomInterface" method="getTotalRevenue"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

custom.php /var/www/html/m2_new_staging/app/code/Rental/Totalrevenue/Model/Api

<?php

namespace Rental\Totalrevenue\Model\Api; use Magento\Reports\Model\ResourceModel\Order\CollectionFactory; use Psr\Log\LoggerInterface;

class Custom { protected $_collectionFactory; protected $logger;

public function __construct( LoggerInterface $logger, CollectionFactory $collectionFactory ) {

$this-&gt;logger = $logger;

}

/**

  • @inheritdoc

*/

public function getTotalRevenue() { //pass ID parameter as you need from store or website $isFilter = $this->getRequest()->getParam( 'store' ) || $this->getRequest()->getParam( 'website' ) || $this->getRequest()->getParam( 'group' ); $period = $this->getRequest()->getParam('period', Period::PERIOD_24_HOURS); // 1y, 2y / @var $collection Collection / $collection = $this->_collectionFactory->create()->addCreateAtPeriodFilter( $period )->calculateTotals( $isFilter ); if ($this->getRequest()->getParam('store')) { $collection->addFieldToFilter('store_id', $this->getRequest()->getParam('store')); } else { if ($this->getRequest()->getParam('website')) { $storeIds = $this->_storeManager->getWebsite($this->getRequest()->getParam('website'))->getStoreIds(); $collection->addFieldToFilter('store_id', ['in' => $storeIds]); } else { if ($this->getRequest()->getParam('group')) { $storeIds = $this->_storeManager->getGroup($this->getRequest()->getParam('group'))->getStoreIds(); $collection->addFieldToFilter('store_id', ['in' => $storeIds]); } elseif (!$collection->isLive()) { $collection->addFieldToFilter( 'store_id', ['eq' => $this->_storeManager->getStore(Store::ADMIN_CODE)->getId()] ); } } } $collection->load(); $totals = $collection->getFirstItem(); return $totals->getRevenue(); //return 'Total Revenue $param ' . $param; }

public function getPost($value) { $response = ['success' => false];

try {
  // Your Code here

  $response = ['success' =&gt; true, 'message' =&gt; $value];
} catch (\Exception $e) {
  $response = ['success' =&gt; false, 'message' =&gt; $e-&gt;getMessage()];
  $this-&gt;logger-&gt;info($e-&gt;getMessage());
}
$returnArray = json_encode($response);
return $returnArray;

}

}

CustomInterface.php

/var/www/html/m2_new_staging/app/code/Rental/Totalrevenue/Api

<?php

namespace Rental\Totalrevenue\Api;

interface CustomInterface {

/**

  • GET for Post api
  • @param string $param
  • @return string

*/ public function getTotalRevenue();

}

here i have code $period = $this->getRequest()->getParam('period', Period::PERIOD_24_HOURS);, can we have changes that period like, today, last 3 months?

Rushikesh Solanki
  • 909
  • 12
  • 28

1 Answers1

0

Go to the Path Magento\Backend\Block\Dashboard\Totals This block is responsible form returning the Revenue to the dashboard Magento_Backend::dashboard\totalbar.phtml.check this line $this->addTotal(__('Revenue'), $totals->getRevenue());

Mehran
  • 492
  • 3
  • 14