{"id":773,"date":"2020-01-23T11:45:40","date_gmt":"2020-01-23T11:45:40","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/01\/23\/get-total-segment-using-magento-2-rest-api\/"},"modified":"2025-05-22T15:07:09","modified_gmt":"2025-05-22T09:37:09","slug":"get-total-segment-using-magento-2-rest-api","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-total-segment-using-magento-2-rest-api\/","title":{"rendered":"How to Get Total Segment Using Magento 2 Rest API"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">While <a href=\"http:\/\/meetanshi.com\/blog\/create-custom-rest-api-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">creating a custom rest API in Magento 2<\/a>, you may need to work with the order total and use their values. Today, I have come up with the blog to get&nbsp;total segment using Magento 2 rest API.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to&nbsp;Get Total Segment Using Magento 2 Rest API:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create app\/code\/Vendor\/Extension\/etc\/webapi.xml file and put the below code:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" ?>\n&lt;routes xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Webapi:etc\/webapi.xsd\">\n\n    &lt;route url=\"\/V1\/Extension\/guest-carts\/:cartId\/\" method=\"POST\">\n        &lt;service class=\"Vendor\\Extension\\Api\\GuestFeesInformationManagementInterface\" method=\"collect\"\/>\n        &lt;resources>\n            &lt;resource ref=\"anonymous\" \/>\n        &lt;\/resources>\n    &lt;\/route>\n&lt;\/routes><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Now, create app\/code\/Vendor\/Extension\/Api\/GuestFeesInformationManagementInterface.php and put below code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Extension\\Api;\n\n\ninterface GuestFeesInformationManagementInterface\n{\n    \/**\n     * @param string $cartId\n     * @param \\Magento\\Checkout\\Api\\Data\\TotalsInformationInterface $addressInformation\n     *\n     * @return string\n     *\/\n    public function collect(\n        $cartId,\n        \\Magento\\Checkout\\Api\\Data\\TotalsInformationInterface $addressInformation\n    );\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Go to app\/code\/Vendor\/Extension\/etc\/di.xml and put the below code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n    &lt;preference for=\"Vendor\\Extension\\Api\\GuestFeesInformationManagementInterface\" type=\"Vendor\\Extension\\Model\\Api\\GuestFeesInformationManagement\"\/>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. Create app\/code\/Vendor\/Extension\/Model\/Api\/GuestFeesInformationManagement.php file with the below code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Extension\\Model\\Api;\n\nuse Vendor\\Extension\\Api\\GuestFeesInformationManagementInterface;\nuse Magento\\Quote\\Model\\QuoteIdMaskFactory;\nuse Magento\\Checkout\\Model\\TotalsInformationManagement as CheckoutTotalsInformationManagement;\nuse Magento\\Quote\\Api\\CartRepositoryInterface;\nuse Magento\\Quote\\Model\\Quote\\Address\\Total;\nuse Magento\\Quote\\Api\\CartTotalRepositoryInterface;\n\n\/**\n * Class GuestFeesInformationManagement\n * @package Vendor\\Extension\\Model\\Api\n *\/\nclass GuestFeesInformationManagement implements GuestFeesInformationManagementInterface\n{\n    \/** @var QuoteIdMaskFactory *\/\n    protected $quoteIdMaskFactory;\n    \/**\n     * @var CheckoutTotalsInformationManagement\n     *\/\n    protected $checkoutTotalsInformationManagement;\n    \/**\n     * @var CartRepositoryInterface\n     *\/\n    protected $cartRepository;\n    \/**\n     * @var Total\n     *\/\n    protected $total;\n    \/**\n     * @var CartTotalRepositoryInterface\n     *\/\n    protected $cartTotalRepository;\n\n\n    \/**\n     * @param QuoteIdMaskFactory $quoteIdMaskFactory\n     *\/\n    public function __construct(\n        QuoteIdMaskFactory $quoteIdMaskFactory,\n        CheckoutTotalsInformationManagement $checkoutTotalsInformationManagement,\n        CartRepositoryInterface $cartRepository,\n        Total $total,\n        CartTotalRepositoryInterface $cartTotalRepository\n    )\n    {\n        $this->quoteIdMaskFactory = $quoteIdMaskFactory;\n        $this->cartRepository = $cartRepository;\n        $this->checkoutTotalsInformationManagement = $checkoutTotalsInformationManagement;\n        $this->total = $total;\n        $this->cartTotalRepository = $cartTotalRepository;\n    }\n\n    \/**\n     * @param string $cartId\n     * @param \\Magento\\Checkout\\Api\\Data\\TotalsInformationInterface $addressInformation\n     *\n     * @return string\n     * @throws \\Magento\\Framework\\Exception\\LocalizedException\n     *\/\n    public function collect(\n        $cartId,\n        \\Magento\\Checkout\\Api\\Data\\TotalsInformationInterface $addressInformation\n    )\n    {\n\n        try {\n            $quote = $this->cartRepository->get($cartId);\n        } catch (\\Magento\\Framework\\Exception\\NoSuchEntityException $e) {\n            \\Magento\\Framework\\App\\ObjectManager::getInstance()->get('Psr\\Log\\LoggerInterface')->info($e->getMessage());\n            return '';\n        }\n\n\n        $totals = $this->cartTotalRepository->get($quote->getId());\n        $totalSagement = [];\n        foreach ($totals->getTotalSegments() as $totalSegment) {\n            $totalSegmentArray = $totalSegment->toArray();\n            $totalSagement[$totalSegmentArray['code']] = $totalSegmentArray['value'];\n        }\n\n        $returnArr = json_encode($totalSagement);\n        return $returnArr;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Also, if you liked the post, share it to get maximum Magento people to use this functionality.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-773","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/773","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=773"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/773\/revisions"}],"predecessor-version":[{"id":15120,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/773\/revisions\/15120"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}