1

I need add a new field in the response of cart totals API example.com /rest/V1/carts/mine/totals/. In order to do that, I used new interface which extends the actual interface Magento\Quote\Api\Data\TotalsInterface and created corresponding model which extends \Magento\Quote\Model\Cart\Totals. Company\MobileApi\Api\Quote\TotalsInterface

    <?php
namespace Company\MobileApi\Api\Quote;

    interface TotalsInterface extends \Magento\Quote\Api\Data\TotalsInterface
    {
        /**
         * free shipping amount.
         */
        const KEY_FREE_SHIPPING_AMOUNT = 'free_shipping_amount';
        /**
         * Get free shipping amount
         *
         * @return float|null
         */
        public function getFreeShippingAmount();

        /**
         * Set ree shipping amount in quote
         *
         * @param float $freeShippingAmount
         * @return $this
         */
        public function setFreeShippingAmount($freeShippingAmount);
    }

Company\MobileApis\Api\Quote\Totals

<?php

namespace Faya\MobileApis\Api\Quote; use Faya\MobileApi\Api\Quote\TotalsInterface;

class Totals extends \Magento\Quote\Model\Cart\Totals implements TotalsInterface
{
    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {
        $this->config = $scopeConfig;
    }
    /**
     * Get free shipping amount
     *
     * @return float|null
     */
    public function getFreeShippingAmount() {
        return $this->getData(500);
        //return $this->config->getValue('carriers/ups/free_shipping_subtotal');
    }

    /**
     * Set grand total in quote currency
     *
     * @param float $freeShippingAmount
     * @return $this
     */
    public function setFreeShippingAmount($freeShippingAmount) {
        return $this->setData(self::KEY_FREE_SHIPPING_AMOUNT, 500);
    }
}

Then in the API main class

public function __construct(
        \Company\MobileApi\Api\Quote\TotalsInterface $totalsFactory
    ) {
        $this->totalsFactory = $totalsFactory;
    }
public function get($cartId)
    {
        $quote = $this->quoteRepository->getActive($cartId);
        $quoteTotals = $this->totalsFactory->create();
        $this->dataObjectHelper->populateWithArray(
            $quoteTotals,
            $addressTotalsData,
            '\Company\MobileApi\Api\Quote\TotalsInterface'
        );
        $items = [];
        foreach ($quote->getAllVisibleItems() as $index => $item) {
            $items[$index] = $this->itemConverter->modelToDataObject($item);
        }
        $calculatedTotals = $this->totalsConverter->process($addressTotals);
        $quoteTotals->setTotalSegments($calculatedTotals);

        $amount = $quoteTotals->getGrandTotal() - $quoteTotals->getTaxAmount();
        $amount = $amount > 0 ? $amount : 0;
        $quoteTotals->setCouponCode($this->couponService->get($cartId));
        $quoteTotals->setGrandTotal($amount);
        $quoteTotals->setItems($items);
        $quoteTotals->setFreeShippingAmount(200.00);
        //return $quoteTotals->setFreeShippingAmount(200.00);//return []
        return $quoteTotals;
    }

In the response of API, I'm not able to get the new variable.

I think, I'm missing something to get a variable in response. Can anyone help on this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.