🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How to Get Total Segment Using Magento 2 Rest API

By Sanjay JethvaUpdated on May 22, 2025 2 min read

While creating a custom rest API in Magento 2, you may need to work with the order total and use their values. Today, I have come up with the blog to get total segment using Magento 2 rest API.

Steps to Get Total Segment Using Magento 2 Rest API:

1. Create app/code/Vendor/Extension/etc/webapi.xml file and put the below code:

<?xml version="1.0" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">

    <route url="/V1/Extension/guest-carts/:cartId/" method="POST">
        <service class="Vendor\Extension\Api\GuestFeesInformationManagementInterface" method="collect"/>
        <resources>
            <resource ref="anonymous" />
        </resources>
    </route>
</routes>

2. Now, create app/code/Vendor/Extension/Api/GuestFeesInformationManagementInterface.php and put below code:

<?php

namespace Vendor\Extension\Api;


interface GuestFeesInformationManagementInterface
{
    /**
     * @param string $cartId
     * @param \Magento\Checkout\Api\Data\TotalsInformationInterface $addressInformation
     *
     * @return string
     */
    public function collect(
        $cartId,
        \Magento\Checkout\Api\Data\TotalsInformationInterface $addressInformation
    );
}

3. Go to app/code/Vendor/Extension/etc/di.xml and put the below code:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Vendor\Extension\Api\GuestFeesInformationManagementInterface" type="Vendor\Extension\Model\Api\GuestFeesInformationManagement"/>
</config>

4. Create app/code/Vendor/Extension/Model/Api/GuestFeesInformationManagement.php file with the below code:

<?php

namespace Vendor\Extension\Model\Api;

use Vendor\Extension\Api\GuestFeesInformationManagementInterface;
use Magento\Quote\Model\QuoteIdMaskFactory;
use Magento\Checkout\Model\TotalsInformationManagement as CheckoutTotalsInformationManagement;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Quote\Address\Total;
use Magento\Quote\Api\CartTotalRepositoryInterface;

/**
 * Class GuestFeesInformationManagement
 * @package Vendor\Extension\Model\Api
 */
class GuestFeesInformationManagement implements GuestFeesInformationManagementInterface
{
    /** @var QuoteIdMaskFactory */
    protected $quoteIdMaskFactory;
    /**
     * @var CheckoutTotalsInformationManagement
     */
    protected $checkoutTotalsInformationManagement;
    /**
     * @var CartRepositoryInterface
     */
    protected $cartRepository;
    /**
     * @var Total
     */
    protected $total;
    /**
     * @var CartTotalRepositoryInterface
     */
    protected $cartTotalRepository;


    /**
     * @param QuoteIdMaskFactory $quoteIdMaskFactory
     */
    public function __construct(
        QuoteIdMaskFactory $quoteIdMaskFactory,
        CheckoutTotalsInformationManagement $checkoutTotalsInformationManagement,
        CartRepositoryInterface $cartRepository,
        Total $total,
        CartTotalRepositoryInterface $cartTotalRepository
    )
    {
        $this->quoteIdMaskFactory = $quoteIdMaskFactory;
        $this->cartRepository = $cartRepository;
        $this->checkoutTotalsInformationManagement = $checkoutTotalsInformationManagement;
        $this->total = $total;
        $this->cartTotalRepository = $cartTotalRepository;
    }

    /**
     * @param string $cartId
     * @param \Magento\Checkout\Api\Data\TotalsInformationInterface $addressInformation
     *
     * @return string
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function collect(
        $cartId,
        \Magento\Checkout\Api\Data\TotalsInformationInterface $addressInformation
    )
    {

        try {
            $quote = $this->cartRepository->get($cartId);
        } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
            \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info($e->getMessage());
            return '';
        }


        $totals = $this->cartTotalRepository->get($quote->getId());
        $totalSagement = [];
        foreach ($totals->getTotalSegments() as $totalSegment) {
            $totalSegmentArray = $totalSegment->toArray();
            $totalSagement[$totalSegmentArray['code']] = $totalSegmentArray['value'];
        }

        $returnArr = json_encode($totalSagement);
        return $returnArr;
    }
}

Also, if you liked the post, share it to get maximum Magento people to use this functionality.

Sanjay Jethva Full Image
Article bySanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.