How to Get Total Segment Using Magento 2 Rest API

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.

Table of Contents