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:
- Create app/code/Vendor/Extension/etc/webapi.xml file and put the below code:
12345678910<?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> - Now, create app/code/Vendor/Extension/Api/GuestFeesInformationManagementInterface.php and put below code:
123456789101112131415161718<?phpnamespace 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);} - Go to app/code/Vendor/Extension/etc/di.xml and put the below code:
1234<?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> - Create app/code/Vendor/Extension/Model/Api/GuestFeesInformationManagement.php file with the below code:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687<?phpnamespace 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;}}
Let me know how did you use the above code in your rest API. Do share your queries, questions, and suggestions in the comments section below. Also, if you liked the post, share it to get maximum Magento people to use this functionality.
Sanjay 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.
1 Comment
You post excellent solutions in the Magento niche. Big fan!