How to Get Total Segment Using Magento 2 Rest API

Steps to Get Total Segment Using Magento 2 Rest API

Create webapi.xml

Directory

app/code/VendorName/ModuleName/etc/webapi.xml

Content of webapi.xml

<?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="/VendorName/ModuleName/guest-carts/:cartId/" method="POST">
      <service class="VendorName\ModuleName\Api\GuestFeesInformationManagementInterface" method="collect"/>
        <resources>
            <resource ref="anonymous" />
        </resources>
    </route>
</routes>

Create GuestFeesInformationManagementInterface.php

Directory

app/code/VendorName/ModuleName/Api/GuestFeesInformationManagementInterface.php

Content of GuestFeesInformationManagementInterface.php

<?php
namespace VendorName\ModuleName\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
    );
}

Create di.xml 

Directory

app/code/VendorName/ModuleName/etc/di.xml

Content of di.xml

<?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="VendorName\ModuleName\Model\Api\GuestFeesInformationManagementInterface" type="Vendor\Extension\Model\Api\GuestFeesInformationManagement"/>
</config>

Create GuestFeesInformationManagement.php

Directory

app/code/VendorName/ModuleName/Model/Api/GuestFeesInformationManagement.php

Content of GuestFeesInformationManagement.php

<?php
namespace VendorName\ModuleName\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;
    }
}

If you are looking for Magento Developers, visit Magento Developer Agency.

Related Products